View Javadoc

1   /*
2    * Created on 03-Nov-2004
3    */
4   package org.jdiagnose.runtime;
5   
6   import org.jdiagnose.Diagnostic;
7   import org.jdiagnose.DiagnosticException;
8   import org.jdiagnose.DiagnosticMessage;
9   import org.jdiagnose.MessageFactory;
10  
11  /***
12   * @author jmccrindle
13   */
14  public class DefaultDiagnosticResult implements DiagnosticResult {
15      
16      private Diagnostic diagnostic;
17      private ResultState state = ResultState.NOT_STARTED;
18      private long startTime;
19      private long finishTime;
20      private DiagnosticMessage message;
21      private MessageFactory messageFactory;
22  
23      public DefaultDiagnosticResult(MessageFactory messageFactory, Diagnostic diagnostic) {
24          this.diagnostic = diagnostic;
25          this.messageFactory = messageFactory;
26      }
27  
28      /* (non-Javadoc)
29       * @see org.jdiagnose.runtime.DiagnosticResult#getName()
30       */
31      public String getName() {
32          return diagnostic.getName();
33      }
34  
35      /* (non-Javadoc)
36       * @see org.jdiagnose.runtime.DiagnosticResult#getState()
37       */
38      public ResultState getState() {
39          return state;
40      }
41  
42      /* (non-Javadoc)
43       * @see org.jdiagnose.runtime.DiagnosticResult#getDuration()
44       */
45      public long getDuration() {
46          return finishTime - startTime;
47      }
48  
49      /* (non-Javadoc)
50       * @see org.jdiagnose.runtime.DiagnosticResult#getStartTime()
51       */
52      public long getStartTime() {
53          return startTime;
54      }
55  
56      /* (non-Javadoc)
57       * @see org.jdiagnose.runtime.DiagnosticResult#getFinishTime()
58       */
59      public long getFinishTime() {
60          return finishTime;
61      }
62  
63      /* (non-Javadoc)
64       * @see org.jdiagnose.runtime.DiagnosticResult#diagnose()
65       */
66      public synchronized void diagnose() {
67          finishTime = startTime = System.currentTimeMillis();
68          try {
69              state = ResultState.RUNNING;
70              diagnostic.diagnose();
71              state = ResultState.SUCCEEDED;
72          } catch (DiagnosticException e) {
73              state = ResultState.FAILED;
74              this.message = messageFactory.getMessage(e);
75          } finally {
76              finishTime = System.currentTimeMillis();
77          }
78      }
79  
80      public Diagnostic getDiagnostic() {
81          return diagnostic;
82      }
83      
84      public DiagnosticMessage getMessage() {
85          return message;
86      }
87  
88  }