001    /**
002     *
003     * Copyright 2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.mail;
018    
019    import javax.mail.Authenticator;
020    import javax.mail.Session;
021    import java.util.Collection;
022    import java.util.Iterator;
023    import java.util.Properties;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    import org.apache.geronimo.gbean.GBeanInfo;
029    import org.apache.geronimo.gbean.GBeanInfoBuilder;
030    import org.apache.geronimo.gbean.GBeanLifecycle;
031    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
032    import org.apache.geronimo.management.JavaMailResource;
033    
034    
035    /**
036     * GBean that provides access to JavaMail Sessions.
037     * <p/>
038     * This GBean is used to generate JavaMail Sessions.  JavaMail properties that
039     * are common to all JavaMail Sessions are provided via member variables of this
040     * class.
041     *
042     * @version $Rev: 399081 $ $Date: 2006-05-02 16:21:29 -0700 (Tue, 02 May 2006) $
043     * @see ProtocolGBean
044     * @see SMTPTransportGBean
045     * @see POP3StoreGBean
046     * @see IMAPStoreGBean
047     */
048    public class MailGBean implements GBeanLifecycle, JavaMailResource {
049    
050        private final Log log = LogFactory.getLog(MailGBean.class);
051    
052        private final String objectName;
053        private final Collection protocols;
054        private Boolean useDefault;
055        private Properties properties;
056        private Authenticator authenticator;
057        private String storeProtocol;
058        private String transportProtocol;
059        private String host;
060        private String user;
061        private Boolean debug;
062    
063    
064        /**
065         * Construct an instance of MailGBean
066         * <p/>
067         * Values that are set in the individual member variables will override any of
068         * the corresponding values that have been set in the properties set.
069         *
070         * @param protocols         the set of protocol GBeans that contain protocol specific configurations
071         * @param useDefault        whether this GBean will return default Sessions or not
072         * @param properties        the set of default properties for the protocols
073         * @param authenticator     the authenticator object
074         * @param storeProtocol     the store protocol that Sessions created from this GBean will return
075         * @param transportProtocol the transport protocol that Sessions created from this GBean will return
076         * @param host              the default Mail server
077         * @param user              the username to provide when connecting to a Mail server
078         * @param debug             the debug setting for Sessions created from this GBean
079         */
080        public MailGBean(String objectName, Collection protocols, Boolean useDefault, Properties properties, Authenticator authenticator,
081                         String storeProtocol, String transportProtocol, String host, String user, Boolean debug) {
082            this.objectName = objectName;
083            this.protocols = protocols;
084            setUseDefault(useDefault);
085            this.properties = (properties == null ? new Properties() : properties);
086            setAuthenticator(authenticator);
087            setStoreProtocol(storeProtocol);
088            setTransportProtocol(transportProtocol);
089            setHost(host);
090            setUser(user);
091            setDebug(debug);
092    
093        }
094    
095        /**
096         * Returns the set of protocol GBeans that contain protocol specific configurations.
097         */
098        public Collection getProtocols() {
099            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            // 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 {
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    }