1 /**
2 *
3 * Copyright 2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
20 import javax.mail.Session;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.Properties;
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: 399081 $ $Date: 2006-05-02 16:21:29 -0700 (Tue, 02 May 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 objectName;
53 private final Collection protocols;
54 private Boolean useDefault;
55 private Properties properties;
56 private Authenticator authenticator;
57 private String storeProtocol;
58 private String transportProtocol;
59 private String host;
60 private String user;
61 private Boolean 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 objectName, Collection protocols, Boolean useDefault, Properties properties, Authenticator authenticator,
81 String storeProtocol, String transportProtocol, String host, String user, Boolean debug) {
82 this.objectName = objectName;
83 this.protocols = protocols;
84 setUseDefault(useDefault);
85 this.properties = (properties == null ? new Properties() : 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 getProtocols() {
99 return protocols;
100 }
101
102 /**
103 * Returns whether this GBean will return default Sessions or not.
104 */
105 public Boolean 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 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 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 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 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 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 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 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 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 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 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 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 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 user) {
271 this.user = user;
272 }
273
274 /**
275 * Returns the debug setting for Sessions created from this GBean.
276 */
277 public Boolean 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 debug) {
290 this.debug = debug;
291 }
292
293 public Object $getResource() {
294 Properties props = new Properties(properties);
295
296 if (protocols != null) {
297 for (Iterator 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
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 {
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 {
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 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.class, false);
368 infoFactory.addReference("Protocols", ProtocolGBean.class, NameFactory.GERONIMO_SERVICE);
369 infoFactory.addAttribute("useDefault", Boolean.class, true);
370 infoFactory.addAttribute("properties", Properties.class, true);
371 infoFactory.addReference("Authenticator", Authenticator.class, NameFactory.GERONIMO_SERVICE);
372 infoFactory.addAttribute("storeProtocol", String.class, true);
373 infoFactory.addAttribute("transportProtocol", String.class, true);
374 infoFactory.addAttribute("host", String.class, true);
375 infoFactory.addAttribute("user", String.class, true);
376 infoFactory.addAttribute("debug", Boolean.class, true);
377 infoFactory.addOperation("$getResource");
378 infoFactory.addOperation("getProtocols");
379 infoFactory.addInterface(JavaMailResource.class);
380
381 infoFactory.setConstructor(new String[]{"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 }