001    /**
002     * Copyright (c) 2000-2013 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    
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 String getName() {
071                    return _name;
072            }
073    
074            @Override
075            public String getNamespace() {
076                    return _namespace;
077            }
078    
079            @Override
080            public RequestStatus getRequestStatus() {
081                    return _requestStatus;
082            }
083    
084            public long getTimeout() {
085                    return _timeout;
086            }
087    
088            @Override
089            public String getUser() {
090                    return _user;
091            }
092    
093            public void prepare() {
094                    if (_stopWatch == null) {
095                            _stopWatch = new StopWatch();
096                    }
097    
098                    _stopWatch.start();
099            }
100    
101            public void setAttributes(Map<String, String> attributes) {
102                    _attributes = attributes;
103            }
104    
105            public void setCompanyId(long companyId) {
106                    _companyId = companyId;
107            }
108    
109            public void setDescription(String description) {
110                    _description = description;
111            }
112    
113            public void setName(String name) {
114                    _name = name;
115            }
116    
117            public void setNamespace(String namespace) {
118                    _namespace = namespace;
119            }
120    
121            public void setTimeout(long timeout) {
122                    _timeout = timeout;
123            }
124    
125            public void setUser(String user) {
126                    _user = user;
127            }
128    
129            @Override
130            public String toString() {
131                    StringBundler sb = new StringBundler(19);
132    
133                    sb.append("{attributes=");
134                    sb.append(_attributes);
135                    sb.append(", companyId=");
136                    sb.append(_companyId);
137                    sb.append(", description=");
138                    sb.append(_description);
139                    sb.append(", duration=");
140                    sb.append(_duration);
141                    sb.append(", name=");
142                    sb.append(_name);
143                    sb.append(", namespace=");
144                    sb.append(_namespace);
145                    sb.append(", requestStatus=");
146                    sb.append(_requestStatus);
147                    sb.append(", stopWatch=");
148                    sb.append(_stopWatch);
149                    sb.append(", timeout=");
150                    sb.append(_timeout);
151                    sb.append(", user=");
152                    sb.append(_user);
153                    sb.append("}");
154    
155                    return sb.toString();
156            }
157    
158            private Map<String, String> _attributes;
159            private long _companyId;
160            private String _description;
161            private long _duration;
162            private String _name;
163            private String _namespace;
164            private RequestStatus _requestStatus;
165            private transient StopWatch _stopWatch;
166            private long _timeout = -1;
167            private String _user;
168    
169    }