1 17 18 package org.apache.james.remotemanager; 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.*; 38 import org.apache.james.util.watchdog.Watchdog; 39 import org.apache.james.util.watchdog.WatchdogFactory; 40 import org.apache.james.util.watchdog.WatchdogTarget; 41 42 import java.net.InetAddress ; 43 import java.net.UnknownHostException ; 44 import java.util.HashMap ; 45 46 54 public class RemoteManager 55 extends AbstractJamesService implements Component, RemoteManagerMBean { 56 57 60 private HashMap adminAccounts = new HashMap (); 61 62 65 private UsersStore usersStore; 66 67 70 private UsersRepository users; 71 72 75 private MailServer mailServer; 76 77 80 private Pool theHandlerPool = null; 81 82 85 private ObjectFactory theHandlerFactory = new RemoteManagerHandlerFactory(); 86 87 90 private WatchdogFactory theWatchdogFactory; 91 92 95 private RemoteManagerHandlerConfigurationData theConfigData 96 = new RemoteManagerHandlerConfigurationDataImpl(); 97 98 101 public void compose( final ComponentManager componentManager ) 102 throws ComponentException { 103 super.compose(componentManager); 104 mailServer = (MailServer)componentManager. 105 lookup( "org.apache.james.services.MailServer" ); 106 usersStore = (UsersStore)componentManager. 107 lookup( "org.apache.james.services.UsersStore" ); 108 users = usersStore.getRepository("LocalUsers"); 109 if (users == null) { 110 throw new ComponentException("The user repository could not be found."); 111 } 112 } 113 114 117 public void configure( final Configuration configuration ) 118 throws ConfigurationException { 119 120 super.configure(configuration); 121 if (isEnabled()) { 122 Configuration handlerConfiguration = configuration.getChild("handler"); 123 Configuration admin = handlerConfiguration.getChild( "administrator_accounts" ); 124 Configuration[] accounts = admin.getChildren( "account" ); 125 for ( int i = 0; i < accounts.length; i++ ) { 126 adminAccounts.put( accounts[ i ].getAttribute( "login" ), 127 accounts[ i ].getAttribute( "password" ) ); 128 } 129 } 130 } 131 132 135 public void initialize() throws Exception { 136 super.initialize(); 137 if (!isEnabled()) { 138 return; 139 } 140 141 if (connectionLimit != null) { 142 theHandlerPool = new HardResourceLimitingPool(theHandlerFactory, 5, connectionLimit.intValue()); 143 getLogger().debug("Using a bounded pool for RemoteManager handlers with upper limit " + connectionLimit.intValue()); 144 } else { 145 theHandlerPool = new DefaultPool(theHandlerFactory, null, 5, 30); 148 getLogger().debug("Using an unbounded pool for RemoteManager handlers."); 149 } 150 if (theHandlerPool instanceof LogEnabled) { 151 ((LogEnabled)theHandlerPool).enableLogging(getLogger()); 152 } 153 if (theHandlerPool instanceof Initializable) { 154 ((Initializable)theHandlerPool).initialize(); 155 } 156 157 theWatchdogFactory = getWatchdogFactory(); 158 } 159 160 163 protected int getDefaultPort() { 164 return 4555; 165 } 166 167 170 public String getServiceType() { 171 return "Remote Manager Service"; 172 } 173 174 177 protected ConnectionHandler newHandler() 178 throws Exception { 179 RemoteManagerHandler theHandler = (RemoteManagerHandler)theHandlerPool.get(); 180 theHandler.enableLogging(getLogger()); 181 182 Watchdog theWatchdog = theWatchdogFactory.getWatchdog(theHandler.getWatchdogTarget()); 183 184 theHandler.setConfigurationData(theConfigData); 185 theHandler.setWatchdog(theWatchdog); 186 return theHandler; 187 } 188 189 192 public void releaseConnectionHandler( ConnectionHandler connectionHandler ) { 193 if (!(connectionHandler instanceof RemoteManagerHandler)) { 194 throw new IllegalArgumentException ("Attempted to return non-RemoteManagerHandler to pool."); 195 } 196 theHandlerPool.put((Poolable)connectionHandler); 197 } 198 199 202 private static class RemoteManagerHandlerFactory 203 implements ObjectFactory { 204 205 208 public Object newInstance() throws Exception { 209 return new RemoteManagerHandler(); 210 } 211 212 215 public Class getCreatedClass() { 216 return RemoteManagerHandler.class; 217 } 218 219 222 public void decommission( Object object ) throws Exception { 223 return; 224 } 225 } 226 227 230 private class RemoteManagerHandlerConfigurationDataImpl 231 implements RemoteManagerHandlerConfigurationData { 232 233 236 public String getHelloName() { 237 return RemoteManager.this.helloName; 238 } 239 240 243 public MailServer getMailServer() { 244 return RemoteManager.this.mailServer; 245 } 246 247 250 public UsersRepository getUsersRepository() { 251 return RemoteManager.this.users; 252 } 253 254 257 public UsersStore getUserStore() { 258 return RemoteManager.this.usersStore; 259 } 260 261 264 public HashMap getAdministrativeAccountData() { 265 return RemoteManager.this.adminAccounts; 266 } 267 268 } 269 } 270 | Popular Tags |