KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mail > NNTPTransportGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.mail;
18
19 import java.util.Properties JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.apache.geronimo.gbean.GBeanInfo;
25 import org.apache.geronimo.gbean.GBeanInfoBuilder;
26
27 /**
28  * A GBean that provides for the configuration of a JavaMail NNTP transport
29  * protocol.
30  * <p/>
31  * NNTP transport properties that are common to all NNTP transports are
32  * provided via member variables of this class. Values that are set in the
33  * individual member variables will override any of the corresponding values
34  * that have been set in the properties set.
35  *
36  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
37  * @see MailGBean
38  */

39 public class NNTPTransportGBean extends ProtocolGBean implements NNTPGBeanConstants {
40
41     private final Log log = LogFactory.getLog(NNTPTransportGBean.class);
42
43     private Integer JavaDoc port;
44     private Integer JavaDoc connectionTimeout;
45     private Integer JavaDoc timeout;
46     private String JavaDoc from;
47     private Boolean JavaDoc auth;
48     private String JavaDoc saslRealm;
49     private Boolean JavaDoc quitWait;
50     private String JavaDoc socketFactoryClass;
51     private Boolean JavaDoc socketFactoryFallback;
52     private Integer JavaDoc socketFactoryPort;
53
54
55     /**
56      * Construct an instance of NNTPTransportGBean
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 from the email address to use for NNTP POST command
69      * @param auth whether an attempt will be made to authenticate the user
70      * @param saslRealm the realm to use with DIGEST-MD5 authentication
71      * @param quitWait whether the transport will wait for the response to the QUIT command
72      * @param socketFactoryClass the class that will be used to create NNTP sockets
73      * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
74      * socket factory class cannot be created
75      * @param socketFactoryPort whether java.net.Socket class will be created if the specified
76      * socket factory class cannot be created
77      */

78     public NNTPTransportGBean(String JavaDoc objectName, Properties JavaDoc properties, String JavaDoc host, String JavaDoc user,
79                               Integer JavaDoc port,
80                               Integer JavaDoc connectionTimeout,
81                               Integer JavaDoc timeout,
82                               String JavaDoc from,
83                               Boolean JavaDoc auth,
84                               String JavaDoc saslRealm,
85                               Boolean JavaDoc quitWait,
86                               String JavaDoc socketFactoryClass,
87                               Boolean JavaDoc socketFactoryFallback,
88                               Integer JavaDoc socketFactoryPort) {
89         super(objectName, "nntp-post", properties, host, user);
90
91         setPort(port);
92         setConnectionTimeout(connectionTimeout);
93         setTimeout(timeout);
94         setFrom(from);
95         setAuth(auth);
96         setSaslRealm(saslRealm);
97         setQuitWait(quitWait);
98         setSocketFactoryClass(socketFactoryClass);
99         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 JavaDoc 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 JavaDoc port) {
123         this.port = port;
124     }
125
126     /**
127      * Returns the socket connection timeout value in milliseconds.
128      */

129     public Integer JavaDoc 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 JavaDoc connectionTimeout) {
144         this.connectionTimeout = connectionTimeout;
145     }
146
147     /**
148      * Returns the socket I/O timeout value in milliseconds.
149      */

150     public Integer JavaDoc 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 JavaDoc timeout) {
165         this.timeout = timeout;
166     }
167
168     /**
169      * Returns the email address to use for NNTP POST command.
170      */

171     public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc auth) {
210         this.auth = auth;
211     }
212
213     /**
214      * Returns the realm to use with DIGEST-MD5 authentication.
215      */

216     public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
360         log.debug("Started " + getObjectName());
361     }
362
363     public void doStop() throws Exception JavaDoc {
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 JavaDoc.class, true);
377         infoFactory.addAttribute(GBEAN_CONNECTION_TIMEOUT, Integer JavaDoc.class, true);
378         infoFactory.addAttribute(GBEAN_TIMEOUT, Integer JavaDoc.class, true);
379         infoFactory.addAttribute(GBEAN_AUTH, Boolean JavaDoc.class, true);
380         infoFactory.addAttribute(GBEAN_FROM, String JavaDoc.class, true);
381         infoFactory.addAttribute(GBEAN_REALM, String JavaDoc.class, true);
382         infoFactory.addAttribute(GBEAN_QUITWAIT, Boolean JavaDoc.class, true);
383         infoFactory.addAttribute(GBEAN_FACTORY_CLASS, String JavaDoc.class, true);
384         infoFactory.addAttribute(GBEAN_FACTORY_FALLBACK, Boolean JavaDoc.class, true);
385         infoFactory.addAttribute(GBEAN_FACTORY_PORT, Integer JavaDoc.class, true);
386
387         infoFactory.addAttribute(GBEAN_OBJECTNAME, String JavaDoc.class, false);
388         infoFactory.addAttribute(GBEAN_PROTOCOL, String JavaDoc.class, true);
389         infoFactory.addAttribute(GBEAN_PROPERTIES, Properties JavaDoc.class, true);
390         infoFactory.addAttribute(GBEAN_HOST, String JavaDoc.class, true);
391         infoFactory.addAttribute(GBEAN_USER, String JavaDoc.class, true);
392         infoFactory.addOperation(GBEAN_ADD_OVERRIDES, new Class JavaDoc[]{Properties JavaDoc.class});
393
394         infoFactory.setConstructor(new String JavaDoc[]{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
414
Popular Tags