KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mail > MailGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.mail;
18
19 import javax.mail.Authenticator JavaDoc;
20 import javax.mail.Session JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.apache.geronimo.gbean.GBeanInfo;
29 import org.apache.geronimo.gbean.GBeanInfoBuilder;
30 import org.apache.geronimo.gbean.GBeanLifecycle;
31 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
32 import org.apache.geronimo.management.JavaMailResource;
33
34
35 /**
36  * GBean that provides access to JavaMail Sessions.
37  * <p/>
38  * This GBean is used to generate JavaMail Sessions. JavaMail properties that
39  * are common to all JavaMail Sessions are provided via member variables of this
40  * class.
41  *
42  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
43  * @see ProtocolGBean
44  * @see SMTPTransportGBean
45  * @see POP3StoreGBean
46  * @see IMAPStoreGBean
47  */

48 public class MailGBean implements GBeanLifecycle, JavaMailResource {
49
50     private final Log log = LogFactory.getLog(MailGBean.class);
51
52     private final String JavaDoc objectName;
53     private final Collection JavaDoc protocols;
54     private Boolean JavaDoc useDefault;
55     private Properties JavaDoc properties;
56     private Authenticator JavaDoc authenticator;
57     private String JavaDoc storeProtocol;
58     private String JavaDoc transportProtocol;
59     private String JavaDoc host;
60     private String JavaDoc user;
61     private Boolean JavaDoc debug;
62
63
64     /**
65      * Construct an instance of MailGBean
66      * <p/>
67      * Values that are set in the individual member variables will override any of
68      * the corresponding values that have been set in the properties set.
69      *
70      * @param protocols the set of protocol GBeans that contain protocol specific configurations
71      * @param useDefault whether this GBean will return default Sessions or not
72      * @param properties the set of default properties for the protocols
73      * @param authenticator the authenticator object
74      * @param storeProtocol the store protocol that Sessions created from this GBean will return
75      * @param transportProtocol the transport protocol that Sessions created from this GBean will return
76      * @param host the default Mail server
77      * @param user the username to provide when connecting to a Mail server
78      * @param debug the debug setting for Sessions created from this GBean
79      */

80     public MailGBean(String JavaDoc objectName, Collection JavaDoc protocols, Boolean JavaDoc useDefault, Properties JavaDoc properties, Authenticator JavaDoc authenticator,
81                      String JavaDoc storeProtocol, String JavaDoc transportProtocol, String JavaDoc host, String JavaDoc user, Boolean JavaDoc debug) {
82         this.objectName = objectName;
83         this.protocols = protocols;
84         setUseDefault(useDefault);
85         this.properties = (properties == null ? new Properties JavaDoc() : properties);
86         setAuthenticator(authenticator);
87         setStoreProtocol(storeProtocol);
88         setTransportProtocol(transportProtocol);
89         setHost(host);
90         setUser(user);
91         setDebug(debug);
92
93     }
94
95     /**
96      * Returns the set of protocol GBeans that contain protocol specific configurations.
97      */

98     public Collection JavaDoc getProtocols() {
99         return protocols;
100     }
101
102     /**
103      * Returns whether this GBean will return default Sessions or not.
104      */

105     public Boolean JavaDoc getUseDefault() {
106         return useDefault;
107     }
108
109     /**
110      * Sets whether this GBean will return default Sessions or not,
111      *
112      * @param useDefault whether this GBean will return default Sessions or not
113      */

114     public void setUseDefault(Boolean JavaDoc useDefault) {
115         this.useDefault = useDefault;
116     }
117
118     /**
119      * Returns the set of default properties for the protocols.
120      * <p/>
121      * Note: Proerties that are set here will override the properties that are
122      * set in the protocol GBeans.
123      */

124     public Properties JavaDoc getProperties() {
125         return properties;
126     }
127
128     /**
129      * Sets the set of default properties for the protocols.
130      * <p/>
131      * Note: Proerties that are set here will override the properties that are
132      * set in the protocol GBeans.
133      *
134      * @param properties the set of default properties for the protocols
135      */

136     public void setProperties(Properties JavaDoc properties) {
137         this.properties = properties;
138     }
139
140     /**
141      * Returns the authenticator object.
142      * <p/>
143      * Used only if a new Session object is created. Otherwise, it must match
144      * the Authenticator used to create the Session.
145      */

146     public Authenticator JavaDoc getAuthenticator() {
147         return authenticator;
148     }
149
150     /**
151      * Sets the authenticator object.
152      * <p/>
153      * Used only if a new Session object is created. Otherwise, it must match
154      * the Authenticator used to create the Session.
155      *
156      * @param authenticator the authenticator object
157      */

158     public void setAuthenticator(Authenticator JavaDoc authenticator) {
159         this.authenticator = authenticator;
160     }
161
162     /**
163      * Returns the store protocol that Sessions created from this GBean will return.
164      * <p/>
165      * Specifies the default Message Access Protocol. The Session.getStore()
166      * method returns a Store object that implements this protocol. The client
167      * can override this property and explicitly specify the protocol with the
168      * Session.getStore(String protocol) method.
169      */

170     public String JavaDoc getStoreProtocol() {
171         return storeProtocol;
172     }
173
174     /**
175      * Sets the store protocol that Sessions created from this GBean will return.
176      * <p/>
177      * Specifies the default Message Access Protocol. The Session.getStore()
178      * method returns a Store object that implements this protocol. The client
179      * can override this property and explicitly specify the protocol with the
180      * Session.getStore(String protocol) method.
181      * <p/>
182      * Values that are set here will override any of the corresponding value
183      * that has been set in the properties.
184      *
185      * @param storeProtocol the store protocol that Sessions created from this GBean will return
186      */

187     public void setStoreProtocol(String JavaDoc storeProtocol) {
188         this.storeProtocol = storeProtocol;
189     }
190
191     /**
192      * Returns the transport protocol that Sessions created from this GBean will return.
193      * <p/>
194      * Specifies the default Transport Protocol. The Session.getTransport()
195      * method returns a Transport object that implements this protocol. The
196      * client can override this property and explicitly specify the protocol
197      * by using Session.getTransport(String protocol) method.
198      */

199     public String JavaDoc getTransportProtocol() {
200         return transportProtocol;
201     }
202
203     /**
204      * Sets the transport protocol that Sessions created from this GBean will return.
205      * <p/>
206      * Specifies the default Transport Protocol. The Session.getTransport()
207      * method returns a Transport object that implements this protocol. The
208      * client can override this property and explicitly specify the protocol
209      * by using Session.getTransport(String protocol) method.
210      * <p/>
211      * Values that are set here will override any of the corresponding value
212      * that has been set in the properties.
213      *
214      * @param transportProtocol the transport protocol that Sessions created from this GBean will return
215      */

216     public void setTransportProtocol(String JavaDoc transportProtocol) {
217         this.transportProtocol = transportProtocol;
218     }
219
220     /**
221      * Returns the default Mail server.
222      * <p/>
223      * Specifies the default Mail server. The Store and Transport object’s
224      * connect methods use this property, if the protocolspecific host property
225      * is absent, to locate the target host.
226      */

227     public String JavaDoc getHost() {
228         return host;
229     }
230
231     /**
232      * Sets the default Mail server.
233      * <p/>
234      * Specifies the default Mail server. The Store and Transport object’s
235      * connect methods use this property, if the protocolspecific host property
236      * is absent, to locate the target host.
237      * <p/>
238      * Values that are set here will override any of the corresponding value
239      * that has been set in the properties.
240      *
241      * @param host the default Mail server
242      */

243     public void setHost(String JavaDoc host) {
244         this.host = host;
245     }
246
247     /**
248      * Returns the username to provide when connecting to a Mail server.
249      * <p/>
250      * Specifies the username to provide when connecting to a Mail server. The
251      * Store and Transport object’s connect methods use this property, if the
252      * protocolspecific username property is absent, to obtain the username.
253      */

254     public String JavaDoc getUser() {
255         return user;
256     }
257
258     /**
259      * Sets the username to provide when connecting to a Mail server.
260      * <p/>
261      * Specifies the username to provide when connecting to a Mail server. The
262      * Store and Transport object’s connect methods use this property, if the
263      * protocolspecific username property is absent, to obtain the username.
264      * <p/>
265      * Values that are set here will override any of the corresponding value
266      * that has been set in the properties.
267      *
268      * @param user the username to provide when connecting to a Mail server
269      */

270     public void setUser(String JavaDoc user) {
271         this.user = user;
272     }
273
274     /**
275      * Returns the debug setting for Sessions created from this GBean.
276      */

277     public Boolean JavaDoc getDebug() {
278         return debug;
279     }
280
281     /**
282      * Sets the debug setting for Sessions created from this GBean.
283      * <p/>
284      * Values that are set here will override any of the corresponding value
285      * that has been set in the properties.
286      *
287      * @param debug the debug setting for Sessions created from this GBean
288      */

289     public void setDebug(Boolean JavaDoc debug) {
290         this.debug = debug;
291     }
292
293     public Object JavaDoc $getResource() {
294         Properties JavaDoc props = new Properties JavaDoc(properties);
295
296         if (protocols != null) {
297             for (Iterator JavaDoc iter = protocols.iterator(); iter.hasNext();) {
298                 ProtocolGBean protocol = (ProtocolGBean) iter.next();
299                 protocol.addOverrides(props);
300             }
301         }
302
303         props.putAll(properties);
304
305         if (storeProtocol != null) props.put("mail.store.protocol", storeProtocol);
306         if (transportProtocol != null) props.put("mail.transport.protocol", transportProtocol);
307         if (host != null) props.put("mail.host", host);
308         if (user != null) props.put("mail.user", user);
309         // this needs to be translated into a string version.
310
if (debug != null) props.put("mail.debug", debug.toString());
311
312         if (Boolean.TRUE.equals(useDefault)) {
313             if (authenticator == null) {
314                 return Session.getDefaultInstance(props);
315             } else {
316                 return Session.getDefaultInstance(props, authenticator);
317             }
318         } else {
319             if (authenticator == null) {
320                 return Session.getInstance(props);
321             } else {
322                 return Session.getInstance(props, authenticator);
323             }
324         }
325     }
326
327     public void doStart() throws Exception JavaDoc {
328         log.debug("Started " + objectName + " - will return "
329                  + (Boolean.TRUE.equals(useDefault) ? "default" : "new")
330                  + " JavaMail Session "
331                  + (authenticator == null ? "without" : "with")
332                  + " authenticator");
333     }
334
335     public void doStop() throws Exception JavaDoc {
336         log.debug("Stopped " + objectName);
337     }
338
339     public void doFail() {
340         log.warn("Failed " + objectName);
341     }
342
343     /**
344      * Returns the GBean name of this Mail GBean
345      */

346     public String JavaDoc getObjectName() {
347         return objectName;
348     }
349
350     public boolean isStateManageable() {
351         return false;
352     }
353
354     public boolean isStatisticsProvider() {
355         return false;
356     }
357
358     public boolean isEventProvider() {
359         return false;
360     }
361
362     public static final GBeanInfo GBEAN_INFO;
363
364     static {
365         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(MailGBean.class, NameFactory.JAVA_MAIL_RESOURCE);
366
367         infoFactory.addAttribute("objectName", String JavaDoc.class, false);
368         infoFactory.addReference("Protocols", ProtocolGBean.class, NameFactory.GERONIMO_SERVICE);
369         infoFactory.addAttribute("useDefault", Boolean JavaDoc.class, true);
370         infoFactory.addAttribute("properties", Properties JavaDoc.class, true);
371         infoFactory.addReference("Authenticator", Authenticator JavaDoc.class, NameFactory.GERONIMO_SERVICE);
372         infoFactory.addAttribute("storeProtocol", String JavaDoc.class, true);
373         infoFactory.addAttribute("transportProtocol", String JavaDoc.class, true);
374         infoFactory.addAttribute("host", String JavaDoc.class, true);
375         infoFactory.addAttribute("user", String JavaDoc.class, true);
376         infoFactory.addAttribute("debug", Boolean JavaDoc.class, true);
377         infoFactory.addOperation("$getResource");
378         infoFactory.addOperation("getProtocols");
379         infoFactory.addInterface(JavaMailResource.class);
380
381         infoFactory.setConstructor(new String JavaDoc[]{"objectName",
382                                                 "Protocols",
383                                                 "useDefault",
384                                                 "properties",
385                                                 "Authenticator",
386                                                 "storeProtocol",
387                                                 "transportProtocol",
388                                                 "host",
389                                                 "user",
390                                                 "debug"});
391
392         GBEAN_INFO = infoFactory.getBeanInfo();
393     }
394
395     public static GBeanInfo getGBeanInfo() {
396         return GBEAN_INFO;
397     }
398 }
399
Popular Tags