001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.mail;
019    
020    import java.util.Properties;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    import org.apache.geronimo.gbean.GBeanInfo;
026    import org.apache.geronimo.gbean.GBeanInfoBuilder;
027    
028    /**
029     * A GBean that provides for the configuration of a JavaMail NNTP transport
030     * protocol.
031     * <p/>
032     * NNTP transport properties that are common to all NNTP transports are
033     * provided via member variables of this class.  Values that are set in the
034     * individual member variables will override any of the corresponding values
035     * that have been set in the properties set.
036     *
037     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
038     * @see MailGBean
039     */
040    public class NNTPTransportGBean extends ProtocolGBean implements NNTPGBeanConstants {
041    
042        private final Log log = LogFactory.getLog(NNTPTransportGBean.class);
043    
044        private Integer port;
045        private Integer connectionTimeout;
046        private Integer timeout;
047        private String from;
048        private Boolean auth;
049        private String saslRealm;
050        private Boolean quitWait;
051        private String socketFactoryClass;
052        private Boolean socketFactoryFallback;
053        private Integer socketFactoryPort;
054    
055    
056        /**
057         * Construct an instance of NNTPTransportGBean
058         * <p/>
059         * Values that are set in the individual member variables will override any of
060         * the corresponding values that have been set in the properties set.
061         *
062         * @param objectName            the object name of the protocol
063         * @param properties            the set of default properties for the protocol
064         * @param host                  the host the protocol connects to
065         * @param user                  the default name for the protocol
066         * @param port                  the NNTP server port
067         * @param connectionTimeout     the socket connection timeout value in milliseconds
068         * @param timeout               the socket I/O timeout value in milliseconds
069         * @param from                  the email address to use for NNTP POST command
070         * @param auth                  whether an attempt will be made to authenticate the user
071         * @param saslRealm             the realm to use with DIGEST-MD5 authentication
072         * @param quitWait              whether the transport will wait for the response to the QUIT command
073         * @param socketFactoryClass    the class that will be used to create NNTP sockets
074         * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
075         *                              socket factory class cannot be created
076         * @param socketFactoryPort     whether java.net.Socket class will be created if the specified
077         *                              socket factory class cannot be created
078         */
079        public NNTPTransportGBean(String objectName, Properties properties, String host, String user,
080                                  Integer port,
081                                  Integer connectionTimeout,
082                                  Integer timeout,
083                                  String from,
084                                  Boolean auth,
085                                  String saslRealm,
086                                  Boolean quitWait,
087                                  String socketFactoryClass,
088                                  Boolean socketFactoryFallback,
089                                  Integer socketFactoryPort) {
090            super(objectName, "nntp-post", properties, host, user);
091    
092            setPort(port);
093            setConnectionTimeout(connectionTimeout);
094            setTimeout(timeout);
095            setFrom(from);
096            setAuth(auth);
097            setSaslRealm(saslRealm);
098            setQuitWait(quitWait);
099            setSocketFactoryClass(socketFactoryClass);
100            setSocketFactoryFallback(socketFactoryFallback);
101            setSocketFactoryPort(socketFactoryPort);
102        }
103    
104        /**
105         * Returns the NNTP server port to connect to, if the connect() method
106         * doesn't explicitly specify one.
107         */
108        public Integer getPort() {
109            return port;
110        }
111    
112        /**
113         * Sets the NNTP server port to connect to, if the connect() method
114         * doesn't explicitly specify one.
115         * <p/>
116         * Defaults to 25.
117         * <p/>
118         * Values that are set here will override any of the corresponding value
119         * that has been set in the properties.
120         *
121         * @param port the NNTP server port to connect to
122         */
123        public void setPort(Integer port) {
124            this.port = port;
125        }
126    
127        /**
128         * Returns the socket connection timeout value in milliseconds.
129         */
130        public Integer getConnectionTimeout() {
131            return connectionTimeout;
132        }
133    
134        /**
135         * Sets the socket connection timeout value in milliseconds.
136         * <p/>
137         * Default is infinite timeout.
138         * <p/>
139         * Values that are set here will override any of the corresponding value
140         * that has been set in the properties.
141         *
142         * @param connectionTimeout the socket connection timeout value in milliseconds.
143         */
144        public void setConnectionTimeout(Integer connectionTimeout) {
145            this.connectionTimeout = connectionTimeout;
146        }
147    
148        /**
149         * Returns the socket I/O timeout value in milliseconds.
150         */
151        public Integer getTimeout() {
152            return timeout;
153        }
154    
155        /**
156         * Sets the socket I/O timeout value in milliseconds.
157         * <p/>
158         * Default is infinite timeout.
159         * <p/>
160         * Values that are set here will override any of the corresponding value
161         * that has been set in the properties.
162         *
163         * @param timeout the socket I/O timeout value in milliseconds
164         */
165        public void setTimeout(Integer timeout) {
166            this.timeout = timeout;
167        }
168    
169        /**
170         * Returns the email address to use for NNTP POST command.
171         */
172        public String getFrom() {
173            return from;
174        }
175    
176        /**
177         * Sets the email address to use for NNTP POST command
178         * <p/>
179         * Email address to use for NNTP POST command. This sets the envelope
180         * return address. Defaults to msg.getFrom() or InternetAddress.getLocalAddress().
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 from the email address to use for NNTP POST command
186         */
187        public void setFrom(String from) {
188            this.from = from;
189        }
190    
191        /**
192         * Returns whether an attempt will be made to authenticate the user
193         * <p/>
194         * Defaults to false.
195         */
196        public Boolean getAuth() {
197            return auth;
198        }
199    
200        /**
201         * Sets whether an attempt will be made to authenticate the user.
202         * <p/>
203         * Defaults to false.
204         * <p/>
205         * Values that are set here will override any of the corresponding value
206         * that has been set in the properties.
207         *
208         * @param auth whether an attempt will be made to authenticate the user.
209         */
210        public void setAuth(Boolean auth) {
211            this.auth = auth;
212        }
213    
214        /**
215         * Returns the realm to use with DIGEST-MD5 authentication.
216         */
217        public String getSaslRealm() {
218            return saslRealm;
219        }
220    
221        /**
222         * Sets the realm to use with DIGEST-MD5 authentication.
223         * <p/>
224         * Values that are set here will override any of the corresponding value
225         * that has been set in the properties.
226         *
227         * @param saslRealm the realm to use with DIGEST-MD5 authentication
228         */
229        public void setSaslRealm(String saslRealm) {
230            this.saslRealm = saslRealm;
231        }
232    
233        /**
234         * Returns whether the transport will wait for the response to the QUIT command.
235         * <p/>
236         * If set to true, causes the transport to wait for the response to the QUIT
237         * command. If set to false (the default), the QUIT command is sent and the
238         * connection is immediately closed.
239         */
240        public Boolean getQuitWait() {
241            return quitWait;
242        }
243    
244        /**
245         * Sets whether the transport will wait for the response to the QUIT command
246         * <p/>
247         * If set to true, causes the transport to wait for the response to the QUIT
248         * command. If set to false (the default), the QUIT command is sent and the
249         * connection is immediately closed.
250         * <p/>
251         * Values that are set here will override any of the corresponding value
252         * that has been set in the properties.
253         *
254         * @param quitWait whether the transport will wait for the response to the QUIT command
255         */
256        public void setQuitWait(Boolean quitWait) {
257            this.quitWait = quitWait;
258        }
259    
260        /**
261         * Returns the class that will be used to create NNTP sockets.
262         * <p/>
263         * If set, specifies the name of a class that implements the
264         * javax.net.SocketFactory interface. This class will be used to create NNTP
265         * sockets.
266         */
267        public String getSocketFactoryClass() {
268            return socketFactoryClass;
269        }
270    
271        /**
272         * Sets the class that will be used to create NNTP sockets.
273         * <p/>
274         * If set, specifies the name of a class that implements the
275         * javax.net.SocketFactory interface. This class will be used to create NNTP
276         * sockets.
277         * <p/>
278         * Values that are set here will override any of the corresponding value
279         * that has been set in the properties.
280         *
281         * @param socketFactoryClass the class that will be used to create NNTP sockets
282         */
283        public void setSocketFactoryClass(String socketFactoryClass) {
284            this.socketFactoryClass = socketFactoryClass;
285        }
286    
287        /**
288         * Returns whether java.net.Socket class will be created if the specified
289         * socket factory class cannot be created.
290         * <p/>
291         * If set to true, failure to create a socket using the specified socket
292         * factory class will cause the socket to be created using the
293         * java.net.Socket class. Defaults to true.
294         */
295        public Boolean getSocketFactoryFallback() {
296            return socketFactoryFallback;
297        }
298    
299        /**
300         * Sets whether java.net.Socket class will be created if the specified
301         * socket factory class cannot be created.
302         * <p/>
303         * If set to true, failure to create a socket using the specified socket
304         * factory class will cause the socket to be created using the
305         * java.net.Socket class. Defaults to true.
306         * <p/>
307         * Values that are set here will override any of the corresponding value
308         * that has been set in the properties.
309         *
310         * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
311         *                              socket factory class cannot be created
312         */
313        public void setSocketFactoryFallback(Boolean socketFactoryFallback) {
314            this.socketFactoryFallback = socketFactoryFallback;
315        }
316    
317        /**
318         * Returns the port to connect to when using the specified socket factory.
319         * <p/>
320         * Specifies the port to connect to when using the specified socket
321         * factory. If not set, the default port will be used.
322         */
323        public Integer getSocketFactoryPort() {
324            return socketFactoryPort;
325        }
326    
327        /**
328         * Sets the port to connect to when using the specified socket factory.
329         * <p/>
330         * Specifies the port to connect to when using the specified socket
331         * factory. If not set, the default port will be used.
332         * <p/>
333         * Values that are set here will override any of the corresponding value
334         * that has been set in the properties.
335         *
336         * @param socketFactoryPort the port to connect to when using the specified socket factory
337         */
338        public void setSocketFactoryPort(Integer socketFactoryPort) {
339            this.socketFactoryPort = socketFactoryPort;
340        }
341    
342        /**
343         * Add the overrides from the member variables to the properties file.
344         */
345        public void addOverrides(Properties props) {
346            super.addOverrides(props);
347    
348            if (port != null) props.setProperty(NNTP_PORT, port.toString());
349            if (connectionTimeout != null) props.setProperty(NNTP_CONNECTION_TIMEOUT, connectionTimeout.toString());
350            if (timeout != null) props.setProperty(NNTP_TIMEOUT, timeout.toString());
351            if (from != null) props.setProperty(NNTP_FROM, from);
352            if (auth != null) props.setProperty(NNTP_AUTH, auth.toString());
353            if (saslRealm != null) props.setProperty(NNTP_REALM, saslRealm);
354            if (quitWait != null) props.setProperty(NNTP_QUITWAIT, quitWait.toString());
355            if (socketFactoryClass != null) props.setProperty(NNTP_FACTORY_CLASS, socketFactoryClass);
356            if (socketFactoryFallback != null) props.setProperty(NNTP_FACTORY_FALLBACK, socketFactoryFallback.toString());
357            if (socketFactoryPort != null) props.setProperty(NNTP_FACTORY_PORT, socketFactoryPort.toString());
358        }
359    
360        public void doStart() throws Exception {
361            log.debug("Started " + getObjectName());
362        }
363    
364        public void doStop() throws Exception {
365            log.debug("Stopped " + getObjectName());
366        }
367    
368        public void doFail() {
369            log.warn("Failed " + getObjectName());
370        }
371    
372        public static final GBeanInfo GBEAN_INFO;
373    
374        static {
375            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(NNTPTransportGBean.class);
376    
377            infoFactory.addAttribute(GBEAN_PORT, Integer.class, true);
378            infoFactory.addAttribute(GBEAN_CONNECTION_TIMEOUT, Integer.class, true);
379            infoFactory.addAttribute(GBEAN_TIMEOUT, Integer.class, true);
380            infoFactory.addAttribute(GBEAN_AUTH, Boolean.class, true);
381            infoFactory.addAttribute(GBEAN_FROM, String.class, true);
382            infoFactory.addAttribute(GBEAN_REALM, String.class, true);
383            infoFactory.addAttribute(GBEAN_QUITWAIT, Boolean.class, true);
384            infoFactory.addAttribute(GBEAN_FACTORY_CLASS, String.class, true);
385            infoFactory.addAttribute(GBEAN_FACTORY_FALLBACK, Boolean.class, true);
386            infoFactory.addAttribute(GBEAN_FACTORY_PORT, Integer.class, true);
387    
388            infoFactory.addAttribute(GBEAN_OBJECTNAME, String.class, false);
389            infoFactory.addAttribute(GBEAN_PROTOCOL, String.class, true);
390            infoFactory.addAttribute(GBEAN_PROPERTIES, Properties.class, true);
391            infoFactory.addAttribute(GBEAN_HOST, String.class, true);
392            infoFactory.addAttribute(GBEAN_USER, String.class, true);
393            infoFactory.addOperation(GBEAN_ADD_OVERRIDES, new Class[]{Properties.class});
394    
395            infoFactory.setConstructor(new String[]{GBEAN_OBJECTNAME, GBEAN_PROPERTIES, GBEAN_HOST, GBEAN_USER,
396                                                    GBEAN_PORT,
397                                                    GBEAN_CONNECTION_TIMEOUT,
398                                                    GBEAN_TIMEOUT,
399                                                    GBEAN_FROM,
400                                                    GBEAN_AUTH,
401                                                    GBEAN_REALM,
402                                                    GBEAN_QUITWAIT,
403                                                    GBEAN_FACTORY_CLASS,
404                                                    GBEAN_FACTORY_FALLBACK,
405                                                    GBEAN_FACTORY_PORT});
406    
407            GBEAN_INFO = infoFactory.getBeanInfo();
408        }
409    
410        public static GBeanInfo getGBeanInfo() {
411            return GBEAN_INFO;
412        }
413    }
414