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.JwmaSession;
012: import dtw.webmail.model.JwmaException;
013: import dtw.webmail.model.JwmaPreferencesImpl;
014: import dtw.webmail.model.JwmaMailIdentityImpl;
015: import dtw.webmail.plugin.PreferencesPersistencePlugin;
016: import dtw.webmail.util.CastorDatabase;
017: import dtw.webmail.util.CastorDatabasePool;
018: import dtw.webmail.util.JwmaSettings;
019: import dtw.webmail.util.config.JwmaConfiguration;
020: import dtw.webmail.util.config.PostOffice;
021: import org.apache.log4j.Logger;
022: import org.exolab.castor.jdo.Query;
023:
024: import java.io.FileReader;
025: import java.io.FileWriter;
026:
027: /**
028: * A <tt>PreferencesPersistancePlugin</tt> implementation
029: * based on Castor JDO.
030: * <p>
031: * Stores the preferences in a database.
032: *
033: * @author Dieter Wimberger
034: * @version 0.9.7 07/02/2003
035: */
036: public class CastorSQLPreferencesPersistence implements
037: PreferencesPersistencePlugin {
038:
039: //logging
040: private static Logger log = Logger
041: .getLogger(CastorSQLPreferencesPersistence.class);
042:
043: private String m_PreferencesTemplateFile;
044: private CastorPreferences m_PreferencesTemplate;
045: private CastorDatabasePool m_DBPool;
046:
047: public JwmaPreferencesImpl loadPreferences(String identity)
048: throws JwmaException {
049:
050: CastorPreferences prefs = null;
051: CastorDatabase db = null;
052: try {
053: db = m_DBPool.leaseDatabase();
054: //Begin transaction
055: db.begin();
056: //get cached query
057: Query oql = db.getQuery(JwmaDatabase.PREFINSTANCE_BYUSERID);
058: //bind param
059: oql.bind(identity);
060: //retrieve prefs instance
061: prefs = (CastorPreferences) oql.execute().next();
062: //end transaction
063: db.commit();
064:
065: } catch (Exception ex) {
066: throw new JwmaException("").setException(ex);
067: } finally {
068: //release the database
069: m_DBPool.releaseDatabase(db);
070: }
071: return prefs;
072: }//loadPreferences
073:
074: public void savePreferences(JwmaPreferencesImpl prefs)
075: throws JwmaException {
076:
077: CastorDatabase db = null;
078: CastorPreferences cprefs = (CastorPreferences) prefs;
079: try {
080: db = m_DBPool.leaseDatabase();
081: // Begin a transaction
082: db.begin();
083:
084: //check if the preferences are not persistent yet.
085: //simplest is if the timestamp is not zero.
086: if (cprefs.jdoGetTimeStamp() != 0) {
087: cprefs.updatePreferences(db.getDatabase());
088: } else {
089: cprefs.persistPreferences(db.getDatabase());
090: }
091: // Commit the transaction
092: db.commit();
093: } catch (Exception ex) {
094: throw new JwmaException("").setException(ex);
095: } finally {
096: m_DBPool.releaseDatabase(db);
097: }
098: }//savePreferences
099:
100: public boolean isPersistent(String identity) throws JwmaException {
101:
102: //FIXME:This is definately not very efficient, but
103: //if ok, and castor caches..hmm
104: if (identity == null || identity.length() == 0) {
105: return false;
106: }
107: CastorDatabase db = null;
108: boolean exists = false;
109: try {
110: db = m_DBPool.leaseDatabase();
111: //Begin transaction
112: db.begin();
113:
114: //get cached query
115: Query oql = db.getQuery(JwmaDatabase.PREFINSTANCE_BYUSERID);
116: //bind param
117: oql.bind(identity);
118:
119: //retrieve prefs instance
120: if (oql.execute().hasMore()) {
121: exists = true;
122: }
123:
124: //end transaction
125: db.commit();
126:
127: } catch (Exception ex) {
128: String msg = JwmaKernel.getReference().getLogMessage(
129: "jwma.plugin.castor.objcheck");
130: log.error(msg, ex);
131: throw new JwmaException(msg).setException(ex);
132: } finally {
133: //release the database
134: m_DBPool.releaseDatabase(db);
135: }
136: return exists;
137: }//isPersistent
138:
139: public JwmaPreferencesImpl getPreferencesTemplate(
140: JwmaSession session) {
141:
142: //1. get clone
143: JwmaPreferencesImpl prefs = m_PreferencesTemplate.getClone();
144:
145: //2. make post office config dependent adjustments:
146: PostOffice po = session.getPostOffice();
147:
148: //set root folder
149: prefs.setRootFolder(po.getRootFolder());
150:
151: //set replyto domain from postoffice
152: String replyto = po.getReplyToDomain();
153: if (replyto == null || replyto.length() == 0) {
154: replyto = po.getAddress();
155: }
156:
157: ((JwmaMailIdentityImpl) prefs.getMailIdentity())
158: .setReplyTo(session.getUsername() + "@" + replyto);
159:
160: return prefs;
161: }//getPreferencesTemplate
162:
163: public void activate() throws JwmaException {
164: JwmaKernel kernel = JwmaKernel.getReference();
165: try {
166: String etc = kernel.getDirectoryPath(JwmaKernel.ETC_DIR);
167:
168: //String m_MappingFile = etc + settings.getMappingFilename();
169: m_PreferencesTemplateFile = etc
170: + JwmaConfiguration.TEMPLATE_FILENAME;
171:
172: //Setup castor helper
173: CastorHelper helper = CastorHelper.getReference();
174:
175: //load mapping
176: //helper.loadMapping(m_MappingFile);
177:
178: //prepare jdo
179: helper.prepareJDO(etc + "database.xml");
180:
181: //set the pool
182: m_DBPool = helper.getDatabasePool();
183:
184: //load the template
185: m_PreferencesTemplate = (CastorPreferences) helper
186: .unmarshal(new FileReader(m_PreferencesTemplateFile));
187:
188: log.info(JwmaKernel.getReference().getLogMessage(
189: "jwma.plugin.activation"));
190: } catch (Exception ex) {
191: log.info(JwmaKernel.getReference().getLogMessage(
192: "jwma.plugin.failedactivation"));
193: throw new JwmaException("").setException(ex);
194: }
195: }//activate
196:
197: public void deactivate() {
198: //FIXME: shut down pool, close down connections?
199: }//deactivate
200:
201: public void setPreferencesTemplate(JwmaPreferencesImpl template)
202: throws JwmaException {
203:
204: //set the reference immediately
205: m_PreferencesTemplate = (CastorPreferences) template;
206: //store
207: try {
208: FileWriter writer = new FileWriter(
209: m_PreferencesTemplateFile);
210: CastorHelper.getReference().marshal(template, writer);
211: writer.close();
212: } catch (Exception ex) {
213: throw new JwmaException("").setException(ex);
214: }
215: }//setPreferencesTemplate
216:
217: }//class CastorSQLPreferencesPersistence
|