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.JwmaContact;
012: import dtw.webmail.model.JwmaContacts;
013: import dtw.webmail.model.JwmaContactsImpl;
014: import dtw.webmail.model.JwmaException;
015: import dtw.webmail.plugin.ContactManagementPlugin;
016: import dtw.webmail.util.CastorDatabasePool;
017: import dtw.webmail.util.JwmaSettings;
018: import org.apache.log4j.Logger;
019:
020: import java.io.*;
021:
022: /**
023: * A <tt>ContactManagementPlugin</tt> implementation
024: * based on Castor XML marshalling/unmarshalling.
025: * <p>
026: * Stores the contacts as XML files.
027: *
028: * @author Dieter Wimberger
029: * @version 0.9.7 07/02/2003
030: */
031: public class CastorXMLContactManagement implements
032: ContactManagementPlugin {
033:
034: //logging
035: private static Logger log = Logger
036: .getLogger(CastorXMLContactManagement.class);
037:
038: private CastorPreferences m_PreferencesTemplate;
039: private CastorDatabasePool m_DBPool;
040: private String m_DataDir;
041:
042: public JwmaContactsImpl loadContacts(String cuid)
043: throws JwmaException {
044:
045: log.debug("loadContacts(" + cuid + ")");
046: CastorContacts cts = null;
047:
048: try {
049:
050: FileReader reader = new FileReader(getFilename(cuid));
051: log.debug("have file reader");
052: cts = (CastorContacts) CastorHelper.getReference()
053: .unmarshal(reader);
054: log.debug("unmarshalled from reader");
055: reader.close();
056: } catch (Exception ex) {
057: log.debug("loadContacts():", ex);
058: if (ex instanceof JwmaException) {
059: throw (JwmaException) ex;
060: } else {
061: throw new JwmaException("jwma.plugin.std", true)
062: .setException(ex);
063: }
064: }
065: return cts;
066: }//loadContacts
067:
068: public void saveContacts(JwmaContactsImpl contacts)
069: throws JwmaException {
070:
071: try {
072: FileWriter writer = new FileWriter(getFilename(contacts
073: .getUID()));
074: CastorHelper.getReference().marshal(contacts, writer);
075: writer.close();
076: } catch (Exception ex) {
077: if (ex instanceof JwmaException) {
078: throw (JwmaException) ex;
079: } else {
080: throw new JwmaException("").setException(ex);
081: }
082: }
083: }//saveContacts
084:
085: public boolean isPersistent(String cuid) throws JwmaException {
086: try {
087: return new File(getFilename(cuid)).exists();
088: } catch (Exception ex) {
089: throw new JwmaException("").setException(ex);
090: }
091: }//isPersistent
092:
093: public JwmaContactsImpl createContacts() {
094: return new CastorContacts();
095: }//createContacts
096:
097: public void activate() throws JwmaException {
098: JwmaKernel kernel = JwmaKernel.getReference();
099: try {
100: String etc = kernel.getDirectoryPath(JwmaKernel.ETC_DIR);
101:
102: //String m_MappingFile = etc + settings.getMappingFilename();
103:
104: m_DataDir = kernel.getDirectoryPath(JwmaKernel.DATA_DIR);
105:
106: //Setup castor helper
107: CastorHelper helper = CastorHelper.getReference();
108:
109: //load mapping
110: //helper.loadMapping(m_MappingFile);
111:
112: log.info(JwmaKernel.getReference().getLogMessage(
113: "jwma.plugin.activation"));
114: } catch (Exception ex) {
115: log.info(JwmaKernel.getReference().getLogMessage(
116: "jwma.plugin.failedactivation"));
117: throw new JwmaException("").setException(ex);
118: }
119: }//activate
120:
121: public void deactivate() {
122: //FIXME: shut down pool, close down connections?
123: }//deactivate
124:
125: public String exportContact(JwmaContact contact, String TYPE)
126: throws JwmaException {
127: return "";
128: /*
129: if(TYPE.equals(TYPE_VCARD3)) {
130: ByteArrayOutputStream bout=new ByteArrayOutputStream();
131:
132: m_vCardMarshaller.marshallContact(bout,((JpimContact)contact).getContact());
133: return new String(bout.toByteArray());
134: } else {
135: return "";
136: }
137: */
138: }//export
139:
140: public JwmaContact importContact(InputStream in, String TYPE)
141: throws JwmaException {
142: return null;
143: /*
144: if(TYPE.equals(TYPE_VCARD3) || TYPE.equals(TYPE_VCARD2)) {
145: try {
146: return new JpimContact(
147: new vCardUnmarshaller()
148: .unmarshallContact(in)
149: );
150: } catch (Exception ex) {
151: log.error("importContact():",ex);
152: return null;
153: }
154: } else {
155: return null;
156: }
157: */
158: }//importContact
159:
160: public JwmaContact[] importDatabase(InputStream in, String TYPE)
161: throws JwmaException {
162: return null;
163: }//importDatabase
164:
165: public void exportDatabase(OutputStream out, JwmaContacts db,
166: String TYPE) throws JwmaException {
167: return;
168: }//exportDatabase
169:
170: public String[] getSupportedTypes(int IMEX_TYPE) {
171: switch (IMEX_TYPE) {
172: case CONTACT_IMPORT:
173: return CONTACT_IMPORT_TYPES;
174: case CONTACT_EXPORT:
175: return CONTACT_EXPORT_TYPES;
176: case DATABASE_EXPORT:
177: return DATABASE_EXPORT_TYPES;
178: case DATABASE_IMPORT:
179: return DATABASE_IMPORT_TYPES;
180: default:
181: return NO_TYPES;
182: }
183: }//getSupportedTypes
184:
185: public boolean isSupportedContactImportType(String type) {
186: for (int i = 0; i < CONTACT_IMPORT_TYPES.length; i++) {
187: if (type.equalsIgnoreCase(CONTACT_IMPORT_TYPES[i])) {
188: return true;
189: }
190: }
191: return false;
192: }//isSupportedContactImportType
193:
194: private String getFilename(String uid) {
195: return m_DataDir + uid + ".xml";
196: }//getFileName
197:
198: public static final String TYPE_VCARD3 = "text/directory";
199: public static final String TYPE_VCARD2 = "application/directory";
200: private static final String[] NO_TYPES = new String[0];
201:
202: private static final String[] CONTACT_IMPORT_TYPES = NO_TYPES;
203: private static final String[] CONTACT_EXPORT_TYPES = NO_TYPES;
204: private static final String[] DATABASE_EXPORT_TYPES = NO_TYPES;
205: private static final String[] DATABASE_IMPORT_TYPES = NO_TYPES;
206:
207: }//class CastorContactManagement
|