1 17 18 package org.apache.james.nntpserver; 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.nntpserver.repository.NNTPRepository; 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 47 51 public class NNTPServer extends AbstractJamesService implements Component, NNTPServerMBean { 52 53 56 private boolean authRequired = false; 57 58 61 private NNTPRepository repo; 62 63 66 private UsersRepository userRepository = null; 67 68 71 private Pool theHandlerPool = null; 72 73 76 private ObjectFactory theHandlerFactory = new NNTPHandlerFactory(); 77 78 81 private WatchdogFactory theWatchdogFactory; 82 83 86 private NNTPHandlerConfigurationData theConfigData 87 = new NNTPHandlerConfigurationDataImpl(); 88 89 92 public void compose( final ComponentManager componentManager ) 93 throws ComponentException { 94 super.compose(componentManager); 95 UsersStore usersStore = (UsersStore)componentManager.lookup(UsersStore.ROLE); 96 userRepository = usersStore.getRepository("LocalUsers"); 97 if (userRepository == null) { 98 throw new ComponentException("The user repository could not be found."); 99 } 100 101 repo = (NNTPRepository)componentManager 102 .lookup("org.apache.james.nntpserver.repository.NNTPRepository"); 103 104 } 105 106 109 public void configure(final Configuration configuration) throws ConfigurationException { 110 super.configure(configuration); 111 if (isEnabled()) { 112 Configuration handlerConfiguration = configuration.getChild("handler"); 113 authRequired = 114 handlerConfiguration.getChild("authRequired").getValueAsBoolean(false); 115 if (getLogger().isDebugEnabled()) { 116 if (authRequired) { 117 getLogger().debug("NNTP Server requires authentication."); 118 } else { 119 getLogger().debug("NNTP Server doesn't require authentication."); 120 } 121 } 122 } 123 } 124 125 128 public void initialize() throws Exception { 129 super.initialize(); 130 if (!isEnabled()) { 131 return; 132 } 133 134 if (connectionLimit != null) { 135 theHandlerPool = new HardResourceLimitingPool(theHandlerFactory, 5, connectionLimit.intValue()); 136 getLogger().debug("Using a bounded pool for NNTP handlers with upper limit " + connectionLimit.intValue()); 137 } else { 138 theHandlerPool = new DefaultPool(theHandlerFactory, null, 5, 30); 141 getLogger().debug("Using an unbounded pool for NNTP handlers."); 142 } 143 if (theHandlerPool instanceof LogEnabled) { 144 ((LogEnabled)theHandlerPool).enableLogging(getLogger()); 145 } 146 if (theHandlerPool instanceof Initializable) { 147 ((Initializable)theHandlerPool).initialize(); 148 } 149 150 theWatchdogFactory = getWatchdogFactory(); 151 } 152 153 156 protected int getDefaultPort() { 157 return 119; 158 } 159 160 163 public String getServiceType() { 164 return "NNTP Service"; 165 } 166 167 170 protected ConnectionHandler newHandler() 171 throws Exception { 172 NNTPHandler theHandler = (NNTPHandler)theHandlerPool.get(); 173 174 Watchdog theWatchdog = theWatchdogFactory.getWatchdog(theHandler.getWatchdogTarget()); 175 176 theHandler.setConfigurationData(theConfigData); 177 theHandler.setWatchdog(theWatchdog); 178 return theHandler; 179 } 180 181 184 public void releaseConnectionHandler( ConnectionHandler connectionHandler ) { 185 if (!(connectionHandler instanceof NNTPHandler)) { 186 throw new IllegalArgumentException ("Attempted to return non-NNTPHandler to pool."); 187 } 188 theHandlerPool.put((Poolable)connectionHandler); 189 } 190 191 194 private static class NNTPHandlerFactory 195 implements ObjectFactory { 196 197 200 public Object newInstance() throws Exception { 201 return new NNTPHandler(); 202 } 203 204 207 public Class getCreatedClass() { 208 return NNTPHandler.class; 209 } 210 211 214 public void decommission( Object object ) throws Exception { 215 return; 216 } 217 } 218 219 222 private class NNTPHandlerConfigurationDataImpl 223 implements NNTPHandlerConfigurationData { 224 225 228 public String getHelloName() { 229 return NNTPServer.this.helloName; 230 } 231 232 235 public boolean isAuthRequired() { 236 return NNTPServer.this.authRequired; 237 } 238 239 242 public UsersRepository getUsersRepository() { 243 return NNTPServer.this.userRepository; 244 } 245 246 249 public NNTPRepository getNNTPRepository() { 250 return NNTPServer.this.repo; 251 } 252 253 } 254 } 255 | Popular Tags |