001    /**
002     * Copyright (c) 2000-2012 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.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                            _requestStatus = RequestStatus.TIMEOUT;
043                    }
044                    else {
045                            _requestStatus = requestStatus;
046                    }
047            }
048    
049            public Map<String, String> getAttributes() {
050                    return _attributes;
051            }
052    
053            public long getCompanyId() {
054                    return _companyId;
055            }
056    
057            public String getDescription() {
058                    return _description;
059            }
060    
061            public long getDuration() {
062                    return _duration;
063            }
064    
065            public String getName() {
066                    return _name;
067            }
068    
069            public String getNamespace() {
070                    return _namespace;
071            }
072    
073            public RequestStatus getRequestStatus() {
074                    return _requestStatus;
075            }
076    
077            public long getTimeout() {
078                    return _timeout;
079            }
080    
081            public String getUser() {
082                    return _user;
083            }
084    
085            public void prepare() {
086                    if (_stopWatch == null) {
087                            _stopWatch = new StopWatch();
088                    }
089    
090                    _stopWatch.start();
091            }
092    
093            public void setAttributes(Map<String, String> attributes) {
094                    _attributes = attributes;
095            }
096    
097            public void setCompanyId(long companyId) {
098                    _companyId = companyId;
099            }
100    
101            public void setDescription(String description) {
102                    _description = description;
103            }
104    
105            public void setName(String name) {
106                    _name = name;
107            }
108    
109            public void setNamespace(String namespace) {
110                    _namespace = namespace;
111            }
112    
113            public void setTimeout(long timeout) {
114                    _timeout = timeout;
115            }
116    
117            public void setUser(String user) {
118                    _user = user;
119            }
120    
121            @Override
122            public String toString() {
123                    StringBundler sb = new StringBundler(19);
124    
125                    sb.append("{attributes=");
126                    sb.append(_attributes);
127                    sb.append(", companyId=");
128                    sb.append(_companyId);
129                    sb.append(", description=");
130                    sb.append(_description);
131                    sb.append(", duration=");
132                    sb.append(_duration);
133                    sb.append(", name=");
134                    sb.append(_name);
135                    sb.append(", namespace=");
136                    sb.append(_namespace);
137                    sb.append(", requestStatus=");
138                    sb.append(_requestStatus);
139                    sb.append(", stopWatch=");
140                    sb.append(_stopWatch);
141                    sb.append(", timeout=");
142                    sb.append(_timeout);
143                    sb.append(", user=");
144                    sb.append(_user);
145                    sb.append("}");
146    
147                    return sb.toString();
148            }
149    
150            private Map<String, String> _attributes;
151            private long _companyId;
152            private String _description;
153            private long _duration;
154            private String _name;
155            private String _namespace;
156            private RequestStatus _requestStatus;
157            private transient StopWatch _stopWatch;
158            private long _timeout = -1;
159            private String _user;
160    
161    }