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