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.InputStream;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import javax.xml.parsers.SAXParser;
26 import javax.xml.parsers.SAXParserFactory;
27
28 import org.apache.log4j.Logger;
29
30 import com.philemonworks.selfdiagnose.output.DiagnoseRun;
31 import com.philemonworks.selfdiagnose.output.DiagnoseRunReporter;
32 import com.philemonworks.selfdiagnose.output.XMLReporter;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public abstract class SelfDiagnose {
49 public static final String MDC_SELFDIAGNOSE_TASK_RESULT = "selfdiagnose-task-result";
50
51
52
53
54 public final static String VERSION = "2.4-SNAPSHOT";
55 public final static String COPYRIGHT = "(c) PhilemonWorks.com";
56 public final static String CONFIG = "selfdiagnose.xml";
57 private static URL CONFIG_URL = null;
58 private final static Logger LOG = Logger.getLogger(SelfDiagnose.class);
59
60 private static List<DiagnosticTask> tasks = Collections.synchronizedList(new ArrayList<DiagnosticTask>());
61 static {
62 SelfDiagnose.configure(CONFIG);
63 }
64
65
66
67
68
69 public static String getConfigFilename(){
70 if (SelfDiagnose.CONFIG_URL == null) return "unknown";
71 String resourceName = SelfDiagnose.CONFIG_URL.getFile();
72 return resourceName.substring(resourceName.lastIndexOf('/')+1);
73 }
74
75
76
77
78
79
80 public static void configure(String resourceName) {
81
82 URL configURL = DiagnoseUtil.findResource(resourceName, false);
83 if (configURL == null) {
84 LOG
85 .warn("No configuration found. SelfDiagnose will only run tasks that are registered by the application. "
86 + "If this was not intended then make sure that the configuration ["
87 + resourceName
88 + "] can be found on the classpath.");
89 return;
90 }
91 SelfDiagnose.configure(configURL);
92 }
93
94
95
96
97 public static void configure(URL configURL) {
98 if (configURL == null) {
99 LOG.info("Unable to configure because URL is not given");
100 return;
101 }
102 LOG.info("Initializing from configuration [" + configURL.getFile() + "]");
103 tasks = Collections.synchronizedList(new ArrayList());
104 CONFIG_URL = configURL;
105 try {
106 configure(configURL.openStream());
107 } catch (Exception e) {
108 LOG.error("Aborted configuration of SelfDiagnose using [" + configURL.getFile() + "] because: " + e.toString());
109 }
110 }
111
112
113
114
115
116 public static void configure(InputStream is) throws Exception {
117
118 SAXParser p = SAXParserFactory.newInstance().newSAXParser();
119 SelfDiagnoseHandler s = new SelfDiagnoseHandler();
120 p.parse(is, s);
121 is.close();
122 }
123
124
125
126
127
128 public static void reloadConfiguration() {
129 LOG.info("Flushing registered diagnostic tasks by configuration");
130 SelfDiagnose.configure(CONFIG_URL);
131 }
132
133
134
135
136
137
138
139
140 public static DiagnosticTask register(DiagnosticTask task) {
141 return SelfDiagnose.register(task, DiagnoseUtil.detectRequestorClass().getName());
142 }
143
144
145
146
147
148
149
150
151
152
153 public static DiagnosticTask register(DiagnosticTask task, String identifier) {
154 task.setRequestor(identifier);
155 tasks.add(task);
156 return task;
157 }
158
159 public static DiagnoseRun runTasks() { return runTasks(new XMLReporter()); }
160
161
162
163
164
165
166 public static DiagnoseRun runTasks(DiagnoseRunReporter reporter) {
167 return SelfDiagnose.runTasks(tasks, reporter, new ExecutionContext());
168 }
169
170
171
172
173 public static DiagnoseRun runTasks(DiagnoseRunReporter reporter, ExecutionContext ctx) {
174 return SelfDiagnose.runTasks(tasks, reporter, ctx);
175 }
176
177
178
179
180 public static DiagnoseRun runTasks(List<DiagnosticTask> taskList, DiagnoseRunReporter reporter, ExecutionContext ctx) {
181 DiagnoseRun run = new DiagnoseRun();
182 List<DiagnosticTaskResult> results = new ArrayList<DiagnosticTaskResult>(taskList.size());
183 for (int i = 0; i < taskList.size(); i++) {
184 DiagnosticTask each = (DiagnosticTask) taskList.get(i);
185 DiagnosticTaskResult result = each.run(ctx);
186 result.addToResults(results);
187 }
188 run.finished();
189 run.results = results;
190 reporter.report(run);
191 return run;
192 }
193
194
195
196
197
198 public static void run() {
199 SelfDiagnose.runTasks(new XMLReporter());
200 }
201
202
203
204 public static void flush() {
205 tasks = Collections.synchronizedList(new ArrayList<DiagnosticTask>());
206 }
207
208
209
210
211
212 public static List<DiagnosticTask> getTasks() {
213 return tasks;
214 }
215 }