001/*
002 * (C) Copyright 2006-2009 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 * $Id$
020 */
021package org.nuxeo.runtime.management.counters;
022
023import org.javasimon.SimonManager;
024import org.javasimon.SimonState;
025import org.nuxeo.runtime.model.ComponentContext;
026import org.nuxeo.runtime.model.DefaultComponent;
027
028/**
029 * Runtime component that provides the {@link CounterManager} service. Uses Simon Counters for implementation
030 *
031 * @author Tiry (tdelprat@nuxeo.com)
032 */
033public class CounterManagerImpl extends DefaultComponent implements CounterManager {
034
035    public static final String COUNTER_PREFIX = "org.nuxeo";
036
037    protected CounterHistoryRecorder history = new CounterHistoryRecorder(50);
038
039    public void enableCounters() {
040        SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.ENABLED, true);
041    }
042
043    public void disableCounters() {
044        SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.DISABLED, true);
045    }
046
047    @Override
048    public void applicationStarted(ComponentContext context) {
049        // create the root counter
050        SimonManager.getCounter(COUNTER_PREFIX);
051        // register call back for history management
052        SimonManager.callback().addCallback(history);
053    }
054
055    @Override
056    public void decreaseCounter(String counterName) {
057        if (SimonManager.getCounter(counterName).isEnabled()) {
058            SimonManager.getCounter(counterName).decrease();
059        }
060    }
061
062    @Override
063    public void increaseCounter(String counterName) {
064        if (SimonManager.getCounter(counterName).isEnabled()) {
065            SimonManager.getCounter(counterName).increase();
066        }
067    }
068
069    @Override
070    public void decreaseCounter(String counterName, long value) {
071        if (SimonManager.getCounter(counterName).isEnabled()) {
072            SimonManager.getCounter(counterName).decrease(value);
073        }
074    }
075
076    @Override
077    public void increaseCounter(String counterName, long value) {
078        if (SimonManager.getCounter(counterName).isEnabled()) {
079            SimonManager.getCounter(counterName).increase(value);
080        }
081    }
082
083    @Override
084    public void setCounterValue(String counterName, long value) {
085        if (SimonManager.getCounter(counterName).isEnabled()) {
086            SimonManager.getCounter(counterName).set(value);
087        }
088    }
089
090    public CounterHistoryStack getCounterHistory(String counterName) {
091
092        CounterHistoryStack stack = history.getCounterHistory(counterName);
093        if (stack == null) {
094            return new CounterHistoryStack(50);
095        } else {
096            return stack;
097        }
098    }
099
100}