Skip to content

Commit

Permalink
Compatibility to KWallet Framework 6
Browse files Browse the repository at this point in the history
  • Loading branch information
purejava committed Dec 17, 2023
1 parent 7fc1f12 commit ef28dd5
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 108 deletions.
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class App {
try {
connection = DBusConnectionBuilder.forSessionBus().withShared(false).build();

var service = connection.getRemoteObject("org.kde.kwalletd5",
"/modules/kwalletd5", KWallet.class);
var service = connection.getRemoteObject("org.kde.kwalletd6",
"/modules/kwalletd6", KWallet.class);

var wallet = "kdewallet";
var wId = 0;
Expand Down
54 changes: 43 additions & 11 deletions src/main/java/org/purejava/kwallet/KDEWallet.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package org.purejava.kwallet;

import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.purejava.kwallet.freedesktop.dbus.handlers.Messaging;
import org.freedesktop.dbus.interfaces.DBus;
import org.freedesktop.dbus.messages.DBusSignal;
import org.freedesktop.dbus.types.Variant;
import org.purejava.kwallet.KWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class KDEWallet extends Messaging implements KWallet, AutoCloseable {

private Logger log = LoggerFactory.getLogger(KDEWallet.class);
private static final Logger LOG = LoggerFactory.getLogger(KDEWallet.class);
private static String SERVICE;
private static String OBJECT_PATHS;
private DBusConnection connection;

public static final List<Class<? extends DBusSignal>> signals = Arrays.asList(
public static final List<Class<? extends DBusSignal>> SIGNALS = Arrays.asList(
applicationDisconnected.class,
folderUpdated.class,
folderListUpdated.class,
Expand All @@ -35,26 +38,55 @@ public class KDEWallet extends Messaging implements KWallet, AutoCloseable {

public KDEWallet(DBusConnection connection) {
super(connection,
signals,
Static.Service.KWALLETD5,
Static.ObjectPaths.KWALLETD5,
SIGNALS,
SERVICE,
OBJECT_PATHS,
Static.Interfaces.KWALLET);
this.connection = connection;
}

static {
try (var connection = DBusConnectionBuilder.forSessionBus().withShared(false).build()) {
try {
var bus = connection.getRemoteObject("org.freedesktop.DBus",
"/org/freedesktop/DBus", DBus.class);
if (Arrays.asList(bus.ListActivatableNames()).contains(Static.Service.KWALLETD6)) {
SERVICE = Static.Service.KWALLETD6;
OBJECT_PATHS = Static.ObjectPaths.KWALLETD6;
LOG.debug("Kwallet daemon v6 initialized");
} else if (Arrays.asList(bus.ListActivatableNames()).contains(Static.Service.KWALLETD5)) {
SERVICE = Static.Service.KWALLETD5;
OBJECT_PATHS = Static.ObjectPaths.KWALLETD5;
LOG.debug("Kwallet daemon v5 initialized");
}
} catch (DBusException e) {
LOG.error(e.toString(), e.getCause());
}
} catch (IOException | DBusException e) {
LOG.error(e.toString(), e.getCause());
}
}

@Override
public boolean isEnabled() {
if (null == connection) {
LOG.debug("No d-bus connection available");
return false;
}
try {
var bus = connection.getRemoteObject("org.freedesktop.DBus",
"/org/freedesktop/DBus", DBus.class);
if (Arrays.asList(bus.ListActivatableNames()).contains("org.kde.kwalletd5")) {
log.debug("Kwallet daemon is available.");
if (Arrays.asList(bus.ListActivatableNames()).contains(Static.Service.KWALLETD6)) {
LOG.debug("Kwallet daemon v6 is available");
return true;
} else if (Arrays.asList(bus.ListActivatableNames()).contains(Static.Service.KWALLETD5)) {
LOG.debug("Kwallet daemon v5 is available");
return true;
} else {
return false;
return false;
}
} catch (DBusException e) {
log.error(e.toString(), e.getCause());
LOG.error(e.toString(), e.getCause());
return false;
}
}
Expand Down Expand Up @@ -307,7 +339,7 @@ public void close() {
try {
if (null != connection && connection.isConnected()) connection.disconnect();
} catch (Exception e) {
log.error(e.toString(), e.getCause());
LOG.error(e.toString(), e.getCause());
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/purejava/kwallet/Static.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public static class Interfaces {

public static class Service {
public static final String KWALLETD5 = "org.kde.kwalletd5";
public static final String KWALLETD6 = "org.kde.kwalletd6";
}

public static class ObjectPaths {
public static final String KWALLETD5 = "/modules/kwalletd5";
public static final String KWALLETD6 = "/modules/kwalletd6";
}

public static class Interfaces {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class MessageHandler {

private Logger log = LoggerFactory.getLogger(MessageHandler.class);
private static final Logger LOG = LoggerFactory.getLogger(MessageHandler.class);

private DBusConnection connection;

Expand All @@ -35,16 +35,16 @@ public Object[] send(String service, String path, String iface, String method, S
iface,
method, (byte) 0, signature, args);

if (log.isTraceEnabled()) log.trace(String.valueOf(message));
if (LOG.isTraceEnabled()) LOG.trace(String.valueOf(message));
connection.sendMessage(message);

var response = message.getReply(2000L);
if (log.isTraceEnabled()) log.trace(String.valueOf(response));
if (LOG.isTraceEnabled()) LOG.trace(String.valueOf(response));

Object[] parameters = null;
if (response != null) {
parameters = response.getParameters();
log.debug(Arrays.deepToString(parameters));
LOG.debug(Arrays.deepToString(parameters));
}

if (response instanceof org.freedesktop.dbus.errors.Error) {
Expand All @@ -53,7 +53,7 @@ public Object[] send(String service, String path, String iface, String method, S
case "org.freedesktop.DBus.Error.NoReply",
"org.freedesktop.DBus.Error.UnknownMethod",
"org.freedesktop.dbus.exceptions.NotConnected" -> {
log.debug(error);
LOG.debug(error);
return null;
}
default -> throw new DBusException(error);
Expand All @@ -62,7 +62,7 @@ public Object[] send(String service, String path, String iface, String method, S
return parameters;

} catch (DBusException e) {
log.error("Unexpected D-Bus response:", e);
LOG.error("Unexpected D-Bus response:", e);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import org.freedesktop.dbus.messages.DBusSignal;
import org.freedesktop.dbus.types.Variant;
import org.purejava.kwallet.Static;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

abstract public class Messaging {

private static final Logger LOG = LoggerFactory.getLogger(Messaging.class);
private DBusConnection connection;
private MessageHandler msg;
private SignalHandler sh = SignalHandler.getInstance();
Expand All @@ -24,6 +27,9 @@ public Messaging(DBusConnection connection, List<Class<? extends DBusSignal>> si
if (null != signals) {
this.sh.connect(connection, signals);
}
if (null == serviceName || null == objectPath) {
LOG.error("Severe error: Kwallet daemon not initialized properly");
}
this.serviceName = serviceName;
this.objectPath = objectPath;
this.interfaceName = interfaceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

public class SignalHandler implements DBusSigHandler {

private static SignalHandler instance = new SignalHandler();

private Logger log = LoggerFactory.getLogger(SignalHandler.class);
private static final Logger LOG = LoggerFactory.getLogger(SignalHandler.class);

private PropertyChangeSupport support;
private DBusConnection connection = null;
Expand Down Expand Up @@ -59,7 +56,7 @@ public void connect(DBusConnection connection, List<Class<? extends DBusSignal>>
}
}
} catch (DBusException e) {
log.error(e.toString(), e.getCause());
LOG.error(e.toString(), e.getCause());
}
}
}
Expand All @@ -73,39 +70,39 @@ public void handle(DBusSignal s) {

if (s instanceof KWallet.walletOpened wo) {
support.firePropertyChange("KWallet.walletOpened", null, wo.wallet);
log.info("Received signal KWallet.walletOpened: {}", wo.wallet);
LOG.info("Received signal KWallet.walletOpened: {}", wo.wallet);
} else if (s instanceof KWallet.walletAsyncOpened wo) {
support.firePropertyChange("KWallet.walletAsyncOpened", null, wo.handle);
log.info("Received signal KWallet.walletAsyncOpened: {TransactionID: {}, handle: {}}", wo.tId, wo.handle);
LOG.info("Received signal KWallet.walletAsyncOpened: {TransactionID: {}, handle: {}}", wo.tId, wo.handle);
} else if (s instanceof KWallet.walletDeleted wd) {
support.firePropertyChange("KWallet.walletDeleted", null, wd.wallet);
log.info("Received signal KWallet.walletDeleted: {}", wd.wallet);
LOG.info("Received signal KWallet.walletDeleted: {}", wd.wallet);
} else if (s instanceof KWallet.walletClosedId wc) {
support.firePropertyChange("KWallet.walletClosedId", null, wc.handle);
log.info("Received signal KWallet.walletClosedId: {}", wc.handle);
LOG.info("Received signal KWallet.walletClosedId: {}", wc.handle);
} else if (s instanceof KWallet.walletClosed wc) {
support.firePropertyChange("KWallet.walletClosed", null, wc.wallet);
log.info("Received signal KWallet.walletClosed: {}", wc.wallet);
LOG.info("Received signal KWallet.walletClosed: {}", wc.wallet);
} else if (s instanceof KWallet.allWalletsClosed) {
support.firePropertyChange("KWallet.allWalletsClosed", null, s.getPath());
log.info("Received signal KWallet.allWalletsClosed: {}", s.getPath());
LOG.info("Received signal KWallet.allWalletsClosed: {}", s.getPath());
} else if (s instanceof KWallet.folderListUpdated flu) {
support.firePropertyChange("KWallet.folderListUpdated", null, flu.wallet);
log.info("Received signal KWallet.folderListUpdated: {}", flu.wallet);
LOG.info("Received signal KWallet.folderListUpdated: {}", flu.wallet);
} else if (s instanceof KWallet.folderUpdated fu) {
support.firePropertyChange("KWallet.folderUpdated", null, fu.a + "/" + fu.b);
log.info("Received signal KWallet.folderUpdated: {wallet: {}, folder: {}}", fu.a, fu.b);
LOG.info("Received signal KWallet.folderUpdated: {wallet: {}, folder: {}}", fu.a, fu.b);
} else if (s instanceof KWallet.applicationDisconnected ad) {
support.firePropertyChange("KWallet.applicationDisconnected", null, ad.application + "/" + ad.wallet);
log.info("Received signal KWallet.applicationDisconnected: {application: {}, wallet: {}}", ad.application, ad.wallet);
LOG.info("Received signal KWallet.applicationDisconnected: {application: {}, wallet: {}}", ad.application, ad.wallet);
} else if (s instanceof KWallet.walletListDirty) {
support.firePropertyChange("KWallet.walletListDirty", null, s.getPath());
log.debug("Received signal KWallet.walletListDirty: {}", s.getPath());
LOG.debug("Received signal KWallet.walletListDirty: {}", s.getPath());
} else if (s instanceof KWallet.walletCreated wc) {
support.firePropertyChange("KWallet.walletCreated", null, wc.wallet);
log.info("Received signal KWallet.walletCreated: {}", wc.wallet);
LOG.info("Received signal KWallet.walletCreated: {}", wc.wallet);
} else {
log.warn("Received unknown signal: {} {{}}", s.getClass().toString(), s);
LOG.warn("Received unknown signal: {} {{}}", s.getClass(), s);
}
}

Expand All @@ -115,15 +112,15 @@ public DBusSignal[] getHandledSignals() {

public <S extends DBusSignal> List<S> getHandledSignals(Class<S> s) {
return Arrays.stream(handled)
.filter(signal -> signal != null)
.filter(Objects::nonNull)
.filter(signal -> signal.getClass().equals(s))
.map(signal -> (S) signal)
.collect(Collectors.toList());
}

public <S extends DBusSignal> List<S> getHandledSignals(Class<S> s, String path) {
return Arrays.stream(handled)
.filter(signal -> signal != null)
.filter(Objects::nonNull)
.filter(signal -> signal.getClass().equals(s))
.filter(signal -> signal.getPath().equals(path))
.map(signal -> (S) signal)
Expand Down Expand Up @@ -165,12 +162,12 @@ public <S extends DBusSignal> S await(Class<S> s, String path, Callable action,
try {
action.call();
} catch (Exception e) {
log.error(e.toString(), e.getCause());
LOG.error(e.toString(), e.getCause());
}

var executor = Executors.newSingleThreadExecutor();

log.info("Await signal {}" + "({}) within {} seconds.", s.getName(), path, timeout.getSeconds());
LOG.info("Await signal {}" + "({}) within {} seconds.", s.getName(), path, timeout.getSeconds());

final Future<S> handler = executor.submit((Callable) () -> {
var await = init;
Expand All @@ -192,7 +189,7 @@ public <S extends DBusSignal> S await(Class<S> s, String path, Callable action,
return handler.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (TimeoutException | InterruptedException | ExecutionException e) {
handler.cancel(true);
log.warn(e.toString(), e.getCause());
LOG.warn(e.toString(), e.getCause());
} finally {
executor.shutdownNow();
}
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/org/purejava/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Context {

private Logger log;
private static final Logger LOG = LoggerFactory.getLogger(Context.class);

public DBusConnection connection = null;

public Context(Logger log) {
this.log = log;
}
public Context() { }

public void ensureService() {
try {
connection = DBusConnectionBuilder.forSessionBus().withShared(false).build();
} catch (DBusException e) {
log.error(e.toString(), e.getCause());
LOG.error(e.toString(), e.getCause());
}
}

Expand All @@ -28,7 +27,7 @@ public void after() {
connection.disconnect();
Thread.sleep(150L);
} catch (InterruptedException e) {
log.error(e.toString(), e.getCause());
LOG.error(e.toString(), e.getCause());
}
}
}
Loading

0 comments on commit ef28dd5

Please sign in to comment.