001/*
002 * (C) Copyright 2006-2017 Nuxeo (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 */
019package org.nuxeo.runtime.management.counters;
020
021import org.javasimon.SimonManager;
022import org.javasimon.SimonState;
023import org.nuxeo.runtime.model.ComponentContext;
024import org.nuxeo.runtime.model.DefaultComponent;
025
026/**
027 * Runtime component that provides the {@link CounterManager} service. Uses Simon Counters for implementation
028 *
029 * @author Tiry (tdelprat@nuxeo.com)
030 */
031public class CounterManagerImpl extends DefaultComponent implements CounterManager {
032
033    public static final String COUNTER_PREFIX = "org.nuxeo";
034
035    protected CounterHistoryRecorder history = new CounterHistoryRecorder(50);
036
037    @Override
038    public void enableCounters() {
039        SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.ENABLED, true);
040    }
041
042    @Override
043    public void disableCounters() {
044        SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.DISABLED, true);
045    }
046
047    @Override
048    public void start(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    @Override
091    public CounterHistoryStack getCounterHistory(String counterName) {
092
093        CounterHistoryStack stack = history.getCounterHistory(counterName);
094        if (stack == null) {
095            return new CounterHistoryStack(50);
096        } else {
097            return stack;
098        }
099    }
100
101}