Skip to content

Commit

Permalink
Update dbus-java to 3.3.0 to fix hypfvieh/dbus-java#128 and cryptomat…
Browse files Browse the repository at this point in the history
  • Loading branch information
swiesend committed Apr 16, 2021
1 parent ac014cd commit 60f032c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

<!-- 3rd party dependencies -->
<hkdf.version>1.1.0</hkdf.version>
<dbus-java.version>3.2.4</dbus-java.version>
<dbus-java.version>3.3.0</dbus-java.version>
<slf4j.version>1.7.30</slf4j.version>

<!-- test dependencies -->
Expand Down
48 changes: 27 additions & 21 deletions src/main/java/org/freedesktop/secret/simple/SimpleCollection.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.freedesktop.secret.simple;

import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.ObjectPath;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.interfaces.DBus;
import org.freedesktop.dbus.types.Variant;
import org.freedesktop.secret.*;
import org.freedesktop.secret.interfaces.Prompt.Completed;
Expand Down Expand Up @@ -34,22 +34,7 @@
public final class SimpleCollection extends org.freedesktop.secret.simple.interfaces.SimpleCollection {

private static final Logger log = LoggerFactory.getLogger(SimpleCollection.class);
private static DBusConnection connection = null;

static {
try {
connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
} catch (DBusException | RuntimeException e) {
if (e == null) {
log.warn("Could not communicate properly with the D-Bus.");
} else {
log.warn("Could not communicate properly with the D-Bus: " + e.getMessage() + " (" + e.getClass().getSimpleName() + ")");
}
} finally {
disconnect();
}
}

private static DBusConnection connection = getConnection();
private TransportEncryption transport = null;
private Service service = null;
private Session session = null;
Expand All @@ -72,7 +57,7 @@ public SimpleCollection() throws IOException {
ObjectPath path = Static.Convert.toObjectPath(Static.ObjectPaths.DEFAULT_COLLECTION);
collection = new Collection(path, service);
} catch (RuntimeException e) {
throw new IOException("Could not communicate properly with the secret service.", e);
throw new IOException("Could not initialize the secret service.", e);
}
}

Expand Down Expand Up @@ -139,8 +124,29 @@ public SimpleCollection(String label, CharSequence password) throws IOException
collection = new Collection(path, service);
}
} catch (RuntimeException e) {
throw new IOException("Could not communicate properly with the secret service.", e);
throw new IOException("Could not initialize the secret service.", e);
}
}

/**
* Try to get a D-Bus connection.
* Sets up a shutdown hook to close the connection at the end of the static lifetime.
*
* @return a DBusConnection or null
*/
private static final DBusConnection getConnection() {
try {
return DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
} catch (DBusException e) {
if (e == null) {
log.warn("Could not communicate properly with the D-Bus.");
} else {
log.warn("Could not communicate properly with the D-Bus: " + e.getMessage() + " (" + e.getClass().getSimpleName() + ")");
}
} finally {
disconnect();
}
return null;
}

/**
Expand All @@ -151,7 +157,7 @@ public SimpleCollection(String label, CharSequence password) throws IOException
public static boolean isAvailable() {
if (connection != null && connection.isConnected()) {
try {
DBus bus = connection.getRemoteObject(
DBus bus = connection.getRemoteObject(
Static.DBus.Service.DBUS,
Static.DBus.ObjectPaths.DBUS,
DBus.class);
Expand All @@ -169,7 +175,7 @@ public static boolean isAvailable() {
transport.close();

return isSessionSupported;
} catch (DBusException | ExceptionInInitializerError | RuntimeException e) {
} catch (DBusException | ExceptionInInitializerError e) {
log.warn("The secret service is not available. You may want to install the `gnome-keyring` package. Is the `gnome-keyring-daemon` running?", e);
return false;
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
Expand Down

0 comments on commit 60f032c

Please sign in to comment.