001    /**
002     * Copyright (c) 2000-2013 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.kernel.monitoring;
016    
017    import com.liferay.portal.kernel.monitoring.statistics.DataSample;
018    import com.liferay.portal.kernel.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 DefaultMonitoringProcessor
031            implements DataSampleProcessor<DataSample>, MonitoringProcessor {
032    
033            @Override
034            public Level getLevel(String namespace) {
035                    Level level = _levels.get(namespace);
036    
037                    if (level == null) {
038                            return Level.OFF;
039                    }
040    
041                    return level;
042            }
043    
044            @Override
045            public Set<String> getNamespaces() {
046                    return _levels.keySet();
047            }
048    
049            @Override
050            public void processDataSample(DataSample dataSample)
051                    throws MonitoringException {
052    
053                    String namespace = dataSample.getNamespace();
054    
055                    Level level = _levels.get(namespace);
056    
057                    if ((level != null) && level.equals(Level.OFF)) {
058                            return;
059                    }
060    
061                    List<DataSampleProcessor<DataSample>> dataSampleProcessors =
062                            _dataSampleProcessors.get(namespace);
063    
064                    if ((dataSampleProcessors == null) || dataSampleProcessors.isEmpty()) {
065                            return;
066                    }
067    
068                    for (DataSampleProcessor<DataSample> dataSampleProcessor :
069                                    dataSampleProcessors) {
070    
071                            dataSampleProcessor.processDataSample(dataSample);
072                    }
073            }
074    
075            public void registerDataSampleProcessor(
076                    String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
077    
078                    List<DataSampleProcessor<DataSample>> dataSampleProcessors =
079                            _dataSampleProcessors.get(namespace);
080    
081                    if (dataSampleProcessors == null) {
082                            dataSampleProcessors =
083                                    new ArrayList<DataSampleProcessor<DataSample>>();
084    
085                            _dataSampleProcessors.put(namespace, dataSampleProcessors);
086                    }
087    
088                    dataSampleProcessors.add(dataSampleProcessor);
089            }
090    
091            public void setDataSampleProcessors(
092                    Map<String, List<DataSampleProcessor<DataSample>>>
093                            dataSampleProcessors) {
094    
095                    _dataSampleProcessors.putAll(dataSampleProcessors);
096            }
097    
098            @Override
099            public void setLevel(String namespace, Level level) {
100                    _levels.put(namespace, level);
101            }
102    
103            public void setLevels(Map<String, String> levels) {
104                    for (Map.Entry<String, String> entry : levels.entrySet()) {
105                            String namespace = entry.getKey();
106                            String levelName = entry.getValue();
107    
108                            Level level = Level.valueOf(levelName);
109    
110                            _levels.put(namespace, level);
111                    }
112            }
113    
114            public void unregisterDataSampleProcessor(
115                    String namespace, DataSampleProcessor<DataSample> dataSampleProcessor) {
116    
117                    List<DataSampleProcessor<DataSample>> dataSampleProcessors =
118                            _dataSampleProcessors.get(namespace);
119    
120                    if (dataSampleProcessors != null) {
121                            dataSampleProcessors.remove(dataSampleProcessor);
122                    }
123            }
124    
125            private Map<String, List<DataSampleProcessor<DataSample>>>
126                    _dataSampleProcessors = new ConcurrentHashMap
127                            <String, List<DataSampleProcessor<DataSample>>>();
128            private Map<String, Level> _levels = new ConcurrentHashMap<String, Level>();
129    
130    }