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.JwmaMailIdentityImpl;
014: import dtw.webmail.model.JwmaPreferencesImpl;
015: import dtw.webmail.plugin.PreferencesPersistencePlugin;
016: import dtw.webmail.util.MD5;
017: import dtw.webmail.util.config.JwmaConfiguration;
018: import dtw.webmail.util.config.PostOffice;
019: import org.apache.log4j.Logger;
020:
021: import java.io.File;
022: import java.io.FileReader;
023: import java.io.FileWriter;
024:
025: /**
026: * A <tt>PreferencesPersistancePlugin</tt> implementation
027: * based on Castor XML marshalling/unmarshalling.
028: * <p>
029: * Stores the preferences as XML files.
030: *
031: * @author Dieter Wimberger
032: * @version 0.9.7 07/02/2003
033: */
034: public class CastorXMLPreferencesPersistence implements
035: PreferencesPersistencePlugin {
036:
037: //logging
038: private static Logger log = Logger
039: .getLogger(CastorXMLPreferencesPersistence.class);
040:
041: //Preferences
042: private String m_PreferencesTemplateFile; //preferences template file
043: private String m_PreferencesMappingFile; //object <-> xml mapping file (Castor)
044: private String m_DataDir;
045: private CastorPreferences m_PreferencesTemplate;
046:
047: public JwmaPreferencesImpl loadPreferences(String identity)
048: throws JwmaException {
049:
050: JwmaPreferencesImpl prefs = null;
051:
052: try {
053: String filename = getFilename(identity);
054: File f = new File(filename);
055: FileReader reader = null;
056:
057: if (f.isAbsolute()) {
058: reader = new FileReader(f);
059: } else {
060: reader = new FileReader(m_DataDir + filename);
061: }
062:
063: prefs = (CastorPreferences) CastorHelper.getReference()
064: .unmarshal(reader);
065:
066: reader.close();
067: } catch (Exception ex) {
068: throw new JwmaException("jwma.plugin.std", true)
069: .setException(ex);
070: }
071: return prefs;
072: }//loadPreferences
073:
074: public void savePreferences(JwmaPreferencesImpl prefs)
075: throws JwmaException {
076:
077: try {
078: String filename = getFilename(prefs.getUserIdentity());
079: FileWriter writer = new FileWriter(m_DataDir + filename);
080: CastorHelper.getReference().marshal(prefs, writer);
081: writer.close();
082: } catch (Exception ex) {
083: throw new JwmaException("").setException(ex);
084: }
085: }//savePreferences
086:
087: public boolean isPersistent(String identity) throws JwmaException {
088: try {
089: File file = new File(m_DataDir + getFilename(identity));
090: return file.exists();
091: } catch (Exception ex) {
092: throw new JwmaException("").setException(ex);
093: }
094: }//isPersistent
095:
096: public JwmaPreferencesImpl getPreferencesTemplate(
097: JwmaSession session) {
098:
099: //1. get clone
100: JwmaPreferencesImpl prefs = m_PreferencesTemplate.getClone();
101:
102: //2. make post office config dependent adjustments:
103: PostOffice po = session.getPostOffice();
104:
105: //set root folder
106: prefs.setRootFolder(po.getRootFolder());
107:
108: //set replyto domain from postoffice
109: String replyto = po.getReplyToDomain();
110: if (replyto == null || replyto.length() == 0) {
111: replyto = po.getAddress();
112: }
113:
114: ((JwmaMailIdentityImpl) prefs.getMailIdentity())
115: .setReplyTo(session.getUsername() + "@" + replyto);
116:
117: return prefs;
118: }//getPreferencesTemplate
119:
120: public void activate() throws JwmaException {
121: try {
122: JwmaKernel kernel = JwmaKernel.getReference();
123: String etc = kernel.getDirectoryPath(JwmaKernel.ETC_DIR);
124:
125: //String m_MappingFile = etc + settings.getMappingFilename();
126: m_PreferencesTemplateFile = etc
127: + JwmaConfiguration.TEMPLATE_FILENAME;
128: m_DataDir = kernel.getDirectoryPath(JwmaKernel.DATA_DIR);
129:
130: //Setup castor helper
131: CastorHelper helper = CastorHelper.getReference();
132:
133: //load mapping
134: //helper.loadMapping(m_MappingFile);
135:
136: //load the template
137: m_PreferencesTemplate = (CastorPreferences) helper
138: .unmarshal(new FileReader(m_PreferencesTemplateFile));
139:
140: log.info(JwmaKernel.getReference().getLogMessage(
141: "jwma.plugin.activation"));
142: } catch (Exception ex) {
143: log.info(JwmaKernel.getReference().getLogMessage(
144: "jwma.plugin.failedactivation"));
145: if (ex instanceof JwmaException) {
146: throw (JwmaException) ex;
147: }
148: throw new JwmaException("").setException(ex);
149: }
150: }//activate
151:
152: public void deactivate() {
153: //nothing important
154: }//deactivate
155:
156: public void setPreferencesTemplate(JwmaPreferencesImpl template)
157: throws JwmaException {
158:
159: //set the reference immediately
160: m_PreferencesTemplate = (CastorPreferences) template;
161: //store
162: try {
163: FileWriter writer = new FileWriter(
164: m_PreferencesTemplateFile);
165: CastorHelper.getReference().marshal(template, writer);
166: writer.close();
167: } catch (Exception ex) {
168: throw new JwmaException("").setException(ex);
169: }
170: }//setPreferencesTemplate
171:
172: private String getFilename(String identity) {
173: return MD5.hash(identity) + ".xml";
174: }//getFileName
175:
176: }//class CastorXMLPreferencesPersistence
|