001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.runtime.management.metrics;
019
020import java.io.IOException;
021import java.util.logging.Level;
022import java.util.logging.Logger;
023
024import org.javasimon.Simon;
025import org.javasimon.SimonManager;
026import org.javasimon.jmx.JmxRegisterCallback;
027import org.javasimon.utils.LoggingCallback;
028
029public class MetricEnabler implements MetricEnablerMXBean {;
030
031    protected MetricSerializer serializer;
032
033    protected LoggingCallback lgCB;
034
035    protected final JmxRegisterCallback jmxCB = new JmxRegisterCallback();
036
037    protected void setSerializer(MetricSerializer serializer) {
038        this.serializer = serializer;
039    }
040
041    @Override
042    public void enable() {
043        SimonManager.enable();
044        SimonManager.callback().addCallback(jmxCB);
045        for (String name : SimonManager.simonNames()) {
046            Simon simon = SimonManager.getSimon(name);
047            jmxCB.simonCreated(simon);
048        }
049    }
050
051    @Override
052    public void disable() {
053        SimonManager.callback().removeCallback(jmxCB);
054        for (String name : SimonManager.simonNames()) {
055            Simon simon = SimonManager.getSimon(name);
056            jmxCB.simonDestroyed(simon);
057        }
058        SimonManager.disable();
059    }
060
061    @Override
062    public boolean isEnabled() {
063        return SimonManager.isEnabled();
064    }
065
066    @Override
067    public void enableLogging() {
068        lgCB = new LoggingCallback();
069        lgCB.setLogger(Logger.getLogger("org.javasimon"));
070        lgCB.setLevel(Level.FINEST);
071        SimonManager.callback().addCallback(lgCB);
072    }
073
074    @Override
075    public void disableLogging() {
076        SimonManager.callback().removeCallback(lgCB);
077        lgCB = null;
078    }
079
080    @Override
081    public boolean isLogging() {
082        return lgCB != null;
083    }
084
085    protected MetricSerializingCallback srzCB;
086
087    @Override
088    public void enableSerializing() throws IOException {
089        serializer.resetOutput();
090        srzCB = new MetricSerializingCallback(serializer);
091        SimonManager.callback().addCallback(srzCB);
092    }
093
094    @Override
095    public void disableSerializing() throws IOException {
096        serializer.closeOutput();
097        SimonManager.callback().removeCallback(srzCB);
098        srzCB = null;
099    }
100
101    @Override
102    public boolean isSerializing() {
103        return srzCB != null;
104    }
105
106}