1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.monitoring.statistics;
16  
17  import com.liferay.portal.kernel.util.AutoResetThreadLocal;
18  import com.liferay.portal.util.PropsValues;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  /**
25   * <a href="DataSampleThreadLocal.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Michael C. Han
28   * @author Brian Wing Shun Chan
29   */
30  public class DataSampleThreadLocal implements Cloneable {
31  
32      public static void addDataSample(DataSample dataSample) {
33          if (!_monitoringDataSampleThreadLocal) {
34              return;
35          }
36  
37          _dataSampleThreadLocal.get()._addDataSample(dataSample);
38      }
39  
40      public static void clearDataSamples() {
41          _dataSampleThreadLocal.remove();
42      }
43  
44      public static List<DataSample> getDataSamples() {
45          if (!_monitoringDataSampleThreadLocal) {
46              return Collections.EMPTY_LIST;
47          }
48  
49          return _dataSampleThreadLocal.get()._getDataSamples();
50      }
51  
52      public static boolean isMonitoringDataSampleThreadLocal() {
53          return _monitoringDataSampleThreadLocal;
54      }
55  
56      public static void setMonitoringDataSampleThreadLocal(
57          boolean monitoringDataSampleThreadLocal) {
58  
59          _monitoringDataSampleThreadLocal = monitoringDataSampleThreadLocal;
60      }
61  
62      public Object clone() {
63          return new DataSampleThreadLocal();
64      }
65  
66      public long getMonitorTime() {
67          return _monitorTime;
68      }
69  
70      private DataSampleThreadLocal() {
71          _monitorTime = System.currentTimeMillis();
72      }
73  
74      private void _addDataSample(DataSample dataSample) {
75          _dataSamples.add(dataSample);
76      }
77  
78      private List<DataSample> _getDataSamples() {
79          return _dataSamples;
80      }
81  
82      private static ThreadLocal<DataSampleThreadLocal> _dataSampleThreadLocal =
83          new AutoResetThreadLocal<DataSampleThreadLocal>(
84              new DataSampleThreadLocal());
85      private static boolean _monitoringDataSampleThreadLocal =
86          PropsValues.MONITORING_DATA_SAMPLE_THREAD_LOCAL;
87  
88      private List<DataSample> _dataSamples = new ArrayList<DataSample>();
89      private long _monitorTime;
90  
91  }