001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.monitoring;
016    
017    import com.liferay.portal.monitoring.statistics.DataSample;
018    import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.Set;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Michael C. Han
028     * @author Brian Wing Shun Chan
029     */
030    public class DefaultMonitoringService
031            implements DataSampleProcessor<DataSample>, MonitoringService {
032    
033            public Level getLevel(String namespace) {
034                    Level level = _levels.get(namespace);
035    
036                    if (level == null) {
037                            return Level.OFF;
038                    }
039    
040                    return level;
041            }
042    
043            public Set<String> getNamespaces() {
044                    return _levels.keySet();
045            }
046    
047            public void processDataSample(DataSample dataSample)
048                    throws MonitoringException {
049    
050                    String namespace = dataSample.getNamespace();
051    
052                    Level level = _levels.get(namespace);
053    
054                    if ((level != null) && (level.equals(Level.OFF))) {
055                            return;
056                    }
057    
058                    List<DataSampleProcessor<DataSample>> dataSampleProcessors =
059                            _dataSampleProcessors.get(namespace);
060    
061                    if ((dataSampleProcessors == null) || dataSampleProcessors.isEmpty()) {
062                            return;
063                    }
064    
065                    for (DataSampleProcessor<DataSample> dataSampleProcessor :
066                                    dataSampleProcessors) {
067    
068                            dataSampleProcessor.processDataSample(dataSample);
069                    }
070            }
071    
072            public void registerDataSampleProcessor(
073                    String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
074    
075                    List<DataSampleProcessor<DataSample>> dataSampleProcessors =
076                            _dataSampleProcessors.get(namespace);
077    
078                    if (dataSampleProcessors == null) {
079                            dataSampleProcessors =
080                                    new ArrayList<DataSampleProcessor<DataSample>>();
081    
082                            _dataSampleProcessors.put(namespace, dataSampleProcessors);
083                    }
084    
085                    dataSampleProcessors.add(dataSampleProcessor);
086            }
087    
088            public void setDataSampleProcessors(
089                    Map<String, List<DataSampleProcessor<DataSample>>>
090                            dataSampleProcessors) {
091    
092                    _dataSampleProcessors.putAll(dataSampleProcessors);
093            }
094    
095            public void setLevel(String namespace, Level level) {
096                    _levels.put(namespace, level);
097            }
098    
099            public void setLevels(Map<String, String> levels) {
100                    for (Map.Entry<String, String> entry : levels.entrySet()) {
101                            String namespace = entry.getKey();
102                            String levelName = entry.getValue();
103    
104                            Level level = Level.valueOf(levelName);
105    
106                            _levels.put(namespace, level);
107                    }
108            }
109    
110            public void unregisterDataSampleProcessor(
111                    String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
112    
113                    List<DataSampleProcessor<DataSample>> dataSampleProcessors =
114                            _dataSampleProcessors.get(namespace);
115    
116                    if (dataSampleProcessors != null) {
117                            dataSampleProcessors.remove(dataSampleProcessor);
118                    }
119            }
120    
121            private Map<String, List<DataSampleProcessor<DataSample>>>
122                    _dataSampleProcessors = new ConcurrentHashMap
123                            <String, List<DataSampleProcessor<DataSample>>>();
124            private Map<String, Level> _levels = new ConcurrentHashMap<String, Level>();
125    
126    }