001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.monitoring.repositories; 018 019 import org.apache.commons.monitoring.Role; 020 import org.apache.commons.monitoring.configuration.Configuration; 021 import org.apache.commons.monitoring.counters.Counter; 022 import org.apache.commons.monitoring.gauges.DefaultGaugeManager; 023 import org.apache.commons.monitoring.gauges.Gauge; 024 import org.apache.commons.monitoring.stopwatches.CounterStopWatch; 025 import org.apache.commons.monitoring.stopwatches.StopWatch; 026 import org.apache.commons.monitoring.store.DataStore; 027 import org.apache.commons.monitoring.store.GaugeValuesRequest; 028 029 import java.util.Iterator; 030 import java.util.Map; 031 032 public class DefaultRepository implements Repository { 033 private final DataStore dataStore; 034 private final DefaultGaugeManager gaugeManager; 035 036 public DefaultRepository() { 037 this.dataStore = Configuration.newInstance(DataStore.class); 038 this.gaugeManager = new DefaultGaugeManager(dataStore); 039 } 040 041 @Configuration.Destroying 042 public void stopGaugeTimers() { 043 gaugeManager.stop(); 044 } 045 046 @Override 047 public Counter getCounter(final Counter.Key key) { 048 return dataStore.getOrCreateCounter(key); 049 } 050 051 @Override 052 public void clear() { 053 dataStore.clearCounters(); 054 } 055 056 @Override 057 public StopWatch start(final Counter monitor) { 058 return new CounterStopWatch(monitor); 059 } 060 061 @Override 062 public Iterator<Counter> iterator() { 063 return dataStore.getCounters().iterator(); 064 } 065 066 @Override 067 public Map<Long, Double> getGaugeValues(final long start, final long end, final Role role) { 068 return dataStore.getGaugeValues(new GaugeValuesRequest(start, end, role)); 069 } 070 071 @Override 072 public void addGauge(final Gauge gauge) { 073 gaugeManager.addGauge(gauge); 074 } 075 076 @Override 077 public void stopGauge(final Role role) { 078 gaugeManager.stopGauge(role); 079 } 080 }