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