1 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 ; 45 import java.net.UnknownHostException ; 46 53 public class POP3Server extends AbstractJamesService implements Component, POP3ServerMBean { 54 55 58 private MailServer mailServer; 59 60 63 private UsersRepository users; 64 65 70 private int lengthReset = 20 * 1024; 71 72 75 private Pool theHandlerPool = null; 76 77 80 private ObjectFactory theHandlerFactory = new POP3HandlerFactory(); 81 82 85 private WatchdogFactory theWatchdogFactory; 86 87 90 private POP3HandlerConfigurationData theConfigData 91 = new POP3HandlerConfigurationDataImpl(); 92 93 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 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 126 public void initialize() throws Exception { 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 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 154 protected int getDefaultPort() { 155 return 110; 156 } 157 158 161 public String getServiceType() { 162 return "POP3 Service"; 163 } 164 165 168 protected ConnectionHandler newHandler() 169 throws Exception { 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 184 public void releaseConnectionHandler( ConnectionHandler connectionHandler ) { 185 if (!(connectionHandler instanceof POP3Handler)) { 186 throw new IllegalArgumentException ("Attempted to return non-POP3Handler to pool."); 187 } 188 theHandlerPool.put((Poolable)connectionHandler); 189 } 190 191 194 private static class POP3HandlerFactory 195 implements ObjectFactory { 196 197 200 public Object newInstance() throws Exception { 201 return new POP3Handler(); 202 } 203 204 207 public Class getCreatedClass() { 208 return POP3Handler.class; 209 } 210 211 214 public void decommission( Object object ) throws Exception { 215 return; 216 } 217 } 218 219 222 private class POP3HandlerConfigurationDataImpl 223 implements POP3HandlerConfigurationData { 224 225 228 public String getHelloName() { 229 return POP3Server.this.helloName; 230 } 231 232 235 public int getResetLength() { 236 return POP3Server.this.lengthReset; 237 } 238 239 242 public MailServer getMailServer() { 243 return POP3Server.this.mailServer; 244 } 245 246 249 public UsersRepository getUsersRepository() { 250 return POP3Server.this.users; 251 } 252 } 253 } 254 | Popular Tags |