001    /**
002     * Copyright (c) 2000-2010 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.util.StringBundler;
018    import com.liferay.portal.monitoring.RequestStatus;
019    
020    import java.io.Serializable;
021    
022    import java.util.Map;
023    
024    import org.apache.commons.lang.time.StopWatch;
025    
026    /**
027     * @author Michael C. Han
028     * @author Brian Wing Shun Chan
029     */
030    public class BaseDataSample implements DataSample, Serializable {
031    
032            public void capture(RequestStatus requestStatus) {
033                    if (_stopWatch != null) {
034                            _stopWatch.stop();
035    
036                            _duration = _stopWatch.getTime();
037                    }
038    
039                    _requestStatus = requestStatus;
040            }
041    
042            public Map<String, String> getAttributes() {
043                    return _attributes;
044            }
045    
046            public long getCompanyId() {
047                    return _companyId;
048            }
049    
050            public String getDescription() {
051                    return _description;
052            }
053    
054            public long getDuration() {
055                    return _duration;
056            }
057    
058            public String getName() {
059                    return _name;
060            }
061    
062            public String getNamespace() {
063                    return _namespace;
064            }
065    
066            public RequestStatus getRequestStatus() {
067                    return _requestStatus;
068            }
069    
070            public String getUser() {
071                    return _user;
072            }
073    
074            public void prepare() {
075                    if (_stopWatch == null) {
076                            _stopWatch = new StopWatch();
077                    }
078                    _stopWatch.start();
079            }
080    
081            public void setAttributes(Map<String, String> attributes) {
082                    _attributes = attributes;
083            }
084    
085            public void setCompanyId(long companyId) {
086                    _companyId = companyId;
087            }
088    
089            public void setDescription(String description) {
090                    _description = description;
091            }
092    
093            public void setName(String name) {
094                    _name = name;
095            }
096    
097            public void setNamespace(String namespace) {
098                    _namespace = namespace;
099            }
100    
101            public void setUser(String user) {
102                    _user = user;
103            }
104    
105            public String toString() {
106                    StringBundler sb = new StringBundler(19);
107    
108                    sb.append("{attributes=");
109                    sb.append(_attributes);
110                    sb.append(", companyId=");
111                    sb.append(_companyId);
112                    sb.append(", description=");
113                    sb.append(_description);
114                    sb.append(", duration=");
115                    sb.append(_duration);
116                    sb.append(", name=");
117                    sb.append(_name);
118                    sb.append(", namespace=");
119                    sb.append(_namespace);
120                    sb.append(", requestStatus=");
121                    sb.append(_requestStatus);
122                    sb.append(", stopWatch=");
123                    sb.append(_stopWatch);
124                    sb.append(", user=");
125                    sb.append(_user);
126                    sb.append("}");
127    
128                    return sb.toString();
129            }
130    
131            private Map<String, String> _attributes;
132            private long _companyId;
133            private String _description;
134            private long _duration;
135            private String _name;
136            private String _namespace;
137            private RequestStatus _requestStatus;
138            private transient StopWatch _stopWatch;
139            private String _user;
140    
141    }