001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.plugin.std;
009:
010: import dtw.webmail.JwmaKernel;
011: import dtw.webmail.model.JwmaException;
012: import dtw.webmail.util.CastorDatabasePool;
013: import org.apache.log4j.Logger;
014: import org.exolab.castor.jdo.Database;
015: import org.exolab.castor.jdo.JDO;
016: import org.exolab.castor.mapping.Mapping;
017: import org.exolab.castor.xml.Marshaller;
018: import org.exolab.castor.xml.Unmarshaller;
019:
020: import java.io.Reader;
021: import java.io.Writer;
022: import java.net.URL;
023:
024: /**
025: * An utility/helper class with common functionality
026: * for the standard Castor based plugin implementations.
027: *
028: * @author Dieter Wimberger
029: * @version 0.9.7 07/02/2003
030: */
031: public class CastorHelper {
032:
033: //Singleton reference
034: private static CastorHelper c_Self;
035: //logging
036: private static Logger log = Logger.getLogger(CastorHelper.class);
037:
038: //instance attributes
039: private Mapping m_Mapping; //Mapping instance
040: private JDO m_JDO; //JDO Engine instance
041: private CastorDatabasePool m_DBPool;
042: private boolean m_JDOprepared = false;
043:
044: private CastorHelper() {
045: //set reference to single instance
046: c_Self = this ;
047: //load mapping
048: loadMapping();
049: }//CastorHelper
050:
051: /**
052: * Marshals an object to the given writer.
053: *
054: * @param object the instance to be marshalled.
055: * @param writer the <tt>Writer</tt>.
056: *
057: * @throws JwmaException on failure.
058: */
059: public void marshal(Object object, Writer writer)
060: throws JwmaException {
061: try {
062: Marshaller m_Marshaller = new Marshaller(writer);
063: m_Marshaller.setMapping(m_Mapping);
064: m_Marshaller.marshal(object);
065: writer.flush();
066: } catch (Exception ex) {
067: throw new JwmaException("").setException(ex);
068: }
069: }//marshal
070:
071: /**
072: * Unmarshalls an object from the given reader.
073: *
074: * @param reader the <tt>Reader</tt>.
075: */
076: public Object unmarshal(Reader reader) throws JwmaException {
077: try {
078: Unmarshaller m_Unmarshaller = new Unmarshaller(m_Mapping);
079: return m_Unmarshaller.unmarshal(reader);
080: } catch (Exception ex) {
081: throw new JwmaException("").setException(ex);
082: }
083: }//unmarshal
084:
085: /**
086: * Prepares Castor JDO, with configuration
087: * (i.e. database.xml) given as URL.
088: *
089: * @param configurl the URL to the configuration file.
090: */
091: public void prepareJDO(String configurl) throws JwmaException {
092: if (m_JDOprepared) {
093: return;
094: }
095: try {
096: //create new jdo object
097: m_JDO = new JDO();
098: //DEBUG:
099: //m_JDO.setLogWriter(new org.exolab.castor.util.Logger(
100: // new PrintWriter(new FileWriter("/Users/wimpi/castorlog.txt"))
101: //));
102: //load configuration
103: m_JDO.loadConfiguration(configurl);
104:
105: //pooling connections
106: m_JDO.setDatabasePooling(true);
107: //the name of the database
108: m_JDO.setDatabaseName("jwma");
109: //set jwma's class loader, just in case
110: //castor was loaded with some strange class loader
111: m_JDO.setClassLoader(JwmaKernel.getReference().getClass()
112: .getClassLoader());
113:
114: //prepare pool
115: m_DBPool = CastorDatabasePool.createCastorDatabasePool(5,
116: new JwmaDatabase());
117: m_JDOprepared = true;
118: } catch (Exception ex) {
119: String msg = JwmaKernel.getReference().getLogMessage(
120: "jwma.plugin.castorhelper.jdofail");
121: log.error(msg, ex);
122: throw new JwmaException(msg).setException(ex);
123: }
124: }//prepareJDO
125:
126: /**
127: * Returns a Castor JDO database.
128: *
129: * @return a Castor JDO <tt>Database</tt> instance.
130: */
131: public Database getJDODatabase() throws JwmaException {
132: try {
133: return m_JDO.getDatabase();
134: } catch (Exception ex) {
135: throw new JwmaException("").setException(ex);
136: }
137: }//getJDODatabase
138:
139: /**
140: * Returns the pool of Castor JDO Database
141: * instances.
142: *
143: * @return the pool as <tt>CastorDatabasePool</tt>.
144: */
145: public CastorDatabasePool getDatabasePool() {
146: return m_DBPool;
147: }//getDatabasePool
148:
149: /**
150: * Returns a reference to the singleton instance
151: * of the <tt>CastorHelper</tt>.
152: *
153: * @return the <tt>CastorHelper</tt> singleton.
154: */
155: public static CastorHelper getReference() {
156: if (c_Self != null) {
157: return c_Self;
158: } else {
159: return new CastorHelper();
160: }
161: }//getReference
162:
163: /**
164: * Loads the castor mapping file.
165: *
166: * @param file the url of the mapping file.
167: */
168: private void loadMapping() {
169:
170: log.info("Loading Castor mappings.");
171:
172: ClassLoader cl = this .getClass().getClassLoader();
173: URL mapping = cl
174: .getResource("dtw/webmail/resources/castor_mappings.xml");
175: m_Mapping = new Mapping(cl);
176:
177: try {
178: m_Mapping.loadMapping(mapping);
179: } catch (Exception ex) {
180: log.fatal("loadMappings()", ex);
181: }
182: }//loadMapping
183:
184: }//class CastorHelper
|