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

de.schlichtherle.truezip.fs.inst.jmx.JmxDirector Maven / Gradle / Ivy

Go to download

This module provides a file system manager and an I/O pool service for managing and monitoring TrueZIP via JMX and java.util.logging. Add the JAR artifact of this module to the run time class path to make its services available for service location in the client API modules.

There is a newer version: 7.7.10
Show newest version
/*
 * Copyright (C) 2005-2015 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package de.schlichtherle.truezip.fs.inst.jmx;

import de.schlichtherle.truezip.entry.Entry;
import de.schlichtherle.truezip.fs.FsController;
import de.schlichtherle.truezip.fs.FsManager;
import de.schlichtherle.truezip.fs.FsModel;
import de.schlichtherle.truezip.fs.inst.*;
import de.schlichtherle.truezip.socket.IOPool;
import de.schlichtherle.truezip.socket.InputSocket;
import de.schlichtherle.truezip.socket.OutputSocket;
import de.schlichtherle.truezip.util.JSE7;
import java.lang.management.ManagementFactory;
import javax.annotation.concurrent.ThreadSafe;
import javax.management.*;

/**
 * @author  Christian Schlichtherle
 */
@ThreadSafe
public class JmxDirector extends InstrumentingDirector {

    private static final String APPLICATION_IO_STATISTICS = "ApplicationIOStatistics";
    private static final String KERNEL_IO_STATISTICS = "KernelIOStatistics";
    private static final String TEMP_IO_STATISTICS = "TempIOStatistics";
    private static final MBeanServer
            mbs = ManagementFactory.getPlatformMBeanServer();
    public static final JmxDirector SINGLETON = JSE7.AVAILABLE
            ? new JmxNio2Director()
            : new JmxDirector();

    private JmxDirector() {
    }

    private volatile JmxIOStatistics application;

    JmxIOStatistics getApplicationIOStatistics() {
        final JmxIOStatistics stats = application;
        assert null != stats;
        return stats;
    }

    void setApplicationIOStatistics(final JmxIOStatistics stats) {
        assert null != stats;
        this.application = stats;
        JmxIOStatisticsView.register(stats, APPLICATION_IO_STATISTICS);
    }

    private volatile JmxIOStatistics kernel;

    JmxIOStatistics getKernelIOStatistics() {
        final JmxIOStatistics stats = kernel;
        assert null != stats;
        return stats;
    }

    void setKernelIOStatistics(final JmxIOStatistics stats) {
        assert null != stats;
        this.kernel = stats;
        JmxIOStatisticsView.register(stats, KERNEL_IO_STATISTICS);
    }

    private volatile JmxIOStatistics temp;

    JmxIOStatistics getTempIOStatistics() {
        final JmxIOStatistics stats = temp;
        assert null != stats;
        return stats;
    }

    void setTempIOStatistics(final JmxIOStatistics stats) {
        assert null != stats;
        this.temp = stats;
        JmxIOStatisticsView.register(stats, TEMP_IO_STATISTICS);
    }

    void clearStatistics() {
        for (final Object[] params : new Object[][] {
            { APPLICATION_IO_STATISTICS, application, },
            { KERNEL_IO_STATISTICS, kernel, },
            { TEMP_IO_STATISTICS, temp, },
        }) {
            final ObjectName pattern;
            try {
                pattern = new ObjectName(FsManager.class.getName() + ":type=" + params[0] + ",name=*");
            } catch (MalformedObjectNameException ex) {
                throw new AssertionError();
            }
            for (final ObjectName found : mbs.queryNames(pattern, null)) {
                final JmxIOStatisticsMXBean proxy = JMX.newMXBeanProxy(
                        mbs, found, JmxIOStatisticsMXBean.class);
                if (((JmxIOStatistics) params[1]).getTimeCreatedMillis()
                        == proxy.getTimeCreatedMillis())
                    continue;
                try {
                    mbs.unregisterMBean(found);
                } catch (InstanceNotFoundException ex) {
                    throw new AssertionError();
                } catch (MBeanRegistrationException ex) {
                    throw new AssertionError(ex);
                }
            }
        }
    }

    @Override
    public > IOPool instrument(IOPool pool) {
        return new JmxIOPool(pool, this);
    }

    @Override
    public FsManager instrument(FsManager manager) {
        return new JmxManager(manager, this);
    }

    @Override
    public FsModel instrument(FsModel model, InstrumentingCompositeDriver context) {
        return new JmxModel(model, this);
    }

    @Override
    public FsController instrument(FsController controller, InstrumentingManager context) {
        return new JmxApplicationController(controller, this);
    }

    @Override
    public FsController instrument(FsController controller, InstrumentingCompositeDriver context) {
        return new JmxKernelController(controller, this);
    }

    @Override
    public > InputSocket instrument(InputSocket input, InstrumentingIOPool.Buffer context) {
        return new JmxInputSocket(input, this, temp);
    }

    @edu.umd.cs.findbugs.annotations.SuppressWarnings("BC_UNCONFIRMED_CAST")
    @Override
    public  InputSocket instrument(InputSocket input, InstrumentingController context) {
        return new JmxInputSocket(input, this, ((JmxController) context).getIOStatistics());
    }

    @Override
    public > OutputSocket instrument(OutputSocket output, InstrumentingIOPool.Buffer context) {
        return new JmxOutputSocket(output, this, temp);
    }

    @edu.umd.cs.findbugs.annotations.SuppressWarnings("BC_UNCONFIRMED_CAST")
    @Override
    public  OutputSocket instrument(OutputSocket output, InstrumentingController context) {
        return new JmxOutputSocket(output, this, ((JmxController) context).getIOStatistics());
    }

    private static final class JmxNio2Director extends JmxDirector {
        @Override
        public > InputSocket instrument(InputSocket input, InstrumentingIOPool.Buffer context) {
            return new JmxNio2InputSocket(input, this, super.temp);
        }

        @edu.umd.cs.findbugs.annotations.SuppressWarnings("BC_UNCONFIRMED_CAST")
        @Override
        public  InputSocket instrument(InputSocket input, InstrumentingController context) {
            return new JmxNio2InputSocket(input, this, ((JmxController) context).getIOStatistics());
        }

        @Override
        public > OutputSocket instrument(OutputSocket output, InstrumentingIOPool.Buffer context) {
            return new JmxNio2OutputSocket(output, this, super.temp);
        }

        @edu.umd.cs.findbugs.annotations.SuppressWarnings("BC_UNCONFIRMED_CAST")
        @Override
        public  OutputSocket instrument(OutputSocket output, InstrumentingController context) {
            return new JmxNio2OutputSocket(output, this, ((JmxController) context).getIOStatistics());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy