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.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    }