KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > MailConfiguration


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * IASRI 4629057: RI needs modifications to mail configuration to enable
31  * javaMail APIs in iAS7. Change to get mail resources from IAS server.xml
32  * (with server_7_0.dtd).
33  * Added multiple mail resources handling.
34  *
35  * IASRI 4650786: JavaMail should support dynamic reconfiguration.
36  */

37 // START OF IASRI 4629057
38
import java.util.Set JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import com.sun.enterprise.deployment.interfaces.MailResourceIntf;
42 import com.sun.enterprise.repository.ResourceProperty;
43 // END OF IASRI 4629057
44

45 import java.util.logging.*;
46 import com.sun.enterprise.deployment.util.LogDomains;
47
48
49 /**
50  * This class represent the configuration information of the JavaMail
51  * Session object within J2EE.
52  */

53 public class MailConfiguration implements Serializable JavaDoc {
54     /* IASRI 4629057
55     private String username = "";
56     private String mailFrom = "";
57     private String mailHost = "";
58     private static String MAIL_FROM = "mail.from";
59     private static String MAIL_USER = "mail.user";
60     private static String MAIL_HOST = "mail.host";
61     */

62
63     // START OF IASRI 4629057
64
private static String JavaDoc PROTOCOL_TYPE_IMAP = "imap";
65     private static String JavaDoc PROTOCOL_TYPE_POP3 = "pop3";
66     private static String JavaDoc PROTOCOL_TYPE_SMTP = "smtp";
67
68     private static String JavaDoc PROP_NAME_PREFIX = "mail-";
69     private static String JavaDoc PROP_NAME_SUFFIX_HOST = "-host";
70     private static String JavaDoc PROP_NAME_SUFFIX_USER = "-user";
71     private static char PROP_NAME_DELIM = '-';
72     
73     private static String JavaDoc DEF_VAL_STORE_PROTOCOL = PROTOCOL_TYPE_IMAP;
74     private static String JavaDoc DEF_VAL_STORE_PROTOCOL_CLASS =
75         "com.sun.mail.imap.IMAPStore";
76     private static String JavaDoc DEF_VAL_TRANSPORT_PROTOCOL = PROTOCOL_TYPE_SMTP;
77     private static String JavaDoc DEF_VAL_TRANSPORT_PROTOCOL_CLASS =
78         "com.sun.mail.smtp.SMTPTransport";
79     private static String JavaDoc DEF_VAL_HOST = "localhost";
80     private static String JavaDoc DEF_VAL_USER = "user.name";
81     private static String JavaDoc DEF_VAL_FROM = "username@host";
82     private static boolean DEF_VAL_DEBUG = false;
83
84     private static String JavaDoc MAIL_STORE_PROTOCOL = "mail.store.protocol";
85     private static String JavaDoc MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
86     private static String JavaDoc MAIL_HOST = "mail.host";
87     private static String JavaDoc MAIL_USER = "mail.user";
88     private static String JavaDoc MAIL_FROM = "mail.from";
89     private static String JavaDoc MAIL_DEBUG = "mail.debug";
90
91     private static String JavaDoc MAIL_PREFIX = "mail.";
92     private static String JavaDoc MAIL_SUFFIX_CLASS = ".class";
93     private static String JavaDoc MAIL_SUFFIX_HOST = ".host";
94     private static String JavaDoc MAIL_SUFFIX_USER = ".user";
95     private static char MAIL_DELIM = '.';
96
97     /**
98      * Mail resource attributes
99      */

100     private String JavaDoc description = "";
101     private String JavaDoc jndiName = "";
102     private boolean enabled = false;
103     private String JavaDoc storeProtocol = DEF_VAL_STORE_PROTOCOL;
104     private String JavaDoc storeProtocolClass = DEF_VAL_STORE_PROTOCOL_CLASS;
105     private String JavaDoc transportProtocol = DEF_VAL_TRANSPORT_PROTOCOL;
106     private String JavaDoc transportProtocolClass = DEF_VAL_TRANSPORT_PROTOCOL_CLASS;
107     private String JavaDoc mailHost = DEF_VAL_HOST;
108     private String JavaDoc username = DEF_VAL_USER;
109     private String JavaDoc mailFrom = DEF_VAL_FROM;
110     private boolean debug = DEF_VAL_DEBUG;
111
112     private Properties JavaDoc mailProperties = new Properties JavaDoc();
113     // END OF IASRI 4629057
114

115     // Create logger object per Java SDK 1.4 to log messages
116
// introduced Santanu De, Sun Microsystems, March 2002
117

118     static Logger _logger = LogDomains.getLogger(LogDomains.DPL_LOGGER);
119     
120     /**
121      * This constructs the mail configuration based on the username and
122      * the from address. This constructor is deprecated.
123      * @param the username
124      * @param the from address
125      * @deprecated
126      */

127     public MailConfiguration(String JavaDoc username, String JavaDoc mailFrom) {
128     this.username = username;
129     this.mailFrom = mailFrom;
130     this.mailHost = "";
131
132         mailProperties.put(MAIL_FROM, this.getMailFrom());
133         mailProperties.put(MAIL_USER, this.getUsername());
134         mailProperties.put(MAIL_HOST, this.getMailHost());
135     }
136     
137     /**
138      * Construct a specification of mail configuration with the given username,
139      * Mail From Address and mail hostname.
140      * @param the username.
141      * @param the from address.
142      * @param the mail hostname.
143      */

144     public MailConfiguration(String JavaDoc username,
145                  String JavaDoc mailFrom,
146                  String JavaDoc mailHost) {
147     this.username = username;
148     this.mailFrom = mailFrom;
149     this.mailHost = mailHost;
150
151         mailProperties.put(MAIL_FROM, this.getMailFrom());
152         mailProperties.put(MAIL_USER, this.getUsername());
153         mailProperties.put(MAIL_HOST, this.getMailHost());
154     }
155
156     // START OF IASRI 4629057
157
// START OF IASRI 4650786
158
/**
159      * Construct a specification of mail configuration.
160      */

161     public MailConfiguration(MailResourceIntf mailRes) {
162         try {
163             loadMailResources(mailRes);
164         }
165         catch (Exception JavaDoc ce) {
166             //ce.printStackTrace();
167
_logger.log(Level.INFO,"enterprise.deployment_mail_cfgexcp",ce);
168
169         }
170     }
171
172     /**
173      * Load all configuration information from the mail resource node in
174      * server.xml for the JavaMail Session object within J2EE.
175      */

176     private void loadMailResources(MailResourceIntf mailResource) throws Exception JavaDoc {
177
178         if (mailResource == null) {
179             _logger.log(Level.FINE,
180                 "MailConfiguration: no MailResource object. mailResource=" +
181                 mailResource);
182             return;
183         }
184         
185         jndiName = mailResource.getName();
186         description = mailResource.getDescription();
187         enabled = mailResource.isEnabled();
188
189         storeProtocol = mailResource.getStoreProtocol();
190         storeProtocolClass = mailResource.getStoreProtocolClass();
191         transportProtocol = mailResource.getTransportProtocol();
192         transportProtocolClass = mailResource.getTransportProtocolClass();
193         mailHost = mailResource.getMailHost();
194         username = mailResource.getUsername();
195         mailFrom = mailResource.getMailFrom();
196         debug = mailResource.isDebug();
197
198         // Save to Property list
199
String JavaDoc storeProtocolClassName = MAIL_PREFIX + storeProtocol +
200             MAIL_SUFFIX_CLASS;
201         String JavaDoc transportProtocolClassName = MAIL_PREFIX + transportProtocol +
202             MAIL_SUFFIX_CLASS;
203
204         mailProperties.put(MAIL_STORE_PROTOCOL, storeProtocol);
205         mailProperties.put(MAIL_TRANSPORT_PROTOCOL, transportProtocol);
206         mailProperties.put(storeProtocolClassName, storeProtocolClass);
207         mailProperties.put(transportProtocolClassName, transportProtocolClass);
208         mailProperties.put(MAIL_FROM, mailFrom);
209         mailProperties.put(MAIL_DEBUG, (debug ? "true" : "false"));
210
211         // Get the properties and save to Property list
212
Set JavaDoc properties = mailResource.getProperties();
213         ResourceProperty property = null;
214         String JavaDoc name = null;
215         String JavaDoc value = null;
216
217         String JavaDoc protRelatedHostPropName = PROP_NAME_PREFIX + storeProtocol +
218             PROP_NAME_SUFFIX_HOST;
219         String JavaDoc protRelatedUserPropName = PROP_NAME_PREFIX + storeProtocol +
220             PROP_NAME_SUFFIX_USER;
221         String JavaDoc protRelatedHostName = MAIL_PREFIX + storeProtocol +
222                         MAIL_SUFFIX_HOST;
223         String JavaDoc protRelatedUserName = MAIL_PREFIX + storeProtocol +
224                         MAIL_SUFFIX_USER;
225
226         for (Iterator JavaDoc it = properties.iterator(); it.hasNext();) {
227             property = (ResourceProperty)it.next();
228             name = property.getName();
229             value = (String JavaDoc)property.getValue();
230
231             if (name.startsWith(PROP_NAME_PREFIX)) {
232                 if (name.equals(protRelatedHostPropName)) {
233                     mailHost = value;
234                     mailProperties.put(protRelatedHostName, value);
235                 }
236                 else if (name.equals(protRelatedUserPropName)) {
237                     username = value;
238                     mailProperties.put(protRelatedUserName, value);
239                 }
240                 else {
241                     name = name.replace(PROP_NAME_DELIM, MAIL_DELIM);
242                     mailProperties.put(name, value);
243                 }
244             }
245         }
246
247         // Save mail.host and mail.user to Property list
248
mailProperties.put(MAIL_HOST, mailHost);
249         mailProperties.put(MAIL_USER, username);
250     }
251     // END OF IASRI 4650786
252
// END OF IASRI 4629057
253

254     /**
255      * Get the username for the mail session the server will provide.
256      * @return the username.
257      */

258     public String JavaDoc getUsername() {
259     return this.username;
260     }
261     
262     /**
263      * Get the mail from address for the mail session the server will provide.
264      * @return the from address.
265      */

266     public String JavaDoc getMailFrom() {
267     return this.mailFrom;
268     }
269     
270     /**
271      * Get the mail hostname for the mail session the server will provide.
272      * @return the hostname of the mail server.
273      */

274     public String JavaDoc getMailHost() {
275     return this.mailHost;
276     }
277
278     // START OF IASRI 4629057
279
/**
280      * Get the default Message Access Protocol for the mail session the server
281      * will provide.
282      * @return the store protocol of the mail server.
283      */

284     public String JavaDoc getMailStoreProtocol() {
285         return this.storeProtocol;
286     }
287
288     /**
289      * Get the default Transport Protocol for the mail session the server will
290      * provide.
291      * @return the transport protocol of the mail server.
292      */

293     public String JavaDoc getMailTransportProtocol() {
294         return this.transportProtocol;
295     }
296
297     /**
298      * Get the default Message Access Protocol class for the mail session the
299      * server will provide.
300      * @return the store protocol of the mail server.
301      */

302     public String JavaDoc getMailStoreProtocolClass() {
303         return this.storeProtocolClass;
304     }
305
306     /**
307      * Get the default Transport Protocol class for the mail session the server
308      * will provide.
309      * @return the transport protocol of the mail server.
310      */

311     public String JavaDoc getMailTransportProtocolClass() {
312         return this.transportProtocolClass;
313     }
314
315     /**
316      * Get the mail debug flag for the mail session the server will provide.
317      * @return the debug flag of the mail server.
318      */

319     public boolean getMailDebug() {
320         return this.debug;
321     }
322
323     /**
324      * Get the mail description for the mail session the server will provide.
325      * @return the description of the mail server.
326      */

327     public String JavaDoc getDescription() {
328         return this.description;
329     }
330
331     /**
332      * Get the mail JNDI name for the mail session the server will provide.
333      * @return the JNDI name of the mail server.
334      */

335     public String JavaDoc getJndiName() {
336         return this.jndiName;
337     }
338
339     /**
340      * Get the mail enable flag for the mail session the server will provide.
341      * @return the enable flag of the mail server.
342      */

343     public boolean getEnabled() {
344         return this.enabled;
345     }
346     // END OF IASRI 4629057
347

348     /**
349      * Get the mail session properties as per JavaMail.
350      * @return the mail session properties.
351      */

352     public Properties JavaDoc getMailProperties() {
353         /* IASRI 4629057
354         Properties mailProperties = new Properties();
355         mailProperties.put(MAIL_FROM, this.getMailFrom());
356         mailProperties.put(MAIL_USER, this.getUsername());
357         mailProperties.put(MAIL_HOST, this.getMailHost());
358         */

359
360         return mailProperties;
361     }
362     
363     /**
364      * A formatted representation of my state.
365      */

366     public void print(StringBuffer JavaDoc toStringBuffer) {
367     /* IASRI 4629057
368     return "MailConfiguration: [" + username + "," + mailFrom + "," +
369         mailHost + "]";
370     */

371
372         // START OF IASRI 4629057
373
toStringBuffer.append("MailConfiguration: [");
374         toStringBuffer.append("description=").append(description);
375         toStringBuffer.append( ", jndiName=").append(jndiName);
376         toStringBuffer.append( ", enabled=").append(enabled);
377
378         toStringBuffer.append( ", storeProtocol=").append(storeProtocol);
379         toStringBuffer.append( ", transportProtocol=").append(transportProtocol);
380         toStringBuffer.append( ", storeProtocolClass=").append(storeProtocolClass);
381         toStringBuffer.append( ", transportProtocolClass=").append(transportProtocolClass);
382         toStringBuffer.append( ", mailHost=").append(mailHost);
383         toStringBuffer.append( ", username=").append(username);
384         toStringBuffer.append( ", mailFrom=").append(mailFrom);
385         toStringBuffer.append( ", debug=").append(debug);
386         toStringBuffer.append( ", mailProperties: [");
387
388         Enumeration JavaDoc e = mailProperties.propertyNames();
389         String JavaDoc name;
390         String JavaDoc value;
391         boolean isFirst = true;
392
393         while (e.hasMoreElements()) {
394             name = (String JavaDoc)e.nextElement();
395             value = mailProperties.getProperty(name);
396             if (isFirst) {
397                 toStringBuffer.append(name).append("=").append(value);
398                 isFirst = false;
399             }
400             else {
401                 toStringBuffer.append( ", ").append(name).append("=").append(value);
402             }
403         }
404         toStringBuffer.append( "]]");
405
406         // END OF IASRI 4629057
407
}
408 }
409
410
Popular Tags