1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.dao.xml; |
15 | |
|
16 | |
import java.beans.XMLDecoder; |
17 | |
import java.io.BufferedInputStream; |
18 | |
import java.io.File; |
19 | |
import java.io.FileInputStream; |
20 | |
|
21 | |
import net.admin4j.util.Admin4jRuntimeException; |
22 | |
|
23 | |
import org.slf4j.Logger; |
24 | |
import org.slf4j.LoggerFactory; |
25 | |
|
26 | 24 | public abstract class BaseDAOXml { |
27 | |
|
28 | 24 | private Logger logger = LoggerFactory.getLogger(this.getClass()); |
29 | |
|
30 | |
protected void versionOutputFile(String xmlFileName, String tempFileName, |
31 | |
String previousFileName) { |
32 | |
File file; |
33 | |
|
34 | 6 | file = new File(previousFileName); |
35 | 6 | if (file.exists()) { |
36 | 0 | file.delete(); |
37 | |
} |
38 | 6 | file = new File(xmlFileName); |
39 | 6 | if (file.exists()) { |
40 | 0 | file.renameTo(new File(previousFileName)); |
41 | |
} |
42 | |
|
43 | |
|
44 | 6 | file = new File(tempFileName); |
45 | 6 | file.renameTo(new File(xmlFileName)); |
46 | 6 | } |
47 | |
|
48 | |
protected String derivePreviousFileName(String xmlFileName) { |
49 | 6 | return xmlFileName + ".previous"; |
50 | |
} |
51 | |
|
52 | |
protected Object readXmlFile(String xmlFileName) { |
53 | 9 | Object result = null; |
54 | 9 | XMLDecoder decoder = null; |
55 | 9 | boolean exceptionThrown = false; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 9 | ClassLoader currentContextLoader = Thread.currentThread().getContextClassLoader(); |
62 | |
try { |
63 | 9 | Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); |
64 | 9 | DefaultExceptionListener errorListener = new DefaultExceptionListener(xmlFileName); |
65 | 9 | decoder = new XMLDecoder( |
66 | |
new BufferedInputStream( |
67 | |
new FileInputStream(xmlFileName))); |
68 | 9 | decoder.setExceptionListener(errorListener); |
69 | 9 | try {result = decoder.readObject();} |
70 | 0 | catch (Exception e) { |
71 | 0 | logger.error("Error reading file " + xmlFileName, e); |
72 | 0 | exceptionThrown = true; |
73 | 9 | } |
74 | |
|
75 | 9 | if (errorListener.isError() || exceptionThrown) { |
76 | 0 | exceptionThrown = false; |
77 | 0 | String previousFileName = derivePreviousFileName(xmlFileName); |
78 | 0 | errorListener = new DefaultExceptionListener(previousFileName); |
79 | 0 | decoder = new XMLDecoder( |
80 | |
new BufferedInputStream( |
81 | |
new FileInputStream(previousFileName))); |
82 | 0 | decoder.setExceptionListener(errorListener); |
83 | 0 | try {result = decoder.readObject();} |
84 | 0 | catch (Exception e) { |
85 | 0 | logger.error("Error reading file " + previousFileName, e); |
86 | 0 | exceptionThrown = true; |
87 | 0 | } |
88 | |
|
89 | 0 | if (errorListener.isError() || exceptionThrown) { |
90 | 0 | Admin4jRuntimeException ae = new Admin4jRuntimeException("Error reading xml Input after two attempts"); |
91 | 0 | ae.addContextValue("xmlFileName", xmlFileName); |
92 | 0 | ae.addContextValue("previousFileName", previousFileName); |
93 | |
|
94 | 0 | throw ae; |
95 | |
} |
96 | |
else { |
97 | 0 | logger.warn("Error reading xmlFile {}. Back up copy used.", xmlFileName); |
98 | |
} |
99 | |
} |
100 | |
} |
101 | 0 | catch (Throwable t) { |
102 | 0 | throw new Admin4jRuntimeException("Error reading XML Exception Information.", t) |
103 | |
.addContextValue("xmlFileName", xmlFileName); |
104 | |
} |
105 | |
finally { |
106 | 9 | if (decoder != null) decoder.close(); |
107 | 9 | Thread.currentThread().setContextClassLoader(currentContextLoader); |
108 | 9 | } |
109 | |
|
110 | 9 | return result; |
111 | |
} |
112 | |
|
113 | |
} |