View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.repositories;
18  
19  import org.apache.commons.monitoring.Role;
20  import org.apache.commons.monitoring.configuration.Configuration;
21  import org.apache.commons.monitoring.counters.Counter;
22  import org.apache.commons.monitoring.gauges.DefaultGaugeManager;
23  import org.apache.commons.monitoring.gauges.Gauge;
24  import org.apache.commons.monitoring.stopwatches.CounterStopWatch;
25  import org.apache.commons.monitoring.stopwatches.StopWatch;
26  import org.apache.commons.monitoring.store.DataStore;
27  import org.apache.commons.monitoring.store.GaugeValuesRequest;
28  
29  import java.util.Iterator;
30  import java.util.Map;
31  
32  public class DefaultRepository implements Repository {
33      private final DataStore dataStore;
34      private final DefaultGaugeManager gaugeManager;
35  
36      public DefaultRepository() {
37          this.dataStore = Configuration.newInstance(DataStore.class);
38          this.gaugeManager = new DefaultGaugeManager(dataStore);
39      }
40  
41      @Configuration.Destroying
42      public void stopGaugeTimers() {
43          gaugeManager.stop();
44      }
45  
46      @Override
47      public Counter getCounter(final Counter.Key key) {
48          return dataStore.getOrCreateCounter(key);
49      }
50  
51      @Override
52      public void clear() {
53          dataStore.clearCounters();
54      }
55  
56      @Override
57      public StopWatch start(final Counter monitor) {
58          return new CounterStopWatch(monitor);
59      }
60  
61      @Override
62      public Iterator<Counter> iterator() {
63          return dataStore.getCounters().iterator();
64      }
65  
66      @Override
67      public Map<Long, Double> getGaugeValues(final long start, final long end, final Role role) {
68          return dataStore.getGaugeValues(new GaugeValuesRequest(start, end, role));
69      }
70  
71      @Override
72      public void addGauge(final Gauge gauge) {
73          gaugeManager.addGauge(gauge);
74      }
75  
76      @Override
77      public void stopGauge(final Role role) {
78          gaugeManager.stopGauge(role);
79      }
80  }