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 NNTPTransportGBean 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 String from;
48      private Boolean auth;
49      private String saslRealm;
50      private Boolean quitWait;
51      private String socketFactoryClass;
52      private Boolean socketFactoryFallback;
53      private Integer socketFactoryPort;
54  
55  
56      /**
57       * Construct an instance of NNTPTransportGBean
58       * <p/>
59       * Values that are set in the individual member variables will override any of
60       * the corresponding values that have been set in the properties set.
61       *
62       * @param objectName            the object name of the protocol
63       * @param properties            the set of default properties for the protocol
64       * @param host                  the host the protocol connects to
65       * @param user                  the default name for the protocol
66       * @param port                  the NNTP server port
67       * @param connectionTimeout     the socket connection timeout value in milliseconds
68       * @param timeout               the socket I/O timeout value in milliseconds
69       * @param from                  the email address to use for NNTP POST command
70       * @param auth                  whether an attempt will be made to authenticate the user
71       * @param saslRealm             the realm to use with DIGEST-MD5 authentication
72       * @param quitWait              whether the transport will wait for the response to the QUIT command
73       * @param socketFactoryClass    the class that will be used to create NNTP sockets
74       * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
75       *                              socket factory class cannot be created
76       * @param socketFactoryPort     whether java.net.Socket class will be created if the specified
77       *                              socket factory class cannot be created
78       */
79      public NNTPTransportGBean(String objectName, Properties properties, String host, String user,
80                                Integer port,
81                                Integer connectionTimeout,
82                                Integer timeout,
83                                String from,
84                                Boolean auth,
85                                String saslRealm,
86                                Boolean quitWait,
87                                String socketFactoryClass,
88                                Boolean socketFactoryFallback,
89                                Integer socketFactoryPort) {
90          super(objectName, "nntp-post", properties, host, user);
91  
92          setPort(port);
93          setConnectionTimeout(connectionTimeout);
94          setTimeout(timeout);
95          setFrom(from);
96          setAuth(auth);
97          setSaslRealm(saslRealm);
98          setQuitWait(quitWait);
99          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