001    /**
002     * Copyright (c) 2000-2013 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    import com.liferay.portal.kernel.util.ListUtil;
019    
020    import java.util.List;
021    import java.util.Queue;
022    import java.util.concurrent.ConcurrentLinkedQueue;
023    
024    /**
025     * @author Michael C. Han
026     * @author Brian Wing Shun Chan
027     */
028    public class DataSampleThreadLocal {
029    
030            public static void addDataSample(DataSample dataSample) {
031                    DataSampleThreadLocal dataSampleThreadLocal =
032                            _dataSampleThreadLocal.get();
033    
034                    dataSampleThreadLocal._addDataSample(dataSample);
035            }
036    
037            public static void clearDataSamples() {
038                    _dataSampleThreadLocal.remove();
039            }
040    
041            public static List<DataSample> getDataSamples() {
042                    DataSampleThreadLocal dataSampleThreadLocal =
043                            _dataSampleThreadLocal.get();
044    
045                    return ListUtil.fromCollection(dataSampleThreadLocal._getDataSamples());
046            }
047    
048            public static void initialize() {
049                    _dataSampleThreadLocal.get();
050            }
051    
052            @Override
053            public Object clone() {
054                    return new DataSampleThreadLocal();
055            }
056    
057            public long getMonitorTime() {
058                    return _monitorTime;
059            }
060    
061            private DataSampleThreadLocal() {
062                    _monitorTime = System.currentTimeMillis();
063            }
064    
065            private void _addDataSample(DataSample dataSample) {
066                    _dataSamples.add(dataSample);
067            }
068    
069            private Queue<DataSample> _getDataSamples() {
070                    return _dataSamples;
071            }
072    
073            private static ThreadLocal<DataSampleThreadLocal> _dataSampleThreadLocal =
074                    new AutoResetThreadLocal<DataSampleThreadLocal>(
075                            DataSampleThreadLocal.class + "._dataSampleThreadLocal") {
076    
077                                    @Override
078                                    protected DataSampleThreadLocal copy(
079                                            DataSampleThreadLocal dataSampleThreadLocal) {
080    
081                                            return dataSampleThreadLocal;
082                                    }
083    
084                                    @Override
085                                    protected DataSampleThreadLocal initialValue() {
086                                            return new DataSampleThreadLocal();
087                                    }
088    
089                            };
090    
091            private Queue<DataSample> _dataSamples =
092                    new ConcurrentLinkedQueue<DataSample>();
093            private long _monitorTime;
094    
095    }