1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.monitoring;
16  
17  import com.liferay.portal.monitoring.statistics.DataSample;
18  import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  /**
27   * <a href="DefaultMonitoringProcessor.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Michael C. Han
30   * @author Brian Wing Shun Chan
31   */
32  public class DefaultMonitoringProcessor
33      implements DataSampleProcessor<DataSample>, MonitoringProcessor {
34  
35      public Level getLevel(String namespace) {
36          Level level = _levels.get(namespace);
37  
38          if (level == null) {
39              return Level.OFF;
40          }
41  
42          return level;
43      }
44  
45      public Set<String> getNamespaces() {
46          return _levels.keySet();
47      }
48  
49      public void processDataSample(DataSample dataSample)
50          throws MonitoringException {
51  
52          String namespace = dataSample.getNamespace();
53  
54          Level level = _levels.get(namespace);
55  
56          if ((level != null) && (level.equals(Level.OFF))) {
57              return;
58          }
59  
60          List<DataSampleProcessor<DataSample>> dataSampleProcessors =
61              _dataSampleProcessors.get(namespace);
62  
63          if ((dataSampleProcessors == null) || dataSampleProcessors.isEmpty()) {
64              return;
65          }
66  
67          for (DataSampleProcessor<DataSample> dataSampleProcessor :
68                  dataSampleProcessors) {
69  
70              dataSampleProcessor.processDataSample(dataSample);
71          }
72      }
73  
74      public void registerDataSampleProcessor(
75          String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
76  
77          List<DataSampleProcessor<DataSample>> dataSampleProcessors =
78              _dataSampleProcessors.get(namespace);
79  
80          if (dataSampleProcessors == null) {
81              dataSampleProcessors =
82                  new ArrayList<DataSampleProcessor<DataSample>>();
83  
84              _dataSampleProcessors.put(namespace, dataSampleProcessors);
85          }
86  
87          dataSampleProcessors.add(dataSampleProcessor);
88      }
89  
90      public void setDataSampleProcessors(
91          Map<String, List<DataSampleProcessor<DataSample>>>
92              dataSampleProcessors) {
93  
94          _dataSampleProcessors.putAll(dataSampleProcessors);
95      }
96  
97      public void setLevel(String namespace, Level level) {
98          _levels.put(namespace, level);
99      }
100 
101     public void setLevels(Map<String, String> levels) {
102         for (Map.Entry<String, String> entry : levels.entrySet()) {
103             String namespace = entry.getKey();
104             String levelName = entry.getValue();
105 
106             Level level = Level.valueOf(levelName);
107 
108             _levels.put(namespace, level);
109         }
110     }
111 
112     public void unregisterDataSampleProcessor(
113         String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
114 
115         List<DataSampleProcessor<DataSample>> dataSampleProcessors =
116             _dataSampleProcessors.get(namespace);
117 
118         if (dataSampleProcessors != null) {
119             dataSampleProcessors.remove(dataSampleProcessor);
120         }
121     }
122 
123     private Map<String, List<DataSampleProcessor<DataSample>>>
124         _dataSampleProcessors = new ConcurrentHashMap
125             <String, List<DataSampleProcessor<DataSample>>>();
126     private Map<String, Level> _levels = new ConcurrentHashMap<String, Level>();
127 
128 }