001    /**
002     * Copyright (c) 2000-present 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.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            public long getMonitorTime() {
053                    return _monitorTime;
054            }
055    
056            private DataSampleThreadLocal() {
057                    _monitorTime = System.currentTimeMillis();
058            }
059    
060            private void _addDataSample(DataSample dataSample) {
061                    _dataSamples.add(dataSample);
062            }
063    
064            private Queue<DataSample> _getDataSamples() {
065                    return _dataSamples;
066            }
067    
068            private static final ThreadLocal<DataSampleThreadLocal>
069                    _dataSampleThreadLocal =
070                            new AutoResetThreadLocal<DataSampleThreadLocal>(
071                                    DataSampleThreadLocal.class + "._dataSampleThreadLocal") {
072    
073                                    @Override
074                                    protected DataSampleThreadLocal copy(
075                                            DataSampleThreadLocal dataSampleThreadLocal) {
076    
077                                            return dataSampleThreadLocal;
078                                    }
079    
080                                    @Override
081                                    protected DataSampleThreadLocal initialValue() {
082                                            return new DataSampleThreadLocal();
083                                    }
084    
085                            };
086    
087            private final Queue<DataSample> _dataSamples =
088                    new ConcurrentLinkedQueue<>();
089            private final long _monitorTime;
090    
091    }