001/* 002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 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 public void enableCounters() { 038 SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.ENABLED, true); 039 } 040 041 public void disableCounters() { 042 SimonManager.getCounter(COUNTER_PREFIX).setState(SimonState.DISABLED, true); 043 } 044 045 @Override 046 public void applicationStarted(ComponentContext context) { 047 // create the root counter 048 SimonManager.getCounter(COUNTER_PREFIX); 049 // register call back for history management 050 SimonManager.callback().addCallback(history); 051 } 052 053 @Override 054 public void decreaseCounter(String counterName) { 055 if (SimonManager.getCounter(counterName).isEnabled()) { 056 SimonManager.getCounter(counterName).decrease(); 057 } 058 } 059 060 @Override 061 public void increaseCounter(String counterName) { 062 if (SimonManager.getCounter(counterName).isEnabled()) { 063 SimonManager.getCounter(counterName).increase(); 064 } 065 } 066 067 @Override 068 public void decreaseCounter(String counterName, long value) { 069 if (SimonManager.getCounter(counterName).isEnabled()) { 070 SimonManager.getCounter(counterName).decrease(value); 071 } 072 } 073 074 @Override 075 public void increaseCounter(String counterName, long value) { 076 if (SimonManager.getCounter(counterName).isEnabled()) { 077 SimonManager.getCounter(counterName).increase(value); 078 } 079 } 080 081 @Override 082 public void setCounterValue(String counterName, long value) { 083 if (SimonManager.getCounter(counterName).isEnabled()) { 084 SimonManager.getCounter(counterName).set(value); 085 } 086 } 087 088 public CounterHistoryStack getCounterHistory(String counterName) { 089 090 CounterHistoryStack stack = history.getCounterHistory(counterName); 091 if (stack == null) { 092 return new CounterHistoryStack(50); 093 } else { 094 return stack; 095 } 096 } 097 098}