001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.monitoring.statistics;
016    
017    import com.liferay.portal.kernel.monitoring.RequestStatus;
018    import com.liferay.portal.kernel.monitoring.statistics.DataSample;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.io.Serializable;
022    
023    import java.util.Map;
024    
025    import org.apache.commons.lang.time.StopWatch;
026    
027    /**
028     * @author Michael C. Han
029     * @author Brian Wing Shun Chan
030     */
031    public class BaseDataSample implements DataSample, Serializable {
032    
033            public void capture(RequestStatus requestStatus) {
034                    if (_stopWatch != null) {
035                            _stopWatch.stop();
036    
037                            _duration = _stopWatch.getTime();
038                    }
039    
040                    if ((_timeout > 0) && (_duration >= _timeout) &&
041                            (requestStatus != RequestStatus.ERROR)) {
042    
043                            _requestStatus = RequestStatus.TIMEOUT;
044                    }
045                    else {
046                            _requestStatus = requestStatus;
047                    }
048            }
049    
050            public Map<String, String> getAttributes() {
051                    return _attributes;
052            }
053    
054            @Override
055            public long getCompanyId() {
056                    return _companyId;
057            }
058    
059            @Override
060            public String getDescription() {
061                    return _description;
062            }
063    
064            @Override
065            public long getDuration() {
066                    return _duration;
067            }
068    
069            @Override
070            public long getGroupId() {
071                    return _groupId;
072            }
073    
074            @Override
075            public String getName() {
076                    return _name;
077            }
078    
079            @Override
080            public String getNamespace() {
081                    return _namespace;
082            }
083    
084            @Override
085            public RequestStatus getRequestStatus() {
086                    return _requestStatus;
087            }
088    
089            public long getTimeout() {
090                    return _timeout;
091            }
092    
093            @Override
094            public String getUser() {
095                    return _user;
096            }
097    
098            public void prepare() {
099                    if (_stopWatch == null) {
100                            _stopWatch = new StopWatch();
101                    }
102    
103                    _stopWatch.start();
104            }
105    
106            public void setAttributes(Map<String, String> attributes) {
107                    _attributes = attributes;
108            }
109    
110            public void setCompanyId(long companyId) {
111                    _companyId = companyId;
112            }
113    
114            public void setDescription(String description) {
115                    _description = description;
116            }
117    
118            public void setGroupId(long groupId) {
119                    _groupId = groupId;
120            }
121    
122            public void setName(String name) {
123                    _name = name;
124            }
125    
126            public void setNamespace(String namespace) {
127                    _namespace = namespace;
128            }
129    
130            public void setTimeout(long timeout) {
131                    _timeout = timeout;
132            }
133    
134            public void setUser(String user) {
135                    _user = user;
136            }
137    
138            @Override
139            public String toString() {
140                    StringBundler sb = new StringBundler(21);
141    
142                    sb.append("{attributes=");
143                    sb.append(_attributes);
144                    sb.append(", companyId=");
145                    sb.append(_companyId);
146                    sb.append(", description=");
147                    sb.append(_description);
148                    sb.append(", duration=");
149                    sb.append(_duration);
150                    sb.append(", groupId=");
151                    sb.append(_groupId);
152                    sb.append(", name=");
153                    sb.append(_name);
154                    sb.append(", namespace=");
155                    sb.append(_namespace);
156                    sb.append(", requestStatus=");
157                    sb.append(_requestStatus);
158                    sb.append(", stopWatch=");
159                    sb.append(_stopWatch);
160                    sb.append(", timeout=");
161                    sb.append(_timeout);
162                    sb.append(", user=");
163                    sb.append(_user);
164                    sb.append("}");
165    
166                    return sb.toString();
167            }
168    
169            private Map<String, String> _attributes;
170            private long _companyId;
171            private String _description;
172            private long _duration;
173            private long _groupId;
174            private String _name;
175            private String _namespace;
176            private RequestStatus _requestStatus;
177            private transient StopWatch _stopWatch;
178            private long _timeout = -1;
179            private String _user;
180    
181    }