KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > mail > factory > JavaMail


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: JavaMail.java,v 1.7 2005/04/28 08:43:25 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.mail.factory;
28
29 //import java
30
import java.io.Serializable JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.Referenceable JavaDoc;
36
37 import org.objectweb.jonas.common.Log;
38 import org.objectweb.jonas.common.PropDump;
39 import org.objectweb.jonas.mail.MailService;
40 import org.objectweb.jonas.mail.MailServiceImpl;
41 import org.objectweb.jonas.service.ServiceManager;
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.api.Logger;
44
45 /**
46  * This class implements JOnAS mail factory objects. It gets the properties from mail factory properties file
47  * and build a properties object for the Session.
48  * @author Florent Benoit
49  * @author Ludovic Bert
50  * Contributor(s):
51  * Adriana Danes :
52  * Refactor code: rename the management methods (use straightforward
53  * names, as they are to be used in a Management Console)
54  * 03/04/01 : JavaMail extends ReconfigDispatcher in order to send reconfiguration Notifications to ReconfigManager
55  *
56  * Markus Karg : Change the JOnAS mail factory initialisation strategy in order
57  * to allow any initialisation parameters for a javax.mail.Session object (not
58  * only a known set pf initialisation parameters as before).
59  *
60  * Adriana Danes :
61  * 03/11/20 : J2EEManagement conformance - replaces old JMail class
62  */

63 public abstract class JavaMail implements Serializable JavaDoc, Referenceable JavaDoc {
64
65     /**
66      * The logger used in JOnAS
67      */

68     private static Logger logger = null;
69
70     /**
71      * The name of the factory.
72      */

73     private String JavaDoc factoryName = null;
74
75     /**
76      * The jndi name of this object.
77      */

78     private String JavaDoc name = null;
79
80     /**
81      * Properties for the javax.mail.Session object.
82      * Keep only the values used for configuring the javax.mail.Session object.
83      */

84     private Properties JavaDoc mailSessionProperties = null;
85
86     /**
87      * Properties for the authentication.
88      */

89     private Properties JavaDoc authenticationProperties = null;
90
91     /**
92      * JOnAS-specific property that authenticates the user name
93      */

94     private static final String JavaDoc PROPERTY_AUTHENTICATION_USERNAME = "mail.authentication.username";
95
96     /**
97      * JOnAS-specific property that authenticates the user password
98      */

99     private static final String JavaDoc PROPERTY_AUTHENTICATION_PASSWORD = "mail.authentication.password";
100
101     /**
102      * Constructor of a JavaMail Object with the given name and properties.
103      * @param factoryName the name of the factory.
104      * @param name the jndi name.
105      * @param mailProperties properties for configuring and manageing this object.
106      */

107     public JavaMail(String JavaDoc factoryName, String JavaDoc name, Properties JavaDoc mailProperties) {
108         //Get the logger
109
if (logger == null) {
110             logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
111         }
112         this.factoryName = factoryName;
113         this.name = name;
114
115         this.mailSessionProperties = (Properties JavaDoc) mailProperties.clone();
116
117         // Retrieve JOnAS specific properties 'type' and 'name' from props
118
mailSessionProperties.remove(MailServiceImpl.PROPERTY_NAME);
119         mailSessionProperties.remove(MailServiceImpl.PROPERTY_TYPE);
120
121         // Construct authentication properties from well-known JOnAS properties only
122
this.authenticationProperties = new Properties JavaDoc();
123         String JavaDoc propValue = null;
124         propValue = (String JavaDoc) mailSessionProperties.remove(PROPERTY_AUTHENTICATION_USERNAME);
125         if (propValue != null) {
126             authenticationProperties.setProperty(PROPERTY_AUTHENTICATION_USERNAME, propValue);
127         }
128         propValue = (String JavaDoc) mailSessionProperties.remove(PROPERTY_AUTHENTICATION_PASSWORD);
129         if (propValue != null) {
130             authenticationProperties.setProperty(PROPERTY_AUTHENTICATION_PASSWORD, propValue);
131         }
132     }
133
134
135     /**
136      * Return the jndi name of this object
137      * @return the jndi name
138      */

139     public String JavaDoc getName() {
140         return name;
141     }
142
143     /**
144      * Set the jndi name of this object.
145      * @param name the jndi name
146      */

147     void setName(String JavaDoc name) {
148         String JavaDoc oldName = this.name;
149         this.name = new String JavaDoc(name);
150         try {
151             ((MailService) ServiceManager.getInstance().getMailService()).renameJavaMailFactory(oldName, this);
152         } catch (Exception JavaDoc e) {
153             // should never occurs at this time
154
}
155     }
156
157     /**
158      * Return the name of this mail factory
159      * @return name of this mail factory.
160      */

161     public String JavaDoc getFactoryName() {
162         return factoryName;
163     }
164
165     /**
166      * Return the type of the factory
167      * @return the type of the mail factory
168      */

169     public abstract String JavaDoc getType();
170
171
172     /**
173      * Retrieves the session properties of this object.
174      * @return the session properties of this object.
175      */

176     Properties JavaDoc getSessionProperties() {
177         return mailSessionProperties;
178     }
179
180     /**
181      * Set the session properties.
182      * @param props the session properties.
183      */

184     void setSessionProperties(Properties JavaDoc props) {
185         this.mailSessionProperties = props;
186         // rebind the mail factory in JNDI
187
try {
188             ((MailService) ServiceManager.getInstance().getMailService()).recreateJavaMailFactory(this);
189         } catch (Exception JavaDoc e) {
190             // should never occurs
191
}
192         //notifyReconfiguration(props);
193
if (logger.isLoggable(BasicLevel.DEBUG)) {
194             PropDump.print("These are the udated session properties", this.mailSessionProperties, logger, BasicLevel.DEBUG);
195         }
196     }
197
198     /**
199      * Retrieves the authentication properties of this object.
200      * @return the authentication properties of this object.
201      */

202     Properties JavaDoc getAuthenticationProperties() {
203         return authenticationProperties;
204     }
205
206     /**
207      * Set the authentication properties.
208      * @param props the authentication properties.
209      */

210     void setAuthenticationProperties(Properties JavaDoc props) {
211         this.authenticationProperties = props;
212         // rebind the mail factory in JNDI
213
try {
214             ((MailService) ServiceManager.getInstance().getMailService()).recreateJavaMailFactory(this);
215         } catch (Exception JavaDoc e) {
216             // should never occurs
217
}
218         //notifyReconfiguration(props);
219
if (logger.isLoggable(BasicLevel.DEBUG)) {
220             PropDump.print("These are the udated auth properties",
221                        this.authenticationProperties, logger, BasicLevel.DEBUG);
222         }
223     }
224
225     /**
226      * Retrieves the Reference of the object.
227      * The Reference contains the factory used to create this object
228      * and the optional parameters used to configure the factory.
229      * @return the non-null Reference of the object.
230      * @throws NamingException if a naming exception was encountered while
231      * retrieving the reference.
232      */

233     public abstract Reference JavaDoc getReference() throws NamingException JavaDoc;
234
235
236     /**
237      * @return Returns the logger.
238      */

239     public static Logger getLogger() {
240         return logger;
241     }
242     /**
243      * @param logger The logger to set.
244      */

245     public static void setLogger(Logger logger) {
246         JavaMail.logger = logger;
247     }
248     /**
249      * @return Returns the mailSessionProperties.
250      */

251     public Properties JavaDoc getMailSessionProperties() {
252         return mailSessionProperties;
253     }
254     /**
255      * @param mailSessionProperties The mailSessionProperties to set.
256      */

257     public void setMailSessionProperties(Properties JavaDoc mailSessionProperties) {
258         this.mailSessionProperties = mailSessionProperties;
259     }
260 }
261
262
Popular Tags