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    /**
029     * A GBean that provides for the configuration of a JavaMail POP3 message store
030     * protocol.
031     * <p/>
032     * POP3 store properties that are common to all POP3 stores 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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
038     * @see MailGBean
039     */
040    public class POP3StoreGBean extends ProtocolGBean {
041    
042        // the POP3 configuration property names
043        static public final String POP3_PORT = "mail.pop3.port";
044        static public final String POP3_CONNECTION_TIMEOUT = "mail.pop3.connectiontimeout";
045        static public final String POP3_TIMEOUT = "mail.pop3.timeout";
046        static public final String POP3_FACTORY_CLASS = "mail.pop3.socketFactory.class";
047        static public final String POP3_FACTORY_FALLBACK = "mail.pop3.socketFactory.fallback";
048        static public final String POP3_FACTORY_PORT = "mail.pop3.socketFactory.port";
049        static public final String POP3_LOCALHOST = "mail.pop3.localhost";
050        static public final String POP3_LOCALADDRESS = "mail.pop3.localaddress";
051        static public final String POP3_LOCALPORT = "mail.pop3.localport";
052        static public final String POP3_RESET = "mail.pop3.resetbeforequit";
053        static public final String POP3_MESSAGE_CLASS = "mail.pop3.message.class";
054        static public final String POP3_APOP = "mail.pop3.apop.enable";
055    
056        static public final String GBEAN_RESET = "rsetBeforeQuit";
057        static public final String GBEAN_APOP = "apopEnable";
058        static public final String GBEAN_MESSAGE_CLASS = "messageClass";
059    
060        private final Log log = LogFactory.getLog(POP3StoreGBean.class);
061    
062        private Integer port;
063        private Integer connectionTimeout;
064        private Integer timeout;
065        private Boolean rsetBeforeQuit;
066        private String messageClass;
067        private String localaddress;
068        private Integer localport;
069        private Boolean apopEnable;
070        private String socketFactoryClass;
071        private Boolean socketFactoryFallback;
072        private Integer socketFactoryPort;
073    
074    
075        /**
076         * Construct an instance of POP3StoreGBean
077         * <p/>
078         * Values that are set in the individual member variables will override any of
079         * the corresponding values that have been set in the properties set.
080         *
081         * @param objectName            the object name of the protocol
082         * @param properties            the set of default properties for the protocol
083         * @param host                  the host the protocol connects to
084         * @param user                  the default name for the protocol
085         * @param port                  the POP3 server port
086         * @param connectionTimeout     the socket connection timeout value in milliseconds
087         * @param timeout               the socket I/O timeout value in milliseconds
088         * @param rsetBeforeQuit        whether an attempt will be made send a POP3 RSET command when closing
089         *                              the folder, before sending the QUIT command
090         * @param messageClass          the class name of a subclass of com.sun.mail.pop3.POP3Message
091         * @param localaddress          the local address (host name) to bind to when creating the POP3 socket
092         * @param localport             the local port number to bind to when creating the POP3 socket
093         * @param apopEnable            whether to use APOP instead of USER/PASS to login to the POP3 server,
094         *                              if the POP3 server supports APOP
095         * @param socketFactoryClass    the class that will be used to create POP3 sockets
096         * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
097         *                              socket factory class cannot be created
098         * @param socketFactoryPort     whether java.net.Socket class will be created if the specified
099         *                              socket factory class cannot be created
100         */
101        public POP3StoreGBean(String objectName, Properties properties, String host, String user,
102                              Integer port,
103                              Integer connectionTimeout,
104                              Integer timeout,
105                              Boolean rsetBeforeQuit,
106                              String messageClass,
107                              String localaddress,
108                              Integer localport,
109                              Boolean apopEnable,
110                              String socketFactoryClass,
111                              Boolean socketFactoryFallback,
112                              Integer socketFactoryPort) {
113            super(objectName, "pop3", properties, host, user);
114    
115            setPort(port);
116            setConnectionTimeout(connectionTimeout);
117            setTimeout(timeout);
118            setRsetBeforeQuit(rsetBeforeQuit);
119            setMessageClass(messageClass);
120            setLocaladdress(localaddress);
121            setLocalport(localport);
122            setApopEnable(apopEnable);
123            setSocketFactoryClass(socketFactoryClass);
124            setSocketFactoryFallback(socketFactoryFallback);
125            setSocketFactoryPort(socketFactoryPort);
126        }
127    
128        /**
129         * Returns the POP3 server port to connect to, if the connect() method
130         * doesn't explicitly specify one.
131         * <p/>
132         * Defaults to 110.
133         */
134        public Integer getPort() {
135            return port;
136        }
137    
138        /**
139         * Sets the POP3 server port to connect to, if the connect() method
140         * doesn't explicitly specify one.
141         * <p/>
142         * Defaults to 110.
143         * <p/>
144         * Values that are set here will override any of the corresponding value
145         * that has been set in the properties.
146         *
147         * @param port the POP3 server port to connect to, if the connect() method
148         *             doesn't explicitly specify one
149         */
150        public void setPort(Integer port) {
151            this.port = port;
152        }
153    
154        /**
155         * Returns the socket connection timeout value in milliseconds.
156         */
157        public Integer getConnectionTimeout() {
158            return connectionTimeout;
159        }
160    
161        /**
162         * Sets the socket connection timeout value in milliseconds.
163         * <p/>
164         * Default is infinite timeout.
165         * <p/>
166         * Values that are set here will override any of the corresponding value
167         * that has been set in the properties.
168         *
169         * @param connectionTimeout the socket connection timeout value in milliseconds.
170         */
171        public void setConnectionTimeout(Integer connectionTimeout) {
172            this.connectionTimeout = connectionTimeout;
173        }
174    
175        /**
176         * Returns the socket I/O timeout value in milliseconds.
177         */
178        public Integer getTimeout() {
179            return timeout;
180        }
181    
182        /**
183         * Sets the socket I/O timeout value in milliseconds.
184         * <p/>
185         * Default is infinite timeout.
186         * <p/>
187         * Values that are set here will override any of the corresponding value
188         * that has been set in the properties.
189         *
190         * @param timeout the socket I/O timeout value in milliseconds
191         */
192        public void setTimeout(Integer timeout) {
193            this.timeout = timeout;
194        }
195    
196        /**
197         * Returns whether an attempt will be made send a POP3 RSET command when
198         * closing the folder, before sending the QUIT command.
199         * <p/>
200         * Send a POP3 RSET command when closing the folder, before sending the
201         * QUIT command. Useful with POP3 servers that implicitly mark all
202         * messages that are read as "deleted"; this will prevent such messages
203         * from being deleted and expunged unless the client requests so. Default
204         * is false.
205         */
206        public Boolean getRsetBeforeQuit() {
207            return rsetBeforeQuit;
208        }
209    
210        /**
211         * Sets whether an attempt will be made send a POP3 RSET command when
212         * closing the folder, before sending the QUIT command.
213         * <p/>
214         * Send a POP3 RSET command when closing the folder, before sending the
215         * QUIT command. Useful with POP3 servers that implicitly mark all messages
216         * that are read as "deleted"; this will prevent such messages from being
217         * deleted and expunged unless the client requests so. Default is false.
218         * <p/>
219         * Values that are set here will override any of the corresponding value
220         * that has been set in the properties.
221         *
222         * @param rsetBeforeQuit whether an attempt will be made send a POP3 RSET command when
223         *                       closing the folder, before sending the QUIT command
224         */
225        public void setRsetBeforeQuit(Boolean rsetBeforeQuit) {
226            this.rsetBeforeQuit = rsetBeforeQuit;
227        }
228    
229        /**
230         * Returns the class name of a subclass of com.sun.mail.pop3.POP3Message.
231         * <p/>
232         * Class name of a subclass of com.sun.mail.pop3.POP3Message. The subclass
233         * can be used to handle (for example) non-standard Content-Type headers.
234         * The subclass must have a public constructor of the form
235         * MyPOP3Message(Folder f, int msgno) throws MessagingException.
236         */
237        public String getMessageClass() {
238            return messageClass;
239        }
240    
241        /**
242         * Sets the class name of a subclass of com.sun.mail.pop3.POP3Message.
243         * <p/>
244         * Class name of a subclass of com.sun.mail.pop3.POP3Message. The subclass
245         * can be used to handle (for example) non-standard Content-Type headers.
246         * The subclass must have a public constructor of the form
247         * MyPOP3Message(Folder f, int msgno) throws MessagingException.
248         * <p/>
249         * Values that are set here will override any of the corresponding value
250         * that has been set in the properties.
251         *
252         * @param messageClass the class name of a subclass of com.sun.mail.pop3.POP3Message.
253         */
254        public void setMessageClass(String messageClass) {
255            this.messageClass = messageClass;
256        }
257    
258        /**
259         * Returns the local address (host name) to bind to when creating the POP3 socket.
260         */
261        public String getLocaladdress() {
262            return localaddress;
263        }
264    
265        /**
266         * Sets the local address (host name) to bind to when creating the POP3 socket.
267         * <p/>
268         * Local address (host name) to bind to when creating the POP3 socket.
269         * Defaults to the address picked by the Socket class. Should not normally
270         * need to be set, but useful with multi-homed hosts where it's important
271         * to pick a particular local address to bind to.
272         * <p/>
273         * Values that are set here will override any of the corresponding value
274         * that has been set in the properties.
275         *
276         * @param localaddress the local address (host name) to bind to when creating the POP3 socket
277         */
278        public void setLocaladdress(String localaddress) {
279            this.localaddress = localaddress;
280        }
281    
282        /**
283         * Returns the local port number to bind to when creating the POP3 socket.
284         */
285        public Integer getLocalport() {
286            return localport;
287        }
288    
289        /**
290         * Sets the local port number to bind to when creating the POP3 socket.
291         * <p/>
292         * Local port number to bind to when creating the POP3 socket. Defaults to
293         * the port number picked by the Socket class.
294         * <p/>
295         * Values that are set here will override any of the corresponding value
296         * that has been set in the properties.
297         *
298         * @param localport the local port number to bind to when creating the POP3 socket
299         */
300        public void setLocalport(Integer localport) {
301            this.localport = localport;
302        }
303    
304        /**
305         * Returns whether to use APOP instead of USER/PASS to login to the POP3
306         * server, if the POP3 server supports APOP.
307         * <p/>
308         * If set to true, use APOP instead of USER/PASS to login to the POP3
309         * server, if the POP3 server supports APOP. APOP sends a digest of the
310         * password rather than the clear text password. Defaults to false.
311         */
312        public Boolean isApopEnable() {
313            return apopEnable;
314        }
315    
316        /**
317         * Sets whether to use APOP instead of USER/PASS to login to the POP3
318         * server, if the POP3 server supports APOP.
319         * <p/>
320         * If set to true, use APOP instead of USER/PASS to login to the POP3
321         * server, if the POP3 server supports APOP. APOP sends a digest of the
322         * password rather than the clear text password. Defaults to false.
323         *
324         * @param apopEnable whether to use APOP instead of USER/PASS to login to the POP3
325         *                   server, if the POP3 server supports APOP
326         */
327        public void setApopEnable(Boolean apopEnable) {
328            this.apopEnable = apopEnable;
329        }
330    
331        /**
332         * Returns the class that will be used to create POP3 sockets.
333         * <p/>
334         * If set, specifies the name of a class that implements the
335         * javax.net.SocketFactory interface. This class will be used to create POP3
336         * sockets.
337         */
338        public String getSocketFactoryClass() {
339            return socketFactoryClass;
340        }
341    
342        /**
343         * Sets the class that will be used to create POP3 sockets.
344         * <p/>
345         * If set, specifies the name of a class that implements the
346         * javax.net.SocketFactory interface. This class will be used to create POP3
347         * sockets.
348         * <p/>
349         * Values that are set here will override any of the corresponding value
350         * that has been set in the properties.
351         *
352         * @param socketFactoryClass the class that will be used to create POP3 sockets
353         */
354        public void setSocketFactoryClass(String socketFactoryClass) {
355            this.socketFactoryClass = socketFactoryClass;
356        }
357    
358        /**
359         * Returns whether java.net.Socket class will be created if the specified
360         * socket factory class cannot be created.
361         * <p/>
362         * If set to true, failure to create a socket using the specified socket
363         * factory class will cause the socket to be created using the
364         * java.net.Socket class. Defaults to true.
365         */
366        public Boolean isSocketFactoryFallback() {
367            return socketFactoryFallback;
368        }
369    
370        /**
371         * Sets whether java.net.Socket class will be created if the specified
372         * socket factory class cannot be created.
373         * <p/>
374         * If set to true, failure to create a socket using the specified socket
375         * factory class will cause the socket to be created using the
376         * java.net.Socket class. Defaults to true.
377         * <p/>
378         * Values that are set here will override any of the corresponding value
379         * that has been set in the properties.
380         *
381         * @param socketFactoryFallback whether java.net.Socket class will be created if the specified
382         *                              socket factory class cannot be created
383         */
384        public void setSocketFactoryFallback(Boolean socketFactoryFallback) {
385            this.socketFactoryFallback = socketFactoryFallback;
386        }
387    
388        /**
389         * Returns the port to connect to when using the specified socket factory.
390         * <p/>
391         * Specifies the port to connect to when using the specified socket
392         * factory. If not set, the default port will be used.
393         */
394        public Integer getSocketFactoryPort() {
395            return socketFactoryPort;
396        }
397    
398        /**
399         * Sets the port to connect to when using the specified socket factory.
400         * <p/>
401         * Specifies the port to connect to when using the specified socket
402         * factory. If not set, the default port will be used.
403         * <p/>
404         * Values that are set here will override any of the corresponding value
405         * that has been set in the properties.
406         *
407         * @param socketFactoryPort the port to connect to when using the specified socket factory
408         */
409        public void setSocketFactoryPort(Integer socketFactoryPort) {
410            this.socketFactoryPort = socketFactoryPort;
411        }
412    
413        /**
414         * Add the overrides from the member variables to the properties file.
415         */
416        public void addOverrides(Properties props) {
417            super.addOverrides(props);
418    
419            if (port != null) props.setProperty(POP3_PORT, port.toString());
420            if (connectionTimeout != null) props.setProperty(POP3_CONNECTION_TIMEOUT, connectionTimeout.toString());
421            if (timeout != null) props.setProperty(POP3_TIMEOUT, timeout.toString());
422            if (rsetBeforeQuit != null) props.setProperty(POP3_RESET, rsetBeforeQuit.toString());
423            if (messageClass != null) props.setProperty(POP3_MESSAGE_CLASS, messageClass);
424            if (localaddress != null) props.setProperty(POP3_LOCALADDRESS, localaddress);
425            if (localport != null) props.setProperty(POP3_LOCALPORT, localport.toString());
426            if (apopEnable != null) props.setProperty(POP3_APOP, apopEnable.toString());
427            if (socketFactoryClass != null) props.setProperty(POP3_FACTORY_CLASS, socketFactoryClass);
428            if (socketFactoryFallback != null) props.setProperty(POP3_FACTORY_FALLBACK, socketFactoryFallback.toString());
429            if (socketFactoryPort != null) props.setProperty(POP3_FACTORY_PORT, socketFactoryPort.toString());
430        }
431    
432        public void doStart() throws Exception {
433            log.debug("Started " + getObjectName());
434        }
435    
436        public void doStop() throws Exception {
437            log.debug("Stopped " + getObjectName());
438        }
439    
440        public void doFail() {
441            log.warn("Failed " + getObjectName());
442        }
443    
444        public static final GBeanInfo GBEAN_INFO;
445    
446        static {
447            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(POP3StoreGBean.class, ProtocolGBean.GBEAN_INFO);
448    
449            infoFactory.addAttribute(GBEAN_PORT, Integer.class, true);
450            infoFactory.addAttribute(GBEAN_CONNECTION_TIMEOUT, Integer.class, true);
451            infoFactory.addAttribute(GBEAN_TIMEOUT, Integer.class, true);
452            infoFactory.addAttribute(GBEAN_RESET, Boolean.class, true);
453            infoFactory.addAttribute(GBEAN_MESSAGE_CLASS, String.class, true);
454            infoFactory.addAttribute(GBEAN_LOCALADDRESS, String.class, true);
455            infoFactory.addAttribute(GBEAN_LOCALPORT, Integer.class, true);
456            infoFactory.addAttribute(GBEAN_APOP, Boolean.class, true);
457            infoFactory.addAttribute(GBEAN_FACTORY_CLASS, String.class, true);
458            infoFactory.addAttribute(GBEAN_FACTORY_FALLBACK, Boolean.class, true);
459            infoFactory.addAttribute(GBEAN_FACTORY_PORT, Integer.class, true);
460    
461            infoFactory.addAttribute(GBEAN_OBJECTNAME, String.class, false);
462            infoFactory.addAttribute(GBEAN_PROTOCOL, String.class, true);
463            infoFactory.addAttribute(GBEAN_PROPERTIES, Properties.class, true);
464            infoFactory.addAttribute(GBEAN_HOST, String.class, true);
465            infoFactory.addAttribute(GBEAN_USER, String.class, true);
466            infoFactory.addOperation(GBEAN_ADD_OVERRIDES, new Class[]{Properties.class});
467    
468            infoFactory.setConstructor(new String[]{GBEAN_OBJECTNAME, GBEAN_PROPERTIES, GBEAN_HOST, GBEAN_USER,
469                                                    GBEAN_PORT,
470                                                    GBEAN_CONNECTION_TIMEOUT,
471                                                    GBEAN_TIMEOUT,
472                                                    GBEAN_RESET,
473                                                    GBEAN_MESSAGE_CLASS,
474                                                    GBEAN_LOCALADDRESS,
475                                                    GBEAN_LOCALPORT,
476                                                    GBEAN_APOP,
477                                                    GBEAN_FACTORY_CLASS,
478                                                    GBEAN_FACTORY_FALLBACK,
479                                                    GBEAN_FACTORY_PORT});
480    
481            GBEAN_INFO = infoFactory.getBeanInfo();
482        }
483    
484        public static GBeanInfo getGBeanInfo() {
485            return GBEAN_INFO;
486        }
487    }