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.monitoring.RequestStatus;
26  
27  import java.io.Serializable;
28  
29  import java.util.Map;
30  
31  import org.apache.commons.lang.time.StopWatch;
32  
33  /**
34   * <a href="BaseDataSample.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Michael C. Han
37   * @author Brian Wing Shun Chan
38   */
39  public class BaseDataSample implements DataSample, Serializable {
40  
41      public void capture(RequestStatus requestStatus) {
42          if (_stopWatch != null) {
43              _stopWatch.stop();
44  
45              _duration = _stopWatch.getTime();
46          }
47  
48          _requestStatus = requestStatus;
49      }
50  
51      public Map<String, String> getAttributes() {
52          return _attributes;
53      }
54  
55      public long getCompanyId() {
56          return _companyId;
57      }
58  
59      public String getDescription() {
60          return _description;
61      }
62  
63      public long getDuration() {
64          return _duration;
65      }
66  
67      public String getName() {
68          return _name;
69      }
70  
71      public String getNamespace() {
72          return _namespace;
73      }
74  
75      public RequestStatus getRequestStatus() {
76          return _requestStatus;
77      }
78  
79      public String getUser() {
80          return _user;
81      }
82  
83      public void prepare() {
84          if (_stopWatch == null) {
85              _stopWatch = new StopWatch();
86          }
87          _stopWatch.start();
88      }
89  
90      public void setAttributes(Map<String, String> attributes) {
91          _attributes = attributes;
92      }
93  
94      public void setCompanyId(long companyId) {
95          _companyId = companyId;
96      }
97  
98      public void setDescription(String description) {
99          _description = description;
100     }
101 
102     public void setName(String name) {
103         _name = name;
104     }
105 
106     public void setNamespace(String namespace) {
107         _namespace = namespace;
108     }
109 
110     public void setUser(String user) {
111         _user = user;
112     }
113 
114     public String toString() {
115         StringBuilder sb = new StringBuilder();
116 
117         sb.append("{attributes=");
118         sb.append(_attributes);
119         sb.append(", companyId=");
120         sb.append(_companyId);
121         sb.append(", description=");
122         sb.append(_description);
123         sb.append(", duration=");
124         sb.append(_duration);
125         sb.append(", name=");
126         sb.append(_name);
127         sb.append(", namespace=");
128         sb.append(_namespace);
129         sb.append(", requestStatus=");
130         sb.append(_requestStatus);
131         sb.append(", stopWatch=");
132         sb.append(_stopWatch);
133         sb.append(", user=");
134         sb.append(_user);
135         sb.append("}");
136 
137         return sb.toString();
138     }
139 
140     private Map<String, String> _attributes;
141     private long _companyId;
142     private String _description;
143     private long _duration;
144     private String _name;
145     private String _namespace;
146     private RequestStatus _requestStatus;
147     private transient StopWatch _stopWatch;
148     private String _user;
149 
150 }