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: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
037     * @see MailGBean
038     */
039    public class NNTPStoreGBean 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 Boolean auth;
047        private String saslRealm;
048        private Boolean quitWait;
049        private String socketFactoryClass;
050        private Boolean socketFactoryFallback;
051        private Integer socketFactoryPort;
052    
053    
054        /**
055         * Construct an instance of NNTPStoreGBean
056         * <p/>
057         * Values that are set in the individual member variables will override any of
058         * the corresponding values that have been set in the properties set.
059         *
060         * @param objectName            the object name of the protocol
061         * @param properties            the set of default properties for the protocol
062         * @param host                  the host the protocol connects to
063         * @param user                  the default name for the protocol
064         * @param port                  the NNTP server port
065         * @param connectionTimeout     the socket connection timeout value in milliseconds
066         * @param timeout               the socket I/O timeout value in milliseconds
067         * @param auth                  whether an attempt will be made to authenticate the user
068         * @param saslRealm             the realm to use with DIGEST-MD5 authentication
069         * @param quitWait              whether the transport will wait for the response to the QUIT command
070         * @param socketFactoryClass    the class that will be used to create NNTP sockets
071         * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
072         *                              socket factory class cannot be created
073         * @param socketFactoryPort     whether java.net.Socket class will be created if the specified
074         *                              socket factory class cannot be created
075         */
076        public NNTPStoreGBean(String objectName, Properties properties, String host, String user,
077                                  Integer port,
078                                  Integer connectionTimeout,
079                                  Integer timeout,
080                                  Boolean auth,
081                                  String saslRealm,
082                                  Boolean quitWait,
083                                  String socketFactoryClass,
084                                  Boolean socketFactoryFallback,
085                                  Integer socketFactoryPort) {
086            super(objectName, "nntp", properties, host, user);
087    
088            setPort(port);
089            setConnectionTimeout(connectionTimeout);
090            setTimeout(timeout);
091            setAuth(auth);
092            setSaslRealm(saslRealm);
093            setQuitWait(quitWait);
094            setSocketFactoryClass(socketFactoryClass);
095            setSocketFactoryFallback(socketFactoryFallback);
096            setSocketFactoryPort(socketFactoryPort);
097        }
098    
099        /**
100         * Returns the NNTP server port to connect to, if the connect() method
101         * doesn't explicitly specify one.
102         */
103        public Integer getPort() {
104            return port;
105        }
106    
107        /**
108         * Sets the NNTP server port to connect to, if the connect() method
109         * doesn't explicitly specify one.
110         * <p/>
111         * Defaults to 25.
112         * <p/>
113         * Values that are set here will override any of the corresponding value
114         * that has been set in the properties.
115         *
116         * @param port the NNTP server port to connect to
117         */
118        public void setPort(Integer port) {
119            this.port = port;
120        }
121    
122        /**
123         * Returns the socket connection timeout value in milliseconds.
124         */
125        public Integer getConnectionTimeout() {
126            return connectionTimeout;
127        }
128    
129        /**
130         * Sets the socket connection timeout value in milliseconds.
131         * <p/>
132         * Default is infinite timeout.
133         * <p/>
134         * Values that are set here will override any of the corresponding value
135         * that has been set in the properties.
136         *
137         * @param connectionTimeout the socket connection timeout value in milliseconds.
138         */
139        public void setConnectionTimeout(Integer connectionTimeout) {
140            this.connectionTimeout = connectionTimeout;
141        }
142    
143        /**
144         * Returns the socket I/O timeout value in milliseconds.
145         */
146        public Integer getTimeout() {
147            return timeout;
148        }
149    
150        /**
151         * Sets the socket I/O timeout value in milliseconds.
152         * <p/>
153         * Default is infinite timeout.
154         * <p/>
155         * Values that are set here will override any of the corresponding value
156         * that has been set in the properties.
157         *
158         * @param timeout the socket I/O timeout value in milliseconds
159         */
160        public void setTimeout(Integer timeout) {
161            this.timeout = timeout;
162        }
163    
164    
165        /**
166         * Returns whether an attempt will be made to authenticate the user.
167         * <p/>
168         * Defaults to false.
169         */
170        public Boolean getAuth() {
171            return auth;
172        }
173    
174        /**
175         * Sets whether an attempt will be made to authenticate the user.
176         * <p/>
177         * Defaults to false.
178         * <p/>
179         * Values that are set here will override any of the corresponding value
180         * that has been set in the properties.
181         *
182         * @param auth whether an attempt will be made to authenticate the user.
183         */
184        public void setAuth(Boolean auth) {
185            this.auth = auth;
186        }
187    
188        /**
189         * Returns the realm to use with DIGEST-MD5 authentication.
190         */
191        public String getSaslRealm() {
192            return saslRealm;
193        }
194    
195        /**
196         * Sets the realm to use with DIGEST-MD5 authentication.
197         * <p/>
198         * Values that are set here will override any of the corresponding value
199         * that has been set in the properties.
200         *
201         * @param saslRealm the realm to use with DIGEST-MD5 authentication
202         */
203        public void setSaslRealm(String saslRealm) {
204            this.saslRealm = saslRealm;
205        }
206    
207        /**
208         * Returns whether the transport will wait for the response to the QUIT command.
209         * <p/>
210         * If set to true, causes the transport to wait for the response to the QUIT
211         * command. If set to false (the default), the QUIT command is sent and the
212         * connection is immediately closed.
213         */
214        public Boolean getQuitWait() {
215            return quitWait;
216        }
217    
218        /**
219         * Sets whether the transport will wait for the response to the QUIT command
220         * <p/>
221         * If set to true, causes the transport to wait for the response to the QUIT
222         * command. If set to false (the default), the QUIT command is sent and the
223         * connection is immediately closed.
224         * <p/>
225         * Values that are set here will override any of the corresponding value
226         * that has been set in the properties.
227         *
228         * @param quitWait whether the transport will wait for the response to the QUIT command
229         */
230        public void setQuitWait(Boolean quitWait) {
231            this.quitWait = quitWait;
232        }
233    
234        /**
235         * Returns the class that will be used to create NNTP sockets.
236         * <p/>
237         * If set, specifies the name of a class that implements the
238         * javax.net.SocketFactory interface. This class will be used to create NNTP
239         * sockets.
240         */
241        public String getSocketFactoryClass() {
242            return socketFactoryClass;
243        }
244    
245        /**
246         * Sets the class that will be used to create NNTP sockets.
247         * <p/>
248         * If set, specifies the name of a class that implements the
249         * javax.net.SocketFactory interface. This class will be used to create NNTP
250         * sockets.
251         * <p/>
252         * Values that are set here will override any of the corresponding value
253         * that has been set in the properties.
254         *
255         * @param socketFactoryClass the class that will be used to create NNTP sockets
256         */
257        public void setSocketFactoryClass(String socketFactoryClass) {
258            this.socketFactoryClass = socketFactoryClass;
259        }
260    
261        /**
262         * Returns whether java.net.Socket class will be created if the specified
263         * socket factory class cannot be created.
264         * <p/>
265         * If set to true, failure to create a socket using the specified socket
266         * factory class will cause the socket to be created using the
267         * java.net.Socket class. Defaults to true.
268         */
269        public Boolean getSocketFactoryFallback() {
270            return socketFactoryFallback;
271        }
272    
273        /**
274         * Sets whether java.net.Socket class will be created if the specified
275         * socket factory class cannot be created.
276         * <p/>
277         * If set to true, failure to create a socket using the specified socket
278         * factory class will cause the socket to be created using the
279         * java.net.Socket class. Defaults to true.
280         * <p/>
281         * Values that are set here will override any of the corresponding value
282         * that has been set in the properties.
283         *
284         * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
285         *                              socket factory class cannot be created
286         */
287        public void setSocketFactoryFallback(Boolean socketFactoryFallback) {
288            this.socketFactoryFallback = socketFactoryFallback;
289        }
290    
291        /**
292         * Returns the port to connect to when using the specified socket factory.
293         * <p/>
294         * Specifies the port to connect to when using the specified socket
295         * factory. If not set, the default port will be used.
296         */
297        public Integer getSocketFactoryPort() {
298            return socketFactoryPort;
299        }
300    
301        /**
302         * Sets the port to connect to when using the specified socket factory.
303         * <p/>
304         * Specifies the port to connect to when using the specified socket
305         * factory. If not set, the default port will be used.
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 socketFactoryPort the port to connect to when using the specified socket factory
311         */
312        public void setSocketFactoryPort(Integer socketFactoryPort) {
313            this.socketFactoryPort = socketFactoryPort;
314        }
315    
316        /**
317         * Add the overrides from the member variables to the properties file.
318         */
319        public void addOverrides(Properties props) {
320            super.addOverrides(props);
321    
322            if (port != null) props.setProperty(NNTPS_PORT, port.toString());
323            if (connectionTimeout != null) props.setProperty(NNTPS_CONNECTION_TIMEOUT, connectionTimeout.toString());
324            if (timeout != null) props.setProperty(NNTPS_TIMEOUT, timeout.toString());
325            if (auth != null) props.setProperty(NNTPS_AUTH, auth.toString());
326            if (saslRealm != null) props.setProperty(NNTPS_REALM, saslRealm);
327            if (quitWait != null) props.setProperty(NNTPS_QUITWAIT, quitWait.toString());
328            if (socketFactoryClass != null) props.setProperty(NNTPS_FACTORY_CLASS, socketFactoryClass);
329            if (socketFactoryFallback != null) props.setProperty(NNTPS_FACTORY_FALLBACK, socketFactoryFallback.toString());
330            if (socketFactoryPort != null) props.setProperty(NNTPS_FACTORY_PORT, socketFactoryPort.toString());
331        }
332    
333        public void doStart() throws Exception {
334            log.debug("Started " + getObjectName());
335        }
336    
337        public void doStop() throws Exception {
338            log.debug("Stopped " + getObjectName());
339        }
340    
341        public void doFail() {
342            log.warn("Failed " + getObjectName());
343        }
344    
345        public static final GBeanInfo GBEAN_INFO;
346    
347        static {
348            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(NNTPStoreGBean.class);
349    
350            infoFactory.addAttribute(GBEAN_PORT, Integer.class, true);
351            infoFactory.addAttribute(GBEAN_CONNECTION_TIMEOUT, Integer.class, true);
352            infoFactory.addAttribute(GBEAN_TIMEOUT, Integer.class, true);
353            infoFactory.addAttribute(GBEAN_AUTH, Boolean.class, true);
354            infoFactory.addAttribute(GBEAN_REALM, String.class, true);
355            infoFactory.addAttribute(GBEAN_QUITWAIT, Boolean.class, true);
356            infoFactory.addAttribute(GBEAN_FACTORY_CLASS, String.class, true);
357            infoFactory.addAttribute(GBEAN_FACTORY_FALLBACK, Boolean.class, true);
358            infoFactory.addAttribute(GBEAN_FACTORY_PORT, Integer.class, true);
359    
360            infoFactory.addAttribute(GBEAN_OBJECTNAME, String.class, false);
361            infoFactory.addAttribute(GBEAN_PROTOCOL, String.class, true);
362            infoFactory.addAttribute(GBEAN_PROPERTIES, Properties.class, true);
363            infoFactory.addAttribute(GBEAN_HOST, String.class, true);
364            infoFactory.addAttribute(GBEAN_USER, String.class, true);
365            infoFactory.addOperation(GBEAN_ADD_OVERRIDES, new Class[]{Properties.class});
366    
367            infoFactory.setConstructor(new String[]{GBEAN_OBJECTNAME, GBEAN_PROPERTIES, GBEAN_HOST, GBEAN_USER,
368                                                    GBEAN_PORT,
369                                                    GBEAN_CONNECTION_TIMEOUT,
370                                                    GBEAN_TIMEOUT,
371                                                    GBEAN_AUTH,
372                                                    GBEAN_REALM,
373                                                    GBEAN_QUITWAIT,
374                                                    GBEAN_FACTORY_CLASS,
375                                                    GBEAN_FACTORY_FALLBACK,
376                                                    GBEAN_FACTORY_PORT});
377    
378            GBEAN_INFO = infoFactory.getBeanInfo();
379        }
380    
381        public static GBeanInfo getGBeanInfo() {
382            return GBEAN_INFO;
383        }
384    }
385    
386