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.IOException;
20 import java.util.Locale;
21 import java.util.Properties;
22 import java.util.Stack;
23
24 import org.apache.log4j.Logger;
25 import org.xml.sax.Attributes;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.helpers.DefaultHandler;
28
29
30
31
32
33
34
35 public class SelfDiagnoseHandler extends DefaultHandler {
36 private Stack iteratorStack = new Stack();
37 private static Properties taskMapping;
38
39
40
41 static {
42 taskMapping = new Properties();
43 try {
44 taskMapping.load(SelfDiagnoseHandler.class.getResourceAsStream("/task-mapping.properties"));
45 } catch (IOException e) {
46 Logger.getLogger(SelfDiagnoseHandler.class).error("Unable to initialize SelfDiagnoseHandler",e);
47 }
48 }
49
50
51
52
53
54
55 public static void addBindingFor(Class diagnosticTaskClass){
56 if (!DiagnosticTask.class.isAssignableFrom(diagnosticTaskClass))
57 throw new RuntimeException("Only DiagnosticTask classes can be handled");
58 String tag = DiagnoseUtil.shortName(diagnosticTaskClass).toLowerCase(Locale.getDefault());
59 taskMapping.setProperty(tag, diagnosticTaskClass.getName());
60 }
61
62
63
64
65
66
67 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
68 if ("selfdiagnose".equals(qName))
69 return;
70 if ("tasks".equals(qName))
71 return;
72 if ("task".equals(qName)) {
73 this.handleCustomTask(attributes);
74 return;
75 }
76 if ("iterator".equals(qName)) {
77 this.handleIterator(attributes);
78 return;
79 }
80 try {
81
82 String className = taskMapping.getProperty(qName.toLowerCase(Locale.getDefault()));
83 Class taskClass = Thread.currentThread().getContextClassLoader().loadClass(className);
84 if (taskClass == null)
85 throw new RuntimeException("No task class registered (case-insensitive) for:" + qName);
86 DiagnosticTask task = (DiagnosticTask)taskClass.newInstance();
87 task.initializeFromAttributes(attributes);
88 this.addTaskToRegistration(task);
89 } catch (Exception ex){
90 throw new SAXException("Failed to create/initialize the task for:"+qName,ex);
91 }
92 }
93 public void endElement(String uri, String localName, String name) throws SAXException {
94 if ("iterator".equals(name)) {
95 CollectionIteratorTask cit = (CollectionIteratorTask) iteratorStack.pop();
96 this.addTaskToRegistration(cit);
97 }
98 }
99 private void handleIterator(Attributes attributes) {
100 CollectionIteratorTask cit = new CollectionIteratorTask();
101 cit.initializeFromAttributes(attributes);
102 iteratorStack.push(cit);
103 }
104 private void handleCustomTask(Attributes attributes) throws SAXException {
105 String className = attributes.getValue("class");
106 CustomDiagnosticTask customTask = new CustomDiagnosticTask();
107 if (className == null) throw new SAXException("Missing xml attribute [class] for tag [task]");
108 try {
109 Class taskClass = Thread.currentThread().getContextClassLoader().loadClass(className);
110 DiagnosticTask task = (DiagnosticTask)taskClass.newInstance();
111 task.initializeFromAttributes(attributes);
112 customTask.setTask(task);
113 } catch (Exception e) {
114 customTask.setErrorMessage(e.getMessage());
115 this.addTaskToRegistration(customTask);
116 throw new SAXException(e);
117 }
118 this.addTaskToRegistration(customTask);
119 }
120 private void addTaskToRegistration(DiagnosticTask task) {
121
122 if (!iteratorStack.isEmpty()) {
123 CollectionIteratorTask cit = (CollectionIteratorTask) iteratorStack.lastElement();
124 cit.register(task);
125 return;
126 }
127 SelfDiagnose.register(task,SelfDiagnose.getConfigFilename());
128 }
129 }