- String commandKey = ns.getString("command");
- final Map<String, Command> commands = Commands.getCommands();
- if (commands.containsKey(commandKey)) {
- Command command = commands.get(commandKey);
-
- if (dBusConn != null) {
- if (command instanceof ExtendedDbusCommand) {
- return ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
- } else if (command instanceof DbusCommand) {
- return ((DbusCommand) command).handleCommand(ns, ts);
- } else {
- System.err.println(commandKey + " is not yet implemented via dbus");
- return 1;
- }
- } else {
- if (command instanceof LocalCommand) {
- return ((LocalCommand) command).handleCommand(ns, m);
- } else if (command instanceof DbusCommand) {
- return ((DbusCommand) command).handleCommand(ns, ts);
- } else {
- System.err.println(commandKey + " is only works via dbus");
- return 1;
- }
- }
+ return handleCommands(ns, m);
+ } catch (IOException e) {
+ logger.error("Cleanup failed", e);
+ return 3;
+ }
+ }
+
+ private static int initDbusClient(final Namespace ns, final boolean systemBus) {
+ try {
+ DBusConnection.DBusBusType busType;
+ if (systemBus) {
+ busType = DBusConnection.DBusBusType.SYSTEM;
+ } else {
+ busType = DBusConnection.DBusBusType.SESSION;
+ }
+ try (DBusConnection dBusConn = DBusConnection.getConnection(busType)) {
+ Signal ts = dBusConn.getRemoteObject(DbusConfig.SIGNAL_BUSNAME,
+ DbusConfig.SIGNAL_OBJECTPATH,
+ Signal.class);
+
+ return handleCommands(ns, ts, dBusConn);
+ }
+ } catch (DBusException | IOException e) {
+ logger.error("Dbus client failed", e);
+ return 3;
+ }
+ }
+
+ private static int handleCommands(Namespace ns, Signal ts, DBusConnection dBusConn) {
+ String commandKey = ns.getString("command");
+ final Map<String, Command> commands = Commands.getCommands();
+ if (commands.containsKey(commandKey)) {
+ Command command = commands.get(commandKey);
+
+ if (command instanceof ExtendedDbusCommand) {
+ return ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
+ } else if (command instanceof DbusCommand) {
+ return ((DbusCommand) command).handleCommand(ns, ts);
+ } else {
+ System.err.println(commandKey + " is not yet implemented via dbus");
+ return 1;