1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.monitoring.statistics;
24  
25  import com.liferay.portal.kernel.util.InitialThreadLocal;
26  import com.liferay.portal.util.PropsValues;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  
32  /**
33   * <a href="DataSampleThreadLocal.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Michael C. Han
36   * @author Brian Wing Shun Chan
37   */
38  public class DataSampleThreadLocal implements Cloneable {
39  
40      public static void addDataSample(DataSample dataSample) {
41          if (!_monitoringDataSampleThreadLocal) {
42              return;
43          }
44  
45          _dataSampleThreadLocal.get()._addDataSample(dataSample);
46      }
47  
48      public static void clearDataSamples() {
49          _dataSampleThreadLocal.remove();
50      }
51  
52      public static List<DataSample> getDataSamples() {
53          if (!_monitoringDataSampleThreadLocal) {
54              return Collections.EMPTY_LIST;
55          }
56  
57          return _dataSampleThreadLocal.get()._getDataSamples();
58      }
59  
60      public static boolean isMonitoringDataSampleThreadLocal() {
61          return _monitoringDataSampleThreadLocal;
62      }
63  
64      public static void setMonitoringDataSampleThreadLocal(
65          boolean monitoringDataSampleThreadLocal) {
66  
67          _monitoringDataSampleThreadLocal = monitoringDataSampleThreadLocal;
68      }
69  
70      public Object clone() {
71          return new DataSampleThreadLocal();
72      }
73  
74      public long getMonitorTime() {
75          return _monitorTime;
76      }
77  
78      private DataSampleThreadLocal() {
79          _monitorTime = System.currentTimeMillis();
80      }
81  
82      private void _addDataSample(DataSample dataSample) {
83          _dataSamples.add(dataSample);
84      }
85  
86      private List<DataSample> _getDataSamples() {
87          return _dataSamples;
88      }
89  
90      private static ThreadLocal<DataSampleThreadLocal> _dataSampleThreadLocal =
91          new InitialThreadLocal<DataSampleThreadLocal>(
92              new DataSampleThreadLocal());
93      private static boolean _monitoringDataSampleThreadLocal =
94          PropsValues.MONITORING_DATA_SAMPLE_THREAD_LOCAL;
95  
96      private List<DataSample> _dataSamples = new ArrayList<DataSample>();
97      private long _monitorTime;
98  
99  }