01: package dtw.webmail.util.config;
02:
03: import org.apache.log4j.Logger;
04: import org.exolab.castor.mapping.Mapping;
05: import org.exolab.castor.xml.Marshaller;
06: import org.exolab.castor.xml.Unmarshaller;
07:
08: import java.io.File;
09: import java.io.FileReader;
10: import java.io.FileWriter;
11: import java.net.URL;
12:
13: /**
14: * Class implementing a <tt>JwmaConfiguration</tt>
15: * with Castor XML based persistence.
16: * <p>
17: * @author Dieter Wimberger (wimpi)
18: * @version (created Feb 24, 2003)
19: */
20: public class CastorXMLConfiguration {
21:
22: private static Logger log = Logger
23: .getLogger(CastorXMLConfiguration.class);
24:
25: private File m_ConfigFile;
26: private Mapping m_Mapping;
27:
28: public CastorXMLConfiguration(File configfile) {
29: super ();
30: m_ConfigFile = configfile;
31: //load mapping
32: if (!loadMapping()) {
33: throw new RuntimeException(
34: "Failed to load configuration mapping file.");
35: }
36: }//constructor
37:
38: /**
39: * Loads the castor mapping file.
40: *
41: * @param file the url of the mapping file.
42: * @return true if successfully loaded, false otherwise.
43: */
44: private boolean loadMapping() {
45: try {
46: ClassLoader cl = this .getClass().getClassLoader();
47: URL mapping = cl
48: .getResource("dtw/webmail/resources/configuration_mapping.xml");
49: m_Mapping = new Mapping(cl);
50: m_Mapping.loadMapping(mapping);
51: log.debug("loadMapping()"
52: + ": Mapping loaded successfully.");
53: return true;
54: } catch (Exception ex) {
55: log.fatal("loadMapping()", ex);
56: return false;
57: }
58: }//loadMapping
59:
60: /**
61: * Stores a <tt>JwmaConfiguration</tt> instance
62: * to the given writer.
63: *
64: * @param config the instance to be marshalled.
65: * @param writer the <tt>Writer</tt>.
66: *
67: * @throws Exception on failure.
68: */
69: synchronized public void store(JwmaConfiguration config)
70: throws Exception {
71:
72: FileWriter writer = new FileWriter(m_ConfigFile);
73: Marshaller m_Marshaller = new Marshaller(writer);
74:
75: m_Marshaller.setMapping(m_Mapping);
76: m_Marshaller.marshal(config);
77: writer.flush();
78: writer.close();
79: log.debug("store()" + ": Configuration stored successfully.");
80: }//marshal
81:
82: /**
83: * Unmarshalls a <tt>JwmaConfiguration</tt> instance
84: * from the given reader.
85: *
86: * @param reader the <tt>Reader</tt>.
87: */
88: public JwmaConfiguration load() throws Exception {
89:
90: JwmaConfiguration config = null;
91: FileReader reader = new FileReader(m_ConfigFile);
92: Unmarshaller m_Unmarshaller = new Unmarshaller(m_Mapping);
93: config = (JwmaConfiguration) m_Unmarshaller.unmarshal(reader);
94: reader.close();
95: log.debug("load()" + ": Configuration loaded successfully.");
96: return config;
97: }//unmarshal
98:
99: }//class CastorXMLConfiguration
|