KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > pop3server > POP3Server


1 /***********************************************************************
2  * Copyright (c) 1999-2004 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.Disposable;
27 import org.apache.avalon.framework.activity.Initializable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.component.Component;
31 import org.apache.avalon.framework.component.ComponentException;
32 import org.apache.avalon.framework.component.ComponentManager;
33 import org.apache.avalon.framework.component.Composable;
34 import org.apache.avalon.framework.logger.LogEnabled;
35
36 import org.apache.james.core.AbstractJamesService;
37 import org.apache.james.services.MailServer;
38 import org.apache.james.services.UsersRepository;
39 import org.apache.james.services.UsersStore;
40 import org.apache.james.util.watchdog.Watchdog;
41 import org.apache.james.util.watchdog.WatchdogFactory;
42 import org.apache.james.util.watchdog.WatchdogTarget;
43
44 import java.net.InetAddress JavaDoc;
45 import java.net.UnknownHostException JavaDoc;
46 /**
47  * <p>Accepts POP3 connections on a server socket and dispatches them to POP3Handlers.</p>
48  *
49  * <p>Also responsible for loading and parsing POP3 specific configuration.</p>
50  *
51  * @version 1.0.0, 24/04/1999
52  */

53 public class POP3Server extends AbstractJamesService implements Component, POP3ServerMBean {
54
55     /**
56      * The internal mail server service
57      */

58     private MailServer mailServer;
59
60     /**
61      * The user repository for this server - used to authenticate users.
62      */

63     private UsersRepository users;
64
65     /**
66      * The number of bytes to read before resetting
67      * the connection timeout timer. Defaults to
68      * 20 KB.
69      */

70     private int lengthReset = 20 * 1024;
71
72     /**
73      * The pool used to provide POP3 Handler objects
74      */

75     private Pool theHandlerPool = null;
76
77     /**
78      * The factory used to provide POP3 Handler objects
79      */

80     private ObjectFactory theHandlerFactory = new POP3HandlerFactory();
81
82     /**
83      * The factory used to generate Watchdog objects
84      */

85     private WatchdogFactory theWatchdogFactory;
86
87     /**
88      * The configuration data to be passed to the handler
89      */

90     private POP3HandlerConfigurationData theConfigData
91         = new POP3HandlerConfigurationDataImpl();
92
93     /**
94      * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
95      */

96     public void compose( final ComponentManager componentManager )
97         throws ComponentException {
98         super.compose(componentManager);
99         mailServer = (MailServer)componentManager.
100             lookup( "org.apache.james.services.MailServer" );
101         UsersStore usersStore = (UsersStore)componentManager.
102             lookup( "org.apache.james.services.UsersStore" );
103         users = usersStore.getRepository("LocalUsers");
104         if (users == null) {
105             throw new ComponentException("The user repository could not be found.");
106         }
107     }
108
109     /**
110      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
111      */

112     public void configure(final Configuration configuration) throws ConfigurationException {
113         super.configure(configuration);
114         if (isEnabled()) {
115             Configuration handlerConfiguration = configuration.getChild("handler");
116             lengthReset = handlerConfiguration.getChild("lengthReset").getValueAsInteger(lengthReset);
117             if (getLogger().isInfoEnabled()) {
118                 getLogger().info("The idle timeout will be reset every " + lengthReset + " bytes.");
119             }
120         }
121     }
122
123     /**
124      * @see org.apache.avalon.framework.activity.Initializable#initialize()
125      */

126     public void initialize() throws Exception JavaDoc {
127         super.initialize();
128         if (!isEnabled()) {
129             return;
130         }
131
132         if (connectionLimit != null) {
133             theHandlerPool = new HardResourceLimitingPool(theHandlerFactory, 5, connectionLimit.intValue());
134             getLogger().debug("Using a bounded pool for POP3 handlers with upper limit " + connectionLimit.intValue());
135         } else {
136             // NOTE: The maximum here is not a real maximum. The handler pool will continue to
137
// provide handlers beyond this value.
138
theHandlerPool = new DefaultPool(theHandlerFactory, null, 5, 30);
139             getLogger().debug("Using an unbounded pool for POP3 handlers.");
140         }
141         if (theHandlerPool instanceof LogEnabled) {
142             ((LogEnabled)theHandlerPool).enableLogging(getLogger());
143         }
144         if (theHandlerPool instanceof Initializable) {
145             ((Initializable)theHandlerPool).initialize();
146         }
147
148         theWatchdogFactory = getWatchdogFactory();
149     }
150
151     /**
152      * @see org.apache.james.core.AbstractJamesService#getDefaultPort()
153      */

154      protected int getDefaultPort() {
155         return 110;
156      }
157
158     /**
159      * @see org.apache.james.core.AbstractJamesService#getServiceType()
160      */

161     public String JavaDoc getServiceType() {
162         return "POP3 Service";
163     }
164
165     /**
166      * @see org.apache.avalon.cornerstone.services.connection.AbstractHandlerFactory#newHandler()
167      */

168     protected ConnectionHandler newHandler()
169             throws Exception JavaDoc {
170         POP3Handler theHandler = (POP3Handler)theHandlerPool.get();
171
172         Watchdog theWatchdog = theWatchdogFactory.getWatchdog(theHandler.getWatchdogTarget());
173
174         theHandler.setConfigurationData(theConfigData);
175
176         theHandler.setWatchdog(theWatchdog);
177
178         return theHandler;
179     }
180
181     /**
182      * @see org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory#releaseConnectionHandler(ConnectionHandler)
183      */

184     public void releaseConnectionHandler( ConnectionHandler connectionHandler ) {
185         if (!(connectionHandler instanceof POP3Handler)) {
186             throw new IllegalArgumentException JavaDoc("Attempted to return non-POP3Handler to pool.");
187         }
188         theHandlerPool.put((Poolable)connectionHandler);
189     }
190
191     /**
192      * The factory for producing handlers.
193      */

194     private static class POP3HandlerFactory
195         implements ObjectFactory {
196
197         /**
198          * @see org.apache.avalon.excalibur.pool.ObjectFactory#newInstance()
199          */

200         public Object JavaDoc newInstance() throws Exception JavaDoc {
201             return new POP3Handler();
202         }
203
204         /**
205          * @see org.apache.avalon.excalibur.pool.ObjectFactory#getCreatedClass()
206          */

207         public Class JavaDoc getCreatedClass() {
208             return POP3Handler.class;
209         }
210
211         /**
212          * @see org.apache.avalon.excalibur.pool.ObjectFactory#decommision(Object)
213          */

214         public void decommission( Object JavaDoc object ) throws Exception JavaDoc {
215             return;
216         }
217     }
218
219     /**
220      * A class to provide POP3 handler configuration to the handlers
221      */

222     private class POP3HandlerConfigurationDataImpl
223         implements POP3HandlerConfigurationData {
224
225         /**
226          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getHelloName()
227          */

228         public String JavaDoc getHelloName() {
229             return POP3Server.this.helloName;
230         }
231
232         /**
233          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getResetLength()
234          */

235         public int getResetLength() {
236             return POP3Server.this.lengthReset;
237         }
238
239         /**
240          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getMailServer()
241          */

242         public MailServer getMailServer() {
243             return POP3Server.this.mailServer;
244         }
245
246         /**
247          * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getUsersRepository()
248          */

249         public UsersRepository getUsersRepository() {
250             return POP3Server.this.users;
251         }
252     }
253 }
254
Popular Tags