1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.dao.xml; |
15 | |
|
16 | |
import java.beans.DefaultPersistenceDelegate; |
17 | |
import java.beans.XMLEncoder; |
18 | |
import java.io.BufferedOutputStream; |
19 | |
import java.io.FileOutputStream; |
20 | |
import java.util.Set; |
21 | |
|
22 | |
import net.admin4j.config.Admin4JConfiguration; |
23 | |
import net.admin4j.dao.ExceptionInfoDAO; |
24 | |
import net.admin4j.entity.ExceptionInfo; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 12 | public class ExceptionInfoDAOXml extends BaseDAOXml implements ExceptionInfoDAO { |
32 | |
|
33 | 6 | private static final Object SAVE_LOCK = new Object(); |
34 | |
|
35 | |
@SuppressWarnings("unchecked") |
36 | |
public Set<ExceptionInfo> findAll() { |
37 | 9 | return (Set<ExceptionInfo>) readXmlFile(Admin4JConfiguration.getExceptionInformationXmlFileName()); |
38 | |
} |
39 | |
|
40 | |
public void saveAll(Set<ExceptionInfo> exceptionList) { |
41 | 3 | XMLEncoder encoder = null; |
42 | 3 | String xmlFileName = Admin4JConfiguration.getExceptionInformationXmlFileName(); |
43 | 3 | String tempFileName = xmlFileName + ".temp"; |
44 | 3 | String previousFileName = derivePreviousFileName(xmlFileName); |
45 | |
|
46 | 3 | boolean encoderClosed = false; |
47 | |
|
48 | 3 | synchronized (SAVE_LOCK) { |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 3 | ClassLoader currentContextLoader = Thread.currentThread().getContextClassLoader(); |
58 | |
try { |
59 | 3 | Thread.currentThread().setContextClassLoader(ExceptionInfo.class.getClassLoader()); |
60 | 3 | encoder = new XMLEncoder( |
61 | |
new BufferedOutputStream( |
62 | |
new FileOutputStream(tempFileName))); |
63 | 3 | encoder.setExceptionListener(new DefaultExceptionListener(xmlFileName)); |
64 | 3 | encoder.setPersistenceDelegate(ExceptionInfo.class, |
65 | |
new DefaultPersistenceDelegate( |
66 | |
new String[]{"exceptionClassName","stackTrace", "alternateId"})); |
67 | 3 | encoder.setPersistenceDelegate(ExceptionInfo.TimeStamp.class, |
68 | |
new DefaultPersistenceDelegate( |
69 | |
new String[]{"timestampInMillis"})); |
70 | 3 | encoder.setPersistenceDelegate(StackTraceElement.class, |
71 | |
new DefaultPersistenceDelegate( |
72 | |
new String[]{"className","methodName", |
73 | |
"fileName","lineNumber"})); |
74 | 3 | encoder.writeObject(exceptionList); |
75 | 3 | encoder.close(); |
76 | 3 | encoderClosed = true; |
77 | |
|
78 | 3 | versionOutputFile(xmlFileName, tempFileName, previousFileName); |
79 | |
|
80 | |
} |
81 | 0 | catch (Throwable t) { |
82 | 0 | if (encoder != null && !encoderClosed) encoder.close(); |
83 | |
} |
84 | |
finally { |
85 | 3 | Thread.currentThread().setContextClassLoader(currentContextLoader); |
86 | 3 | } |
87 | 3 | } |
88 | |
|
89 | 3 | } |
90 | |
|
91 | |
} |