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   */
41  public class DefaultMonitoringService
42      implements DataSampleProcessor<DataSample>, MonitoringService {
43  
44      public Level getLevel(String namespace) {
45          Level level = _levels.get(namespace);
46  
47          if (level == null) {
48              return Level.OFF;
49          }
50  
51          return level;
52      }
53  
54      public Set<String> getNamespaces() {
55          return _levels.keySet();
56      }
57  
58      public void processDataSample(DataSample dataSample)
59          throws MonitoringException {
60  
61          String namespace = dataSample.getNamespace();
62  
63          Level level = _levels.get(namespace);
64  
65          if ((level != null) && (level.equals(Level.OFF))) {
66              return;
67          }
68  
69          List<DataSampleProcessor<DataSample>> dataSampleProcessors =
70              _dataSampleProcessors.get(namespace);
71  
72          if ((dataSampleProcessors == null) || dataSampleProcessors.isEmpty()) {
73              return;
74          }
75  
76          for (DataSampleProcessor<DataSample> dataSampleProcessor :
77                  dataSampleProcessors) {
78  
79              dataSampleProcessor.processDataSample(dataSample);
80          }
81      }
82  
83      public void registerDataSampleProcessor(
84          String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
85  
86          List<DataSampleProcessor<DataSample>> dataSampleProcessors =
87              _dataSampleProcessors.get(namespace);
88  
89          if (dataSampleProcessors == null) {
90              dataSampleProcessors =
91                  new ArrayList<DataSampleProcessor<DataSample>>();
92  
93              _dataSampleProcessors.put(namespace, dataSampleProcessors);
94          }
95  
96          dataSampleProcessors.add(dataSampleProcessor);
97      }
98  
99      public void setDataSampleProcessors(
100         Map<String, List<DataSampleProcessor<DataSample>>>
101             dataSampleProcessors) {
102 
103         _dataSampleProcessors.putAll(dataSampleProcessors);
104     }
105 
106     public void setLevel(String namespace, Level level) {
107         _levels.put(namespace, level);
108     }
109 
110     public void setLevels(Map<String, String> levels) {
111         for (Map.Entry<String, String> entry : levels.entrySet()) {
112             String namespace = entry.getKey();
113             String levelName = entry.getValue();
114 
115             Level level = Level.valueOf(levelName);
116 
117             _levels.put(namespace, level);
118         }
119     }
120 
121     public void unregisterDataSampleProcessor(
122         String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
123 
124         List<DataSampleProcessor<DataSample>> dataSampleProcessors =
125             _dataSampleProcessors.get(namespace);
126 
127         if (dataSampleProcessors != null) {
128             dataSampleProcessors.remove(dataSampleProcessor);
129         }
130     }
131 
132     private Map<String, List<DataSampleProcessor<DataSample>>>
133         _dataSampleProcessors = new ConcurrentHashMap
134             <String, List<DataSampleProcessor<DataSample>>>();
135     private Map<String, Level> _levels = new ConcurrentHashMap<String, Level>();
136 
137 }