All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.thjom.java.systemd.AbstractAdapter Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
/*
 * Java-systemd implementation
 * Copyright (c) 2016 Markus Enax
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of either the GNU Lesser General Public License Version 2 or the
 * Academic Free Licence Version 3.0.
 *
 * Full licence texts are included in the COPYING file with this program.
 */

package de.thjom.java.systemd;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.freedesktop.DBus.Properties.PropertiesChanged;
import org.freedesktop.dbus.DBusSigHandler;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.exceptions.DBusException;

abstract class AbstractAdapter {

    protected final List unitStateListeners = new ArrayList<>();

    private final List> forwarders = new ArrayList<>();

    private ForwardingHandler defaultHandler;

    protected AbstractAdapter() {
        super();
    }

    public abstract  void addHandler(final Class type, final DBusSigHandler handler) throws DBusException;

    public abstract  void removeHandler(final Class type, final DBusSigHandler handler) throws DBusException;

    public  void addConsumer(final Class type, final DBusSigHandler handler) throws DBusException {
        SignalConsumer consumer = new SignalConsumer<>(handler::handle);
        ForwardingHandler forwarder = new ForwardingHandler<>(consumer);

        synchronized (forwarders) {
            forwarders.add(forwarder);
        }

        forwarder.startConsumer();

        addHandler(type, forwarder);
    }

    public  void removeConsumer(final Class type, final DBusSigHandler handler) throws DBusException {
        ForwardingHandler match = null;

        synchronized (forwarders) {
            for (ForwardingHandler forwarder : forwarders) {
                if (Objects.equals(forwarder.getConsumer(), handler)) {
                    match = forwarder;

                    break;
                }
            }

            if (match != null) {
                forwarders.remove(match);
            }
        }

        if (match != null) {
            @SuppressWarnings("unchecked")
            ForwardingHandler forwarder = (ForwardingHandler) match;

            removeHandler(type, forwarder);

            forwarder.stopConsumer();
        }
    }

    public void addListener(final UnitStateListener listener) throws DBusException {
        synchronized (unitStateListeners) {
            if (defaultHandler == null) {
                defaultHandler = new ForwardingHandler<>(createStateConsumer());
                defaultHandler.startConsumer();

                addHandler(PropertiesChanged.class, defaultHandler);
            }

            unitStateListeners.add(listener);
        }
    }

    public void removeListener(final UnitStateListener listener) throws DBusException {
        synchronized (unitStateListeners) {
            unitStateListeners.remove(listener);

            if (unitStateListeners.isEmpty() && defaultHandler != null) {
                removeHandler(PropertiesChanged.class, defaultHandler);

                defaultHandler.stopConsumer();
                defaultHandler = null;
            }
        }
    }

    protected SignalConsumer createStateConsumer() {
        return new SignalConsumer<>(s -> { /* Default behavior (do nothing) */ });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy