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 * @deprecated since 11.4: use dropwizard metrics counter instead
031 */
032@Deprecated(since = "11.4")
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    @Override
040    public void enableCounters() {
041        SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.ENABLED, true);
042    }
043
044    @Override
045    public void disableCounters() {
046        SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.DISABLED, true);
047    }
048
049    @Override
050    public void start(ComponentContext context) {
051        // create the root counter
052        SimonManager.getCounter(COUNTER_PREFIX);
053        // register call back for history management
054        SimonManager.callback().addCallback(history);
055    }
056
057    @Override
058    public void decreaseCounter(String counterName) {
059        if (SimonManager.getCounter(counterName).isEnabled()) {
060            SimonManager.getCounter(counterName).decrease();
061        }
062    }
063
064    @Override
065    public void increaseCounter(String counterName) {
066        if (SimonManager.getCounter(counterName).isEnabled()) {
067            SimonManager.getCounter(counterName).increase();
068        }
069    }
070
071    @Override
072    public void decreaseCounter(String counterName, long value) {
073        if (SimonManager.getCounter(counterName).isEnabled()) {
074            SimonManager.getCounter(counterName).decrease(value);
075        }
076    }
077
078    @Override
079    public void increaseCounter(String counterName, long value) {
080        if (SimonManager.getCounter(counterName).isEnabled()) {
081            SimonManager.getCounter(counterName).increase(value);
082        }
083    }
084
085    @Override
086    public void setCounterValue(String counterName, long value) {
087        if (SimonManager.getCounter(counterName).isEnabled()) {
088            SimonManager.getCounter(counterName).set(value);
089        }
090    }
091
092    @Override
093    public CounterHistoryStack getCounterHistory(String counterName) {
094
095        CounterHistoryStack stack = history.getCounterHistory(counterName);
096        if (stack == null) {
097            return new CounterHistoryStack(50);
098        } else {
099            return stack;
100        }
101    }
102
103}