View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  package org.apache.geronimo.mail;
19  
20  import java.util.Properties;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import org.apache.geronimo.gbean.GBeanInfo;
26  import org.apache.geronimo.gbean.GBeanInfoBuilder;
27  
28  /**
29   * A GBean that provides for the configuration of a JavaMail NNTP transport
30   * protocol.
31   * <p/>
32   * NNTP transport properties that are common to all NNTP transports are
33   * provided via member variables of this class.  Values that are set in the
34   * individual member variables will override any of the corresponding values
35   * that have been set in the properties set.
36   *
37   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
38   * @see MailGBean
39   */
40  public class NNTPStoreGBean extends ProtocolGBean implements NNTPGBeanConstants {
41  
42      private final Log log = LogFactory.getLog(NNTPTransportGBean.class);
43  
44      private Integer port;
45      private Integer connectionTimeout;
46      private Integer timeout;
47      private Boolean auth;
48      private String saslRealm;
49      private Boolean quitWait;
50      private String socketFactoryClass;
51      private Boolean socketFactoryFallback;
52      private Integer socketFactoryPort;
53  
54  
55      /**
56       * Construct an instance of NNTPStoreGBean
57       * <p/>
58       * Values that are set in the individual member variables will override any of
59       * the corresponding values that have been set in the properties set.
60       *
61       * @param objectName            the object name of the protocol
62       * @param properties            the set of default properties for the protocol
63       * @param host                  the host the protocol connects to
64       * @param user                  the default name for the protocol
65       * @param port                  the NNTP server port
66       * @param connectionTimeout     the socket connection timeout value in milliseconds
67       * @param timeout               the socket I/O timeout value in milliseconds
68       * @param auth                  whether an attempt will be made to authenticate the user
69       * @param saslRealm             the realm to use with DIGEST-MD5 authentication
70       * @param quitWait              whether the transport will wait for the response to the QUIT command
71       * @param socketFactoryClass    the class that will be used to create NNTP sockets
72       * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
73       *                              socket factory class cannot be created
74       * @param socketFactoryPort     whether java.net.Socket class will be created if the specified
75       *                              socket factory class cannot be created
76       */
77      public NNTPStoreGBean(String objectName, Properties properties, String host, String user,
78                                Integer port,
79                                Integer connectionTimeout,
80                                Integer timeout,
81                                Boolean auth,
82                                String saslRealm,
83                                Boolean quitWait,
84                                String socketFactoryClass,
85                                Boolean socketFactoryFallback,
86                                Integer socketFactoryPort) {
87          super(objectName, "nntp", properties, host, user);
88  
89          setPort(port);
90          setConnectionTimeout(connectionTimeout);
91          setTimeout(timeout);
92          setAuth(auth);
93          setSaslRealm(saslRealm);
94          setQuitWait(quitWait);
95          setSocketFactoryClass(socketFactoryClass);
96          setSocketFactoryFallback(socketFactoryFallback);
97          setSocketFactoryPort(socketFactoryPort);
98      }
99  
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