1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.philemonworks.selfdiagnose;
18
19 import java.io.Serializable;
20 import java.util.Locale;
21
22 import org.apache.log4j.Logger;
23 import org.xml.sax.Attributes;
24
25
26
27
28
29
30
31
32 public abstract class DiagnosticTask implements Serializable {
33 private static final long serialVersionUID = -1320140869670492598L;
34 public static final String PARAMETER_VARIABLE = "var";
35 public static final String PARAMETER_COMMENT= "comment";
36 public static final String PARAMETER_REPORT= "report";
37
38 protected String requestor = "{unknown}";
39
40
41
42 public String variableName;
43
44
45
46 public String comment;
47
48
49
50 public boolean reportResults = true;
51
52
53
54
55 public DiagnosticTaskResult createResult(){
56 return new DiagnosticTaskResult(this);
57 }
58
59
60
61 public abstract String getDescription();
62
63
64
65
66
67
68 public String getTaskName() {
69 Class theClass= this.getClass();
70 String taskName = theClass.getName();
71 return (taskName.substring(taskName.lastIndexOf('.') + 1)).toLowerCase(Locale.ENGLISH);
72 }
73
74
75
76
77 public String getRequestor() {
78 return requestor;
79 }
80
81
82
83
84
85
86 public void setUp(ExecutionContext ctx) throws DiagnoseException {
87 }
88
89
90
91
92 public DiagnosticTaskResult run(){
93 return this.run(new ExecutionContext());
94 }
95
96
97
98
99
100 public DiagnosticTaskResult run(ExecutionContext ctx){
101 DiagnosticTaskResult result = this.createResult();
102 long begin = System.currentTimeMillis();
103 long end = begin;
104 try {
105 this.setUp(ctx);
106 this.run(ctx, result);
107 end = System.currentTimeMillis();
108 } catch (DiagnoseException e) {
109 result.setFailedMessage(e.getMessage());
110 } catch (Exception ex) {
111 String why = ex.getMessage();
112 if (ex.getCause() != null) why = ex.getCause().getMessage();
113 if (why == null) why = ex.toString();
114 result.setErrorMessage("Unexpected error occurred while running this task because: " + why);
115 Logger.getLogger(SelfDiagnose.class).error(ex);
116 }
117 result.setExecutionTime(end-begin);
118 return result;
119 }
120
121
122
123
124
125
126
127
128 public abstract void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException;
129
130
131
132
133 public void setRequestor(String identifier) {
134 requestor = identifier;
135 }
136
137
138
139
140
141
142
143
144 public void initializeFromAttributes(Attributes attributes){
145 this.setComment(attributes.getValue(PARAMETER_COMMENT));
146 this.setVariableName(attributes.getValue(PARAMETER_VARIABLE));
147 this.setReportResults(!"false".equals(attributes.getValue(PARAMETER_REPORT)));
148 }
149
150
151
152
153
154
155
156 public String getDefaultReportTemplate(boolean isSuccess){
157 return this.getClass().getName() + (isSuccess ? " passed " : " failed ") +
158 "[missing getDefaultReportTemplate()]";
159 }
160
161
162
163 public String getVariableName() {
164 return variableName;
165 }
166
167
168
169 public void setVariableName(String varName) {
170 variableName = varName;
171 }
172 public boolean hasComment() {
173 return comment != null && comment.length() > 0;
174 }
175
176 public String getComment() {
177 return comment;
178 }
179 public void setComment(String comment) {
180 this.comment = comment;
181 }
182 public boolean isReportResults() {
183 return reportResults;
184 }
185 public void setReportResults(boolean reportResults) {
186 this.reportResults = reportResults;
187 }
188 }