001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.monitoring.statistics;
016    
017    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.List;
022    
023    /**
024     * @author Michael C. Han
025     * @author Brian Wing Shun Chan
026     */
027    public class DataSampleThreadLocal implements Cloneable {
028    
029            public static void addDataSample(DataSample dataSample) {
030                    if (!_monitoringDataSampleThreadLocal) {
031                            return;
032                    }
033    
034                    _dataSampleThreadLocal.get()._addDataSample(dataSample);
035            }
036    
037            public static void clearDataSamples() {
038                    _dataSampleThreadLocal.remove();
039            }
040    
041            public static List<DataSample> getDataSamples() {
042                    if (!_monitoringDataSampleThreadLocal) {
043                            return Collections.emptyList();
044                    }
045    
046                    return _dataSampleThreadLocal.get()._getDataSamples();
047            }
048    
049            public static boolean isMonitoringDataSampleThreadLocal() {
050                    return _monitoringDataSampleThreadLocal;
051            }
052    
053            public static void setMonitoringDataSampleThreadLocal(
054                    boolean monitoringDataSampleThreadLocal) {
055    
056                    _monitoringDataSampleThreadLocal = monitoringDataSampleThreadLocal;
057            }
058    
059            @Override
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    
086            private List<DataSample> _dataSamples = new ArrayList<DataSample>();
087            private long _monitorTime;
088    
089    }