001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.runtime.management.metrics;
021
022import java.io.IOException;
023import java.util.logging.Level;
024import java.util.logging.Logger;
025
026import org.javasimon.Simon;
027import org.javasimon.SimonManager;
028import org.javasimon.jmx.JmxRegisterCallback;
029import org.javasimon.utils.LoggingCallback;
030
031public class MetricEnabler implements MetricEnablerMXBean {;
032
033    protected MetricSerializer serializer;
034
035    protected LoggingCallback lgCB;
036
037    protected final JmxRegisterCallback jmxCB = new JmxRegisterCallback();
038
039    protected void setSerializer(MetricSerializer serializer) {
040        this.serializer = serializer;
041    }
042
043    @Override
044    public void enable() {
045        SimonManager.enable();
046        SimonManager.callback().addCallback(jmxCB);
047        for (String name : SimonManager.simonNames()) {
048            Simon simon = SimonManager.getSimon(name);
049            jmxCB.simonCreated(simon);
050        }
051    }
052
053    @Override
054    public void disable() {
055        SimonManager.callback().removeCallback(jmxCB);
056        for (String name : SimonManager.simonNames()) {
057            Simon simon = SimonManager.getSimon(name);
058            jmxCB.simonDestroyed(simon);
059        }
060        SimonManager.disable();
061    }
062
063    @Override
064    public boolean isEnabled() {
065        return SimonManager.isEnabled();
066    }
067
068    @Override
069    public void enableLogging() {
070        lgCB = new LoggingCallback();
071        lgCB.setLogger(Logger.getLogger("org.javasimon"));
072        lgCB.setLevel(Level.FINEST);
073        SimonManager.callback().addCallback(lgCB);
074    }
075
076    @Override
077    public void disableLogging() {
078        SimonManager.callback().removeCallback(lgCB);
079        lgCB = null;
080    }
081
082    @Override
083    public boolean isLogging() {
084        return lgCB != null;
085    }
086
087    protected MetricSerializingCallback srzCB;
088
089    @Override
090    public void enableSerializing() throws IOException {
091        serializer.resetOutput();
092        srzCB = new MetricSerializingCallback(serializer);
093        SimonManager.callback().addCallback(srzCB);
094    }
095
096    @Override
097    public void disableSerializing() throws IOException {
098        serializer.closeOutput();
099        SimonManager.callback().removeCallback(srzCB);
100        srzCB = null;
101    }
102
103    @Override
104    public boolean isSerializing() {
105        return srzCB != null;
106    }
107
108}