001    /**
002     * Copyright (c) 2000-2011 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.statistics;
016    
017    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018    import com.liferay.portal.util.PropsValues;
019    
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.List;
023    
024    /**
025     * @author Michael C. Han
026     * @author Brian Wing Shun Chan
027     */
028    public class DataSampleThreadLocal implements Cloneable {
029    
030            public static void addDataSample(DataSample dataSample) {
031                    if (!_monitoringDataSampleThreadLocal) {
032                            return;
033                    }
034    
035                    _dataSampleThreadLocal.get()._addDataSample(dataSample);
036            }
037    
038            public static void clearDataSamples() {
039                    _dataSampleThreadLocal.remove();
040            }
041    
042            public static List<DataSample> getDataSamples() {
043                    if (!_monitoringDataSampleThreadLocal) {
044                            return Collections.EMPTY_LIST;
045                    }
046    
047                    return _dataSampleThreadLocal.get()._getDataSamples();
048            }
049    
050            public static boolean isMonitoringDataSampleThreadLocal() {
051                    return _monitoringDataSampleThreadLocal;
052            }
053    
054            public static void setMonitoringDataSampleThreadLocal(
055                    boolean monitoringDataSampleThreadLocal) {
056    
057                    _monitoringDataSampleThreadLocal = monitoringDataSampleThreadLocal;
058            }
059    
060            public Object clone() {
061                    return new DataSampleThreadLocal();
062            }
063    
064            public long getMonitorTime() {
065                    return _monitorTime;
066            }
067    
068            private DataSampleThreadLocal() {
069                    _monitorTime = System.currentTimeMillis();
070            }
071    
072            private void _addDataSample(DataSample dataSample) {
073                    _dataSamples.add(dataSample);
074            }
075    
076            private List<DataSample> _getDataSamples() {
077                    return _dataSamples;
078            }
079    
080            private static ThreadLocal<DataSampleThreadLocal> _dataSampleThreadLocal =
081                    new AutoResetThreadLocal<DataSampleThreadLocal>(
082                            DataSampleThreadLocal.class + "._dataSampleThreadLocal",
083                            new DataSampleThreadLocal());
084            private static boolean _monitoringDataSampleThreadLocal =
085                    PropsValues.MONITORING_DATA_SAMPLE_THREAD_LOCAL;
086    
087            private List<DataSample> _dataSamples = new ArrayList<DataSample>();
088            private long _monitorTime;
089    
090    }