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