Skip to content

Commit 632f89d

Browse files
Refactor DBus Connection Initialization (#9)
Refactor DBus Connection Initialization Fixes #6 Co-authored-by: Sebastian Stenzel <[email protected]>
1 parent fe54f7e commit 632f89d

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<!-- runtime dependencies -->
4141
<api.version>1.0.0-beta2</api.version>
4242
<secret-service.version>1.6.2</secret-service.version>
43-
<kdewallet.version>1.1.1</kdewallet.version>
43+
<kdewallet.version>1.2.1</kdewallet.version>
4444
<guava.version>30.0-jre</guava.version>
4545
<slf4j.version>1.7.30</slf4j.version>
4646
<junit.version>5.7.0</junit.version>

src/main/java/org/cryptomator/linux/keychain/KDEWalletKeychainAccess.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.cryptomator.integrations.keychain.KeychainAccessException;
55
import org.cryptomator.integrations.keychain.KeychainAccessProvider;
66
import org.freedesktop.dbus.connections.impl.DBusConnection;
7+
import org.freedesktop.dbus.exceptions.DBusConnectionException;
78
import org.freedesktop.dbus.exceptions.DBusException;
89
import org.kde.KWallet;
910
import org.kde.Static;
@@ -22,24 +23,7 @@ public class KDEWalletKeychainAccess implements KeychainAccessProvider {
2223
private final Optional<ConnectedWallet> wallet;
2324

2425
public KDEWalletKeychainAccess() {
25-
ConnectedWallet wallet = null;
26-
try {
27-
DBusConnection conn = null;
28-
try {
29-
conn = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
30-
} catch (RuntimeException e) {
31-
if (e.getMessage() == "Cannot Resolve Session Bus Address") {
32-
LOG.warn("SESSION DBus not found.");
33-
}
34-
}
35-
if (conn == null) {
36-
conn = DBusConnection.getConnection(DBusConnection.DBusBusType.SYSTEM);
37-
}
38-
wallet = new ConnectedWallet(conn);
39-
} catch (DBusException e) {
40-
LOG.warn("Connecting to D-Bus failed.", e);
41-
}
42-
this.wallet = Optional.ofNullable(wallet);
26+
this.wallet = ConnectedWallet.connect();
4327
}
4428

4529
@Override
@@ -85,6 +69,28 @@ public ConnectedWallet(DBusConnection connection) {
8569
this.wallet = new KDEWallet(connection);
8670
}
8771

72+
static Optional<ConnectedWallet> connect() {
73+
try {
74+
return Optional.of(new ConnectedWallet(getConnection()));
75+
} catch (DBusException e) {
76+
LOG.warn("Connecting to D-Bus failed.", e);
77+
return Optional.empty();
78+
}
79+
}
80+
81+
private static DBusConnection getConnection() throws DBusException {
82+
try {
83+
return DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
84+
} catch (DBusConnectionException ce) {
85+
LOG.warn("SESSION DBus not found, falling back to SYSTEM DBus");
86+
try {
87+
return DBusConnection.getConnection(DBusConnection.DBusBusType.SYSTEM);
88+
} catch (DBusException e) {
89+
throw e;
90+
}
91+
}
92+
}
93+
8894
public boolean isSupported() {
8995
return wallet.isEnabled();
9096
}

0 commit comments

Comments
 (0)