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