Skip to content

Refactor DBus Connection Initialization #6

Closed

Description

@overheadhunter

Establishing a DBus connection is currently done in KDEWalletKeychainAccess's constructor:

public KDEWalletKeychainAccess() {
ConnectedWallet wallet = null;
try {
DBusConnection conn = null;
try {
conn = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
} catch (RuntimeException e) {
if (e.getMessage() == "Cannot Resolve Session Bus Address") {
LOG.warn("SESSION DBus not found.");
}
}
if (conn == null) {
conn = DBusConnection.getConnection(DBusConnection.DBusBusType.SYSTEM);
}
wallet = new ConnectedWallet(conn);
} catch (DBusException e) {
LOG.warn("Connecting to D-Bus failed.", e);
}
this.wallet = Optional.ofNullable(wallet);
}

Due to the nature of final fields and variable scope, this code isn't really beautiful. Instead we could add a factory method (static Optional<ConnectedWallet> connect()) with the "try-retry-fail" stuff in ConnectedWallet. Then simply invoke this from the constructor: this.wallet = ConnectedWallet.connect().

This should eliminate the need for local variables, as we can just return from inside the try-catch blocks.

Activity

  1. added a commit that references this issue on Jun 7, 2021
    632f89d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions