View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with 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,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.pop3server;
21  
22  import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
23  import org.apache.avalon.excalibur.pool.DefaultPool;
24  import org.apache.avalon.excalibur.pool.HardResourceLimitingPool;
25  import org.apache.avalon.excalibur.pool.ObjectFactory;
26  import org.apache.avalon.excalibur.pool.Pool;
27  import org.apache.avalon.excalibur.pool.Poolable;
28  import org.apache.avalon.framework.activity.Initializable;
29  import org.apache.avalon.framework.configuration.Configuration;
30  import org.apache.avalon.framework.configuration.ConfigurationException;
31  import org.apache.avalon.framework.service.ServiceException;
32  import org.apache.avalon.framework.service.ServiceManager;
33  import org.apache.avalon.framework.logger.LogEnabled;
34  
35  import org.apache.james.core.AbstractJamesService;
36  import org.apache.james.services.MailServer;
37  import org.apache.james.services.UsersRepository;
38  import org.apache.james.util.watchdog.Watchdog;
39  import org.apache.james.util.watchdog.WatchdogFactory;
40  
41  /***
42   * <p>Accepts POP3 connections on a server socket and dispatches them to POP3Handlers.</p>
43   *
44   * <p>Also responsible for loading and parsing POP3 specific configuration.</p>
45   *
46   * @version 1.0.0, 24/04/1999
47   */
48  public class POP3Server extends AbstractJamesService implements POP3ServerMBean {
49  
50      /***
51       * The internal mail server service
52       */
53      private MailServer mailServer;
54  
55      /***
56       * The user repository for this server - used to authenticate users.
57       */
58      private UsersRepository users;
59  
60      /***
61       * The number of bytes to read before resetting
62       * the connection timeout timer.  Defaults to
63       * 20 KB.
64       */
65      private int lengthReset = 20 * 1024;
66  
67      /***
68       * The pool used to provide POP3 Handler objects
69       */
70      private Pool theHandlerPool = null;
71  
72      /***
73       * The factory used to provide POP3 Handler objects
74       */
75      private ObjectFactory theHandlerFactory = new POP3HandlerFactory();
76  
77      /***
78       * The factory used to generate Watchdog objects
79       */
80      private WatchdogFactory theWatchdogFactory;
81  
82      /***
83       * The configuration data to be passed to the handler
84       */
85      private POP3HandlerConfigurationData theConfigData
86          = new POP3HandlerConfigurationDataImpl();
87  
88      /***
89       * @see org.apache.avalon.framework.service.Serviceable#compose(ServiceManager)
90       */
91      public void service( final ServiceManager componentManager )
92          throws ServiceException {
93          super.service(componentManager);
94          mailServer = (MailServer)componentManager.lookup( MailServer.ROLE );
95          users = (UsersRepository)componentManager.lookup( UsersRepository.ROLE );
96      }
97  
98      /***
99       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
100      */
101     public void configure(final Configuration configuration) throws ConfigurationException {
102         super.configure(configuration);
103         if (isEnabled()) {
104             Configuration handlerConfiguration = configuration.getChild("handler");
105             lengthReset = handlerConfiguration.getChild("lengthReset").getValueAsInteger(lengthReset);
106             if (getLogger().isInfoEnabled()) {
107                 getLogger().info("The idle timeout will be reset every " + lengthReset + " bytes.");
108             }
109         }
110     }
111 
112     /***
113      * @see org.apache.avalon.framework.activity.Initializable#initialize()
114      */
115     public void initialize() throws Exception {
116         super.initialize();
117         if (!isEnabled()) {
118             return;
119         }
120 
121         if (connectionLimit != null) {
122             theHandlerPool = new HardResourceLimitingPool(theHandlerFactory, 5, connectionLimit.intValue());
123             getLogger().debug("Using a bounded pool for POP3 handlers with upper limit " + connectionLimit.intValue());
124         } else {
125             // NOTE: The maximum here is not a real maximum.  The handler pool will continue to
126             //       provide handlers beyond this value.
127             theHandlerPool = new DefaultPool(theHandlerFactory, null, 5, 30);
128             getLogger().debug("Using an unbounded pool for POP3 handlers.");
129         }
130         if (theHandlerPool instanceof LogEnabled) {
131             ((LogEnabled)theHandlerPool).enableLogging(getLogger());
132         }
133         if (theHandlerPool instanceof Initializable) {
134             ((Initializable)theHandlerPool).initialize();
135         }
136 
137         theWatchdogFactory = getWatchdogFactory();
138     }
139 
140     /***
141      * @see org.apache.james.core.AbstractJamesService#getDefaultPort()
142      */
143      protected int getDefaultPort() {
144         return 110;
145      }
146 
147     /***
148      * @see org.apache.james.core.AbstractJamesService#getServiceType()
149      */
150     public String getServiceType() {
151         return "POP3 Service";
152     }
153 
154     /***
155      * @see org.apache.avalon.cornerstone.services.connection.AbstractHandlerFactory#newHandler()
156      */
157     protected ConnectionHandler newHandler()
158             throws Exception {
159         POP3Handler theHandler = (POP3Handler)theHandlerPool.get();
160 
161         Watchdog theWatchdog = theWatchdogFactory.getWatchdog(theHandler.getWatchdogTarget());
162 
163         theHandler.setConfigurationData(theConfigData);
164 
165         theHandler.setWatchdog(theWatchdog);
166 
167         return theHandler;
168     }
169 
170     /***
171      * @see org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory#releaseConnectionHandler(ConnectionHandler)
172      */
173     public void releaseConnectionHandler( ConnectionHandler connectionHandler ) {
174         if (!(connectionHandler instanceof POP3Handler)) {
175             throw new IllegalArgumentException("Attempted to return non-POP3Handler to pool.");
176         }
177         theHandlerPool.put((Poolable)connectionHandler);
178     }
179 
180     /***
181      * The factory for producing handlers.
182      */
183     private static class POP3HandlerFactory
184         implements ObjectFactory {
185 
186         /***
187          * @see org.apache.avalon.excalibur.pool.ObjectFactory#newInstance()
188          */
189         public Object newInstance() throws Exception {
190             return new POP3Handler();
191         }
192 
193         /***
194          * @see org.apache.avalon.excalibur.pool.ObjectFactory#getCreatedClass()
195          */
196         public Class getCreatedClass() {
197             return POP3Handler.class;
198         }
199 
200         /***
201          * @see org.apache.avalon.excalibur.pool.ObjectFactory#decommision(Object)
202          */
203         public void decommission( Object object ) throws Exception {
204             return;
205         }
206     }
207 
208     /***
209      * A class to provide POP3 handler configuration to the handlers
210      */
211     private class POP3HandlerConfigurationDataImpl
212         implements POP3HandlerConfigurationData {
213 
214         /***
215          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getHelloName()
216          */
217         public String getHelloName() {
218             return POP3Server.this.helloName;
219         }
220 
221         /***
222          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getResetLength()
223          */
224         public int getResetLength() {
225             return POP3Server.this.lengthReset;
226         }
227 
228         /***
229          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getMailServer()
230          */
231         public MailServer getMailServer() {
232             return POP3Server.this.mailServer;
233         }
234 
235         /***
236          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getUsersRepository()
237          */
238         public UsersRepository getUsersRepository() {
239             return POP3Server.this.users;
240         }
241     }
242 }