KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > mail > servlet > MailServerConfig


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: MailServerConfig.java,v 1.7 2006/04/02 00:34:25 wfro Exp $
5  * Description: openCRX EMailImporter
6  * Revision: $Revision: 1.7 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/04/02 00:34:25 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004-2006, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.mail.servlet;
57
58 import java.util.Iterator JavaDoc;
59
60 import javax.servlet.ServletContext JavaDoc;
61
62 import org.opencrx.kernel.workflow1.cci.ExternalTask;
63 import org.openmdx.application.log.AppLog;
64 import org.openmdx.base.accessor.jmi.cci.JmiServiceException;
65 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0;
66 import org.openmdx.compatibility.base.naming.Path;
67 import org.openmdx.kernel.exception.BasicException;
68
69 /**
70  * Class holding the configuration parameters needed for establishing
71  * a connection to a mail server and reading email messages from the server.
72  * For simple access the attributes are not private and hence can be accessed
73  * directly, although the attributes are final to avoid inconsistencies after
74  * using the configuration for a mail server connection
75  */

76 public class MailServerConfig {
77
78     //-----------------------------------------------------------------------
79
/**
80      * Try to read the configuration for the mail server hosting the emails
81      * to be imported from the openCRX server. The config is stored specifically
82      * for provider / segment combinations.
83      *
84      * @param providerName The name of the current provider
85      * @param segmentName The name of the current segment
86      * @return The mail server configuration according to
87      * provider and segment
88      */

89     public MailServerConfig(
90         RefPackage_1_0 rootPkg,
91         String JavaDoc providerName,
92         String JavaDoc segmentName,
93         ServletContext JavaDoc context
94     ) {
95         String JavaDoc server = null;
96         String JavaDoc account = null;
97         String JavaDoc password = null;
98         Integer JavaDoc port = null;
99         String JavaDoc protocol = null;
100         String JavaDoc mailbox = null;
101         Boolean JavaDoc sslRequired = null;
102         Boolean JavaDoc deleteImportedMessages = null;
103         Boolean JavaDoc debug = null;
104         try {
105             Path taskIdentity = new Path(
106                 "xri:@openmdx:org.opencrx.kernel.workflow1/provider/" + providerName + "/segment/" + segmentName + "/wfProcess/" + TASK_ID
107             );
108             ExternalTask task = null;
109             try {
110                 task = (ExternalTask)rootPkg.refObject(taskIdentity.toXri());
111             }
112             catch(JmiServiceException e) {
113                 // Create MailImporter task and default configuration properties
114
if(e.getExceptionCode() != BasicException.Code.NOT_FOUND) {
115                     throw e;
116                 }
117                 try {
118                     org.opencrx.kernel.workflow1.cci.workflow1Package workflowPkg = getWorkflowPkg(rootPkg);
119                     org.opencrx.kernel.base.cci.basePackage basePkg = getBasePkg(rootPkg);
120                     org.opencrx.kernel.workflow1.cci.Segment workflowSegment =
121                         (org.opencrx.kernel.workflow1.cci.Segment)rootPkg.refObject(
122                             taskIdentity.getPrefix(taskIdentity.size() - 2).toXri()
123                         );
124                     // Create mail importer task
125
task = workflowPkg.getExternalTaskClass().createExternalTask();
126                     task.setName(MailImporterServlet.class.getName());
127                     task.setDescription("Mail importer. To override the default property create a new property without suffix .Default");
128                     workflowPkg.refBegin();
129                     workflowSegment.addWfProcess(
130                         TASK_ID,
131                         task
132                     );
133                     // server.Default
134
org.opencrx.kernel.base.cci.StringProperty sp = basePkg.getStringPropertyClass().createStringProperty();
135                     sp.setName(MailServerConfig.OPTION_MAIL_SERVER + ".Default");
136                     sp.setDescription("Incoming mail server");
137                     sp.setStringValue("mail.changeme.com");
138                     task.addProperty(sp);
139                     // account.Default
140
sp = basePkg.getStringPropertyClass().createStringProperty();
141                     sp.setName(MailServerConfig.OPTION_MAIL_ACCOUNT + ".Default");
142                     sp.setDescription("Account name");
143                     sp.setStringValue("account");
144                     task.addProperty(sp);
145                     // password.Default
146
sp = basePkg.getStringPropertyClass().createStringProperty();
147                     sp.setName(MailServerConfig.OPTION_MAIL_PASSWORD + ".Default");
148                     sp.setDescription("Account password");
149                     sp.setStringValue("password");
150                     task.addProperty(sp);
151                     // port.Default
152
org.opencrx.kernel.base.cci.IntegerProperty ip = basePkg.getIntegerPropertyClass().createIntegerProperty();
153                     ip.setName(MailServerConfig.OPTION_MAIL_SERVER_PORT + ".Default");
154                     ip.setDescription("Server port number");
155                     ip.setIntegerValue(110);
156                     task.addProperty(ip);
157                     // protocol.Default
158
sp = basePkg.getStringPropertyClass().createStringProperty();
159                     sp.setName(MailServerConfig.OPTION_MAIL_PROTOCOL + ".Default");
160                     sp.setDescription("Mail protocol");
161                     sp.setStringValue("pop3");
162                     task.addProperty(sp);
163                     // mailbox
164
sp = basePkg.getStringPropertyClass().createStringProperty();
165                     sp.setName(MailServerConfig.OPTION_MAIL_BOX + ".Default");
166                     sp.setDescription("Mailbox name");
167                     sp.setStringValue("INBOX");
168                     task.addProperty(sp);
169                     // sslRequired.Default
170
org.opencrx.kernel.base.cci.BooleanProperty bp = basePkg.getBooleanPropertyClass().createBooleanProperty();
171                     bp.setName(MailServerConfig.OPTION_MAIL_SSL_REQUIRED + ".Default");
172                     bp.setDescription("Server requires SSL");
173                     bp.setBooleanValue(false);
174                     task.addProperty(bp);
175                     // deleteImportedMessages.Default
176
bp = basePkg.getBooleanPropertyClass().createBooleanProperty();
177                     bp.setName(MailServerConfig.OPTION_MAIL_DELETE_IMPORTED_MESSAGES + ".Default");
178                     bp.setDescription("Delete imported messages");
179                     bp.setBooleanValue(false);
180                     task.addProperty(bp);
181                     // debug.Default
182
bp = basePkg.getBooleanPropertyClass().createBooleanProperty();
183                     bp.setName(MailServerConfig.OPTION_MAIL_DEBUG + ".Default");
184                     bp.setDescription("Enable debugging output");
185                     bp.setBooleanValue(true);
186                     task.addProperty(bp);
187                     workflowPkg.refCommit();
188                 }
189                 catch(JmiServiceException e0) {
190                     AppLog.info("Can not create default configuration", e0.getMessage());
191                     throw e0;
192                 }
193             }
194             // Get task configuration
195
for(
196                 int i = 0;
197                 i < 2;
198                 i++
199             ) {
200                 String JavaDoc suffix = new String JavaDoc[]{".Default", ""}[i];
201                 for(
202                     Iterator JavaDoc j = task.getProperty().iterator();
203                     j.hasNext();
204                 ) {
205                     org.opencrx.kernel.base.cci.Property property = (org.opencrx.kernel.base.cci.Property)j.next();
206                     if((MailServerConfig.OPTION_MAIL_SERVER + suffix).equals(property.getName())) {
207                         server = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue();
208                     }
209                     else if((MailServerConfig.OPTION_MAIL_ACCOUNT + suffix).equals(property.getName())) {
210                         account = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue();
211                     }
212                     else if((MailServerConfig.OPTION_MAIL_PASSWORD + suffix).equals(property.getName())) {
213                         password = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue();
214                     }
215                     else if((MailServerConfig.OPTION_MAIL_SERVER_PORT + suffix).equals(property.getName())) {
216                         port = new Integer JavaDoc(((org.opencrx.kernel.base.cci.IntegerProperty)property).getIntegerValue());
217                     }
218                     else if((MailServerConfig.OPTION_MAIL_PROTOCOL + suffix).equals(property.getName())) {
219                         protocol = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue();
220                     }
221                     else if((MailServerConfig.OPTION_MAIL_BOX + suffix).equals(property.getName())) {
222                         mailbox = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue();
223                     }
224                     else if((MailServerConfig.OPTION_MAIL_SSL_REQUIRED + suffix).equals(property.getName())) {
225                         sslRequired = new Boolean JavaDoc(((org.opencrx.kernel.base.cci.BooleanProperty)property).isBooleanValue());
226                     }
227                     else if((MailServerConfig.OPTION_MAIL_DELETE_IMPORTED_MESSAGES + suffix).equals(property.getName())) {
228                         deleteImportedMessages = new Boolean JavaDoc(((org.opencrx.kernel.base.cci.BooleanProperty)property).isBooleanValue());
229                     }
230                     else if((MailServerConfig.OPTION_MAIL_DEBUG + suffix).equals(property.getName())) {
231                         debug = new Boolean JavaDoc(((org.opencrx.kernel.base.cci.BooleanProperty)property).isBooleanValue());
232                     }
233                 }
234             }
235         }
236         catch(Exception JavaDoc e) {
237             AppLog.info("Can not retrieve config from external task configuration", e.getMessage());
238         }
239         // Fallback to servlet config
240
this.mailServer = server == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_SERVER) : server;
241         this.mailAccount = account == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_ACCOUNT) : account;
242         this.mailPassword = password == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_PASSWORD) : password;
243         this.mailPort = port == null ? Integer.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_SERVER_PORT)) : port;
244         protocol = protocol == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_PROTOCOL) : protocol;
245         if(
246             (protocol == null) ||
247             (!MailStore.POP3_MODE.equalsIgnoreCase(protocol) && !MailStore.IMAP_MODE.equalsIgnoreCase(protocol))
248         ) {
249             this.mailProtocol = MailStore.POP3_MODE;
250         }
251         else {
252             this.mailProtocol = protocol;
253         }
254         this.mailbox = mailbox == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_BOX) : mailbox;
255         this.isSslRequired = sslRequired == null ? Boolean.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_SSL_REQUIRED)).booleanValue() : sslRequired.booleanValue();
256         this.deleteImportedMessages = deleteImportedMessages == null ? Boolean.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_DELETE_IMPORTED_MESSAGES)).booleanValue() : deleteImportedMessages.booleanValue();
257         this.debugMode = debug == null ? Boolean.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_DEBUG)).booleanValue() : debug.booleanValue();
258     }
259     
260     //-----------------------------------------------------------------------
261
protected static org.opencrx.kernel.base.cci.basePackage getBasePkg(
262         RefPackage_1_0 rootPkg
263     ) {
264         return (org.opencrx.kernel.base.cci.basePackage)rootPkg.refPackage(
265             org.opencrx.kernel.base.cci.basePackage.class.getName()
266         );
267     }
268
269     //-----------------------------------------------------------------------
270
protected static org.opencrx.kernel.workflow1.cci.workflow1Package getWorkflowPkg(
271         RefPackage_1_0 rootPkg
272     ) {
273         return (org.opencrx.kernel.workflow1.cci.workflow1Package)rootPkg.refPackage(
274             org.opencrx.kernel.workflow1.cci.workflow1Package.class.getName()
275         );
276     }
277         
278     //-----------------------------------------------------------------------
279
/* (non-Javadoc)
280    * @see java.lang.Object#toString()
281    */

282   public String JavaDoc toString(
283   ) {
284     StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
285     tmp.append("EMailServerConfiguration{");
286     tmp.append("mailServer='").append(mailServer).append("', ");
287     tmp.append("mailProtocol='").append(mailProtocol).append("', ");
288     tmp.append("mailPort='").append(mailPort).append("', ");
289     tmp.append("debugMode='").append(debugMode).append("', ");
290     tmp.append("isSslRequired='").append(isSslRequired).append("', ");
291     tmp.append("deleteImportedMessages='").append(this.deleteImportedMessages).append("', ");
292     tmp.append("mailAccount='").append(mailAccount).append("'}");
293     return tmp.toString();
294   }
295
296   //-------------------------------------------------------------------------
297
public String JavaDoc getMailServer(
298   ) {
299       return this.mailServer;
300   }
301   
302   //-------------------------------------------------------------------------
303
public String JavaDoc getMailAccount(
304   ) {
305       return this.mailAccount;
306   }
307
308   //-------------------------------------------------------------------------
309
public String JavaDoc getMailPassword(
310   ) {
311       return this.mailPassword;
312   }
313   
314   //-------------------------------------------------------------------------
315
public Integer JavaDoc getMailPort(
316   ) {
317       return this.mailPort;
318   }
319   
320   //-------------------------------------------------------------------------
321
public String JavaDoc getMailbox(
322   ) {
323       return this.mailbox;
324   }
325   
326   //-------------------------------------------------------------------------
327
public boolean deleteImportedMessages(
328   ) {
329       return this.deleteImportedMessages;
330   }
331   
332   //-------------------------------------------------------------------------
333
private static final String JavaDoc TASK_ID = "MailImporter";
334     private static final String JavaDoc OPTION_MAIL_DEBUG = "debug";
335     private static final String JavaDoc OPTION_MAIL_SSL_REQUIRED = "sslRequired";
336     private static final String JavaDoc OPTION_MAIL_BOX = "mailbox";
337     private static final String JavaDoc OPTION_MAIL_PROTOCOL = "protocol";
338     private static final String JavaDoc OPTION_MAIL_SERVER_PORT = "port";
339     private static final String JavaDoc OPTION_MAIL_PASSWORD = "password";
340     private static final String JavaDoc OPTION_MAIL_ACCOUNT = "account";
341     private static final String JavaDoc OPTION_MAIL_SERVER = "server";
342     private static final String JavaDoc OPTION_MAIL_DELETE_IMPORTED_MESSAGES = "deleteImportedMessages";
343     
344     /* the mail server */
345     protected final String JavaDoc mailServer;
346     /* the mail account */
347     protected final String JavaDoc mailAccount;
348     /* password for the mail account, must not be encrypted */
349     protected final String JavaDoc mailPassword;
350     /* the port of the mail server supporting the specified protocol */
351     protected final Integer JavaDoc mailPort;
352     /* the mail protocol, either "pop3" or "imap" */
353     protected final String JavaDoc mailProtocol;
354     /* the mailbox */
355     protected final String JavaDoc mailbox;
356     /* enables/disables debugging facility of the javamail implementation */
357     protected final boolean debugMode;
358     /* enables/disables the use of SSL for the mail transfer */
359     protected final boolean isSslRequired;
360     /* delete imported messages if set to true */
361     protected final boolean deleteImportedMessages;
362   
363 }
364
Popular Tags