KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > remotemanager > RemoteManager


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.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 JavaDoc;
43 import java.net.UnknownHostException JavaDoc;
44 import java.util.HashMap JavaDoc;
45
46 /**
47  * Provides a really rude network interface to administer James.
48  * Allow to add accounts.
49  * TODO: -improve protocol
50  * -add remove user
51  * -much more...
52  * @version 1.0.0, 24/04/1999
53  */

54 public class RemoteManager
55     extends AbstractJamesService implements Component, RemoteManagerMBean {
56
57     /**
58      * A HashMap of (user id, passwords) for James administrators
59      */

60     private HashMap JavaDoc adminAccounts = new HashMap JavaDoc();
61
62     /**
63      * The UsersStore that contains all UsersRepositories managed by this RemoteManager
64      */

65     private UsersStore usersStore;
66
67     /**
68      * The current UsersRepository being managed/viewed/modified
69      */

70     private UsersRepository users;
71
72     /**
73      * The reference to the internal MailServer service
74      */

75     private MailServer mailServer;
76
77     /**
78      * The pool used to provide RemoteManager Handler objects
79      */

80     private Pool theHandlerPool = null;
81
82     /**
83      * The pool used to provide RemoteManager Handler objects
84      */

85     private ObjectFactory theHandlerFactory = new RemoteManagerHandlerFactory();
86
87     /**
88      * The factory used to generate Watchdog objects
89      */

90     private WatchdogFactory theWatchdogFactory;
91
92     /**
93      * The configuration data to be passed to the handler
94      */

95     private RemoteManagerHandlerConfigurationData theConfigData
96         = new RemoteManagerHandlerConfigurationDataImpl();
97
98     /**
99      * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
100      */

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     /**
115      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
116      */

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     /**
133      * @see org.apache.avalon.framework.activity.Initializable#initialize()
134      */

135     public void initialize() throws Exception JavaDoc {
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             // NOTE: The maximum here is not a real maximum. The handler pool will continue to
146
// provide handlers beyond this value.
147
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     /**
161      * @see org.apache.james.core.AbstractJamesService#getDefaultPort()
162      */

163      protected int getDefaultPort() {
164         return 4555;
165      }
166
167     /**
168      * @see org.apache.james.core.AbstractJamesService#getServiceType()
169      */

170     public String JavaDoc getServiceType() {
171         return "Remote Manager Service";
172     }
173
174     /**
175      * @see org.apache.avalon.cornerstone.services.connection.AbstractHandlerFactory#newHandler()
176      */

177     protected ConnectionHandler newHandler()
178             throws Exception JavaDoc {
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     /**
190      * @see org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory#releaseConnectionHandler(ConnectionHandler)
191      */

192     public void releaseConnectionHandler( ConnectionHandler connectionHandler ) {
193         if (!(connectionHandler instanceof RemoteManagerHandler)) {
194             throw new IllegalArgumentException JavaDoc("Attempted to return non-RemoteManagerHandler to pool.");
195         }
196         theHandlerPool.put((Poolable)connectionHandler);
197     }
198
199     /**
200      * The factory for producing handlers.
201      */

202     private static class RemoteManagerHandlerFactory
203         implements ObjectFactory {
204
205         /**
206          * @see org.apache.avalon.excalibur.pool.ObjectFactory#newInstance()
207          */

208         public Object JavaDoc newInstance() throws Exception JavaDoc {
209             return new RemoteManagerHandler();
210         }
211
212         /**
213          * @see org.apache.avalon.excalibur.pool.ObjectFactory#getCreatedClass()
214          */

215         public Class JavaDoc getCreatedClass() {
216             return RemoteManagerHandler.class;
217         }
218
219         /**
220          * @see org.apache.avalon.excalibur.pool.ObjectFactory#decommision(Object)
221          */

222         public void decommission( Object JavaDoc object ) throws Exception JavaDoc {
223             return;
224         }
225     }
226
227     /**
228      * A class to provide RemoteManager handler configuration to the handlers
229      */

230     private class RemoteManagerHandlerConfigurationDataImpl
231         implements RemoteManagerHandlerConfigurationData {
232
233         /**
234          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getHelloName()
235          */

236         public String JavaDoc getHelloName() {
237             return RemoteManager.this.helloName;
238         }
239
240         /**
241          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getMailServer()
242          */

243         public MailServer getMailServer() {
244             return RemoteManager.this.mailServer;
245         }
246
247         /**
248          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersRepository()
249          */

250         public UsersRepository getUsersRepository() {
251             return RemoteManager.this.users;
252         }
253
254         /**
255          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersStore()
256          */

257         public UsersStore getUserStore() {
258             return RemoteManager.this.usersStore;
259         }
260
261         /**
262          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getAdministrativeAccountData()
263          */

264         public HashMap JavaDoc getAdministrativeAccountData() {
265             return RemoteManager.this.adminAccounts;
266         }
267
268     }
269 }
270
Popular Tags