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
031// @deprecated since 11.4: use dropwizard metrics instead
032@Deprecated(since = "11.4")
033public class MetricEnabler implements MetricEnablerMXBean {
034
035    protected MetricSerializer serializer;
036
037    protected LoggingCallback lgCB;
038
039    protected final JmxRegisterCallback jmxCB = new JmxRegisterCallback();
040
041    protected void setSerializer(MetricSerializer serializer) {
042        this.serializer = serializer;
043    }
044
045    @Override
046    public void enable() {
047        SimonManager.enable();
048        SimonManager.callback().addCallback(jmxCB);
049        for (String name : SimonManager.simonNames()) {
050            Simon simon = SimonManager.getSimon(name);
051            jmxCB.simonCreated(simon);
052        }
053    }
054
055    @Override
056    public void disable() {
057        SimonManager.callback().removeCallback(jmxCB);
058        for (String name : SimonManager.simonNames()) {
059            Simon simon = SimonManager.getSimon(name);
060            jmxCB.simonDestroyed(simon);
061        }
062        SimonManager.disable();
063    }
064
065    @Override
066    public boolean isEnabled() {
067        return SimonManager.isEnabled();
068    }
069
070    @Override
071    public void enableLogging() {
072        lgCB = new LoggingCallback();
073        lgCB.setLogger(Logger.getLogger("org.javasimon"));
074        lgCB.setLevel(Level.FINEST);
075        SimonManager.callback().addCallback(lgCB);
076    }
077
078    @Override
079    public void disableLogging() {
080        SimonManager.callback().removeCallback(lgCB);
081        lgCB = null;
082    }
083
084    @Override
085    public boolean isLogging() {
086        return lgCB != null;
087    }
088
089    protected MetricSerializingCallback srzCB;
090
091    @Override
092    public void enableSerializing() throws IOException {
093        serializer.resetOutput();
094        srzCB = new MetricSerializingCallback(serializer);
095        SimonManager.callback().addCallback(srzCB);
096    }
097
098    @Override
099    public void disableSerializing() throws IOException {
100        serializer.closeOutput();
101        SimonManager.callback().removeCallback(srzCB);
102        srzCB = null;
103    }
104
105    @Override
106    public boolean isSerializing() {
107        return srzCB != null;
108    }
109
110}