View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.db;
17  
18  import java.util.Collections;
19  import java.util.Date;
20  import java.util.List;
21  
22  import org.jdiagnose.DefaultDiagnosticMessage;
23  import org.jdiagnose.DiagnosticMessage;
24  import org.jdiagnose.RemoteResult;
25  import org.jdiagnose.ResultInfo;
26  import org.jdiagnose.runtime.ResultState;
27  
28  /***
29   * User: jamie
30   * Date: May 30, 2004
31   * Time: 7:42:52 PM
32   */
33  public class DbRemoteResult implements ResultInfo, RemoteResult, DiagnosticMessage {
34      
35      private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
36              .getLog(DbRemoteResult.class);
37  
38      public static final int MESSAGE_BODY_MAX_LENGTH = 1023;
39      public static final int MESSAGE_MESSAGE_MAX_LENGTH = 511;
40  
41      private String resultState;
42      
43      private long id;
44      private Date startDate;
45      private Date finishDate;
46      private String name;
47      private String summary;
48      private String body;
49      private long duration;
50      private String guid;
51      private String agent;
52      private String host;
53      private long sequenceNumber;
54  
55      public DbRemoteResult(RemoteResult remoteResult) {
56          ResultInfo result = remoteResult.getResultInfo();
57          this.guid = remoteResult.getGuid();
58          this.agent = remoteResult.getAgent();
59          this.host = remoteResult.getHost();
60          this.resultState = result.getState().toString();
61          this.startDate = new Date(result.getStartTime());
62          this.finishDate = new Date(result.getFinishTime());
63          this.duration = result.getFinishTime() - result.getStartTime();
64          this.name = result.getName();
65          this.sequenceNumber = remoteResult.getSequenceNumber();
66          DiagnosticMessage message = result.getMessage();
67          if(message != null) {
68              this.summary = truncate(message.getSummary(), MESSAGE_MESSAGE_MAX_LENGTH);
69              this.body = truncate(message.getBody(), MESSAGE_BODY_MAX_LENGTH);
70          }
71          log.debug(this.toString());
72      }
73      
74      /***
75       */
76      private String truncate(String value, int maxLength) {
77          if(value != null && value.length() >= maxLength) {
78              return value.substring(0, maxLength - 1);
79          }
80          return value;
81      }
82  
83      public DbRemoteResult() {
84          // noop
85      }
86      
87      public String toString() {
88          return 
89          getGuid() + "," +
90          getHost() + "," +
91          getAgent() + "," +
92          getStartTime() + "," +
93          getFinishTime() + "," +
94          getName() + "," +
95          getSummary() + "," +
96          getBody();
97      }
98  
99      public long getDuration() {
100         return duration;
101     }
102     
103     public void setDuration(long duration) {
104         this.duration = duration;
105     }
106     
107     public String getResultState() {
108         return resultState;
109     }
110     
111     public void setResultState(String resultState) {
112         this.resultState = resultState;
113     }
114     
115     public List getResults() {
116         return Collections.EMPTY_LIST;
117     }
118 
119     public ResultState getState() {
120         if(resultState.equals(ResultState.SUCCEEDED.toString())) {
121             return ResultState.SUCCEEDED;
122         } else if (resultState.equals(ResultState.FAILED.toString())) {
123             return ResultState.FAILED;
124         } else if (resultState.equals(ResultState.NOT_STARTED.toString())) {
125             return ResultState.NOT_STARTED;
126         } else if (resultState.equals(ResultState.RUNNING.toString())) {
127             return ResultState.RUNNING;
128         } else {
129             return null;
130         }
131     }
132 
133     public long getFinishTime() {
134         return finishDate.getTime();
135     }
136 
137     public long getStartTime() {
138         return startDate.getTime();
139     }
140 
141     public void setName(String name) {
142         this.name = name;
143     }
144     
145     public String getName() {
146         return name;
147     }
148 
149     public Date getFinishDate() {
150         return finishDate;
151     }
152     public void setFinishDate(Date finishDate) {
153         this.finishDate = finishDate;
154     }
155     public Date getStartDate() {
156         return startDate;
157     }
158     public void setStartDate(Date startDate) {
159         this.startDate = startDate;
160     }
161 
162     public ResultInfo getResultInfo() {
163         return this;
164     }
165 
166     public String getGuid() {
167         return guid;
168     }
169 
170     public String getAgent() {
171         return agent;
172     }
173 
174     public String getHost() {
175         return host;
176     }
177     /***
178      * @param agent The agent to set.
179      */
180     public void setAgent(String agent) {
181         this.agent = agent;
182     }
183     /***
184      * @param guid The guid to set.
185      */
186     public void setGuid(String guid) {
187         this.guid = guid;
188     }
189     /***
190      * @param host The host to set.
191      */
192     public void setHost(String host) {
193         this.host = host;
194     }
195     /***
196      * @return Returns the id.
197      */
198     public long getId() {
199         return id;
200     }
201     /***
202      * @param id The id to set.
203      */
204     public void setId(long id) {
205         this.id = id;
206     }
207     /***
208      * @return Returns the sequenceNumber.
209      */
210     public long getSequenceNumber() {
211         return sequenceNumber;
212     }
213     /***
214      * @param sequenceNumber The sequenceNumber to set.
215      */
216     public void setSequenceNumber(long sequenceNumber) {
217         this.sequenceNumber = sequenceNumber;
218     }
219     public String getBody() {
220         return body;
221     }
222     public String getSummary() {
223         return summary;
224     }
225     public void setBody(String body) {
226         this.body = body;
227     }
228     public void setSummary(String summary) {
229         this.summary = summary;
230     }
231 
232     /* (non-Javadoc)
233      * @see org.jdiagnose.ResultInfo#getMessage()
234      */
235     public DiagnosticMessage getMessage() {
236         return new DefaultDiagnosticMessage(summary, body);
237     }
238 }