1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.reporting.verifiers;
25
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.NodeList;
29
30 import com.mindtree.techworks.insight.reporting.InitializationException;
31 import com.mindtree.techworks.insight.reporting.SerializationXmlUtils;
32 import com.mindtree.techworks.insight.reporting.jobs.Job;
33 import com.mindtree.techworks.insight.reporting.jobs.JobInitializationException;
34 import com.mindtree.techworks.insight.reporting.jobs.JobPersistanceHandler;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public class VerifierDeserializerImpl implements VerifierDeserializer {
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public static Verifier deserialize (String serializedVerifier)
83 throws VerifierInitializationException {
84
85 Document document = null;
86 try {
87 document = SerializationXmlUtils.createDocument (serializedVerifier);
88 } catch (InitializationException e) {
89 throw new VerifierInitializationException (
90 "Error creating document", e);
91 }
92
93
94
95 String className = null;
96 try {
97 className = SerializationXmlUtils
98 .getSerializedObjectClassName (document);
99 } catch (InitializationException e) {
100 throw new VerifierInitializationException (
101 "Could not get class for Verifier.", e);
102 }
103
104 try {
105 Verifier newVerifier = (Verifier) Class.forName (className)
106 .newInstance ();
107 VerifierDeserializer deserializer = newVerifier.getDeserializer ();
108
109
110 return deserializer.deserializeVerifier (serializedVerifier);
111 } catch (InstantiationException e) {
112 throw new VerifierInitializationException (
113 "Could not get verifier class or instance.");
114 } catch (IllegalAccessException e) {
115 throw new VerifierInitializationException (
116 "Could not get verifier class or instance.");
117 } catch (ClassNotFoundException e) {
118 throw new VerifierInitializationException (
119 "Could not get verifier class or instance.");
120 }
121
122 }
123
124
125
126
127
128
129
130
131
132 public Verifier deserializeVerifier (String serializedVerifier)
133 throws VerifierInitializationException {
134
135
136 Document document = null;
137 try {
138 document = SerializationXmlUtils.createDocument (serializedVerifier);
139 } catch (InitializationException e) {
140 throw new VerifierInitializationException (
141 "Could not create XML document", e);
142 }
143
144
145 String className = null;
146 try {
147 className = SerializationXmlUtils
148 .getSerializedObjectClassName (document);
149 } catch (InitializationException e) {
150 throw new VerifierInitializationException (
151 "Could not get class for Verifier.", e);
152 }
153
154 Verifier newVerifier = null;
155
156 try {
157
158 newVerifier = (Verifier) Class.forName (className).newInstance ();
159
160 } catch (InstantiationException e) {
161 throw new VerifierInitializationException (
162 "Could not get verifier class or instance.");
163 } catch (IllegalAccessException e) {
164 throw new VerifierInitializationException (
165 "Could not get verifier class or instance.");
166 } catch (ClassNotFoundException e) {
167 throw new VerifierInitializationException (
168 "Could not get verifier class or instance.");
169 }
170
171
172
173 if ( !(newVerifier instanceof DefaultImplDeserializable)) {
174 throw new VerifierInitializationException (
175 "The verifier cannot be deserialized by this class - " +
176 "needs to implement DefaultImplDeserializable.");
177 }
178
179 DefaultImplDeserializable deserializable = (DefaultImplDeserializable) newVerifier;
180
181
182 Element documentElement = document.getDocumentElement ();
183 NodeList fieldsList = documentElement.getElementsByTagName ("fields");
184
185
186 if (fieldsList.getLength () > 0) {
187 Element fieldsRoot = (Element) fieldsList.item (0);
188 NodeList fields = fieldsRoot.getElementsByTagName ("field");
189 for (int i = 0; i < fields.getLength (); i++ ) {
190 processField ((Element) fields.item (i), deserializable);
191 }
192 }
193
194
195 NodeList jobsList = documentElement.getElementsByTagName("jobs");
196
197 if (jobsList.getLength () > 0) {
198 Element jobsRoot = (Element) jobsList.item (0);
199 NodeList jobs = jobsRoot.getElementsByTagName (
200 JobPersistanceHandler.SERIALIZED_JOB_ROOT_TAG);
201 for (int i = 0; i < jobs.getLength (); i++ ) {
202 processJob ((Element) jobs.item (i), newVerifier);
203 }
204 }
205
206 return newVerifier;
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 private void processField (Element field,
231 DefaultImplDeserializable deserializable)
232 throws VerifierInitializationException {
233
234
235 Element nameElement = (Element) field.getElementsByTagName ("name")
236 .item (0);
237
238 if (null == nameElement) {
239 throw new VerifierInitializationException (
240 "Each field needs a name!");
241 }
242
243 String name = SerializationXmlUtils.getTextValue (nameElement);
244
245
246 NodeList values = field.getElementsByTagName ("value");
247 for (int i = 0; i < values.getLength (); i++ ) {
248 String value = SerializationXmlUtils.getTextValue ((Element) values
249 .item (i));
250
251
252 deserializable.deserializeField (name, value);
253 }
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 private void processJob (Element serializedJob, Verifier verifier)
274 throws VerifierInitializationException {
275
276
277 String jobString = null;
278 try {
279 jobString = SerializationXmlUtils.serializeXmlElement(serializedJob);
280 } catch (InitializationException e) {
281 throw new VerifierInitializationException (
282 "Could not serialize Job DOM Element for processing.", e);
283 }
284 Job deserializedJob = null;
285
286
287 try {
288 deserializedJob = JobPersistanceHandler.deserializeJob(jobString);
289 } catch (JobInitializationException e) {
290
291 throw new VerifierInitializationException (
292 "Could not deserialize job", e);
293 }
294
295
296 verifier.registerJob(deserializedJob);
297
298 }
299 }