1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.monitoring;
24  
25  import com.liferay.portal.monitoring.statistics.DataSample;
26  import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  import java.util.concurrent.ConcurrentHashMap;
33  
34  /**
35   * <a href="DefaultMonitoringService.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Michael C. Han
38   * @author Brian Wing Shun Chan
39   */
40  public class DefaultMonitoringService
41      implements DataSampleProcessor<DataSample>, MonitoringService {
42  
43      public Level getLevel(String namespace) {
44          Level level = _levels.get(namespace);
45  
46          if (level == null) {
47              return Level.OFF;
48          }
49  
50          return level;
51      }
52  
53      public Set<String> getNamespaces() {
54          return _levels.keySet();
55      }
56  
57      public void processDataSample(DataSample dataSample)
58          throws MonitoringException {
59  
60          String namespace = dataSample.getNamespace();
61  
62          Level level = _levels.get(namespace);
63  
64          if ((level != null) && (level.equals(Level.OFF))) {
65              return;
66          }
67  
68          List<DataSampleProcessor<DataSample>> dataSampleProcessors =
69              _dataSampleProcessors.get(namespace);
70  
71          if ((dataSampleProcessors == null) || dataSampleProcessors.isEmpty()) {
72              return;
73          }
74  
75          for (DataSampleProcessor<DataSample> dataSampleProcessor :
76                  dataSampleProcessors) {
77  
78              dataSampleProcessor.processDataSample(dataSample);
79          }
80      }
81  
82      public void registerDataSampleProcessor(
83          String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
84  
85          List<DataSampleProcessor<DataSample>> dataSampleProcessors =
86              _dataSampleProcessors.get(namespace);
87  
88          if (dataSampleProcessors == null) {
89              dataSampleProcessors =
90                  new ArrayList<DataSampleProcessor<DataSample>>();
91  
92              _dataSampleProcessors.put(namespace, dataSampleProcessors);
93          }
94  
95          dataSampleProcessors.add(dataSampleProcessor);
96      }
97  
98      public void setDataSampleProcessors(
99          Map<String, List<DataSampleProcessor<DataSample>>>
100             dataSampleProcessors) {
101 
102         _dataSampleProcessors.putAll(dataSampleProcessors);
103     }
104 
105     public void setLevel(String namespace, Level level) {
106         _levels.put(namespace, level);
107     }
108 
109     public void setLevels(Map<String, String> levels) {
110         for (Map.Entry<String, String> entry : levels.entrySet()) {
111             String namespace = entry.getKey();
112             String levelName = entry.getValue();
113 
114             Level level = Level.valueOf(levelName);
115 
116             _levels.put(namespace, level);
117         }
118     }
119 
120     public void unregisterDataSampleProcessor(
121         String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
122 
123         List<DataSampleProcessor<DataSample>> dataSampleProcessors =
124             _dataSampleProcessors.get(namespace);
125 
126         if (dataSampleProcessors != null) {
127             dataSampleProcessors.remove(dataSampleProcessor);
128         }
129     }
130 
131     private Map<String, List<DataSampleProcessor<DataSample>>>
132         _dataSampleProcessors = new ConcurrentHashMap
133             <String, List<DataSampleProcessor<DataSample>>>();
134     private Map<String, Level> _levels = new ConcurrentHashMap<String, Level>();
135 
136 }