View Javadoc

1   /************************************************************************
2    * Copyright (c) 1999-2006 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.Initializable;
27  import org.apache.avalon.framework.service.ServiceException;
28  import org.apache.avalon.framework.service.ServiceManager;
29  import org.apache.avalon.framework.configuration.Configuration;
30  import org.apache.avalon.framework.configuration.ConfigurationException;
31  import org.apache.avalon.framework.logger.LogEnabled;
32  
33  import org.apache.james.core.AbstractJamesService;
34  import org.apache.james.services.MailServer;
35  import org.apache.james.services.UsersRepository;
36  import org.apache.james.services.UsersStore;
37  import org.apache.james.util.watchdog.Watchdog;
38  import org.apache.james.util.watchdog.WatchdogFactory;
39  
40  import java.util.HashMap;
41  
42  /***
43   * Provides a really rude network interface to administer James.
44   * Allow to add accounts.
45   * TODO: -improve protocol
46   *       -add remove user
47   *       -much more...
48   * @version 1.0.0, 24/04/1999
49   */
50  public class RemoteManager
51      extends AbstractJamesService implements RemoteManagerMBean {
52  
53      /***
54       * A HashMap of (user id, passwords) for James administrators
55       */
56      private HashMap adminAccounts = new HashMap();
57  
58      /***
59       * The UsersStore that contains all UsersRepositories managed by this RemoteManager
60       */
61      private UsersStore usersStore;
62  
63      /***
64       * The current UsersRepository being managed/viewed/modified
65       */
66      private UsersRepository users;
67  
68      /***
69       * The service prompt to be displayed when waiting for input.
70       */
71      private String prompt = "";
72      
73      /***
74       * The reference to the internal MailServer service
75       */
76      private MailServer mailServer;
77  
78      /***
79       * The pool used to provide RemoteManager Handler objects
80       */
81      private Pool theHandlerPool = null;
82  
83      /***
84       * The pool used to provide RemoteManager Handler objects
85       */
86      private ObjectFactory theHandlerFactory = new RemoteManagerHandlerFactory();
87  
88      /***
89       * The factory used to generate Watchdog objects
90       */
91      private WatchdogFactory theWatchdogFactory;
92  
93      /***
94       * The configuration data to be passed to the handler
95       */
96      private RemoteManagerHandlerConfigurationData theConfigData
97          = new RemoteManagerHandlerConfigurationDataImpl();
98  
99      /***
100      * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
101      */
102     public void service( final ServiceManager componentManager )
103         throws ServiceException {
104         super.service(componentManager);
105         mailServer = (MailServer)componentManager.
106             lookup( MailServer.ROLE );
107         usersStore = (UsersStore)componentManager.
108             lookup( UsersStore.ROLE );
109         users = (UsersRepository) componentManager.lookup(UsersRepository.ROLE);
110         if (users == null) {
111             throw new ServiceException("","The user repository could not be found.");
112         }
113     }
114 
115     /***
116      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
117      */
118     public void configure( final Configuration configuration )
119         throws ConfigurationException {
120 
121         super.configure(configuration);
122         if (isEnabled()) {
123             Configuration handlerConfiguration = configuration.getChild("handler");
124             Configuration admin = handlerConfiguration.getChild( "administrator_accounts" );
125             Configuration[] accounts = admin.getChildren( "account" );
126             for ( int i = 0; i < accounts.length; i++ ) {
127                 adminAccounts.put( accounts[ i ].getAttribute( "login" ),
128                                    accounts[ i ].getAttribute( "password" ) );
129             }
130             Configuration promtConfiguration = handlerConfiguration.getChild("prompt", false);
131             if (promtConfiguration != null) prompt = promtConfiguration.getValue();
132             if (prompt == null) prompt = ""; 
133             else if (!prompt.equals("") && !prompt.endsWith(" ")) prompt += " "; 
134         }
135     }
136 
137     /***
138      * @see org.apache.avalon.framework.activity.Initializable#initialize()
139      */
140     public void initialize() throws Exception {
141         super.initialize();
142         if (!isEnabled()) {
143             return;
144         }
145 
146         if (connectionLimit != null) {
147             theHandlerPool = new HardResourceLimitingPool(theHandlerFactory, 5, connectionLimit.intValue());
148             getLogger().debug("Using a bounded pool for RemoteManager handlers with upper limit " + connectionLimit.intValue());
149         } else {
150             // NOTE: The maximum here is not a real maximum.  The handler pool will continue to
151             //       provide handlers beyond this value.
152             theHandlerPool = new DefaultPool(theHandlerFactory, null, 5, 30);
153             getLogger().debug("Using an unbounded pool for RemoteManager handlers.");
154         }
155         if (theHandlerPool instanceof LogEnabled) {
156             ((LogEnabled)theHandlerPool).enableLogging(getLogger());
157         }
158         if (theHandlerPool instanceof Initializable) {
159             ((Initializable)theHandlerPool).initialize();
160         }
161 
162         theWatchdogFactory = getWatchdogFactory();
163     }
164 
165     /***
166      * @see org.apache.james.core.AbstractJamesService#getDefaultPort()
167      */
168      protected int getDefaultPort() {
169         return 4555;
170      }
171 
172     /***
173      * @see org.apache.james.core.AbstractJamesService#getServiceType()
174      */
175     public String getServiceType() {
176         return "Remote Manager Service";
177     }
178 
179     /***
180      * @see org.apache.avalon.cornerstone.services.connection.AbstractHandlerFactory#newHandler()
181      */
182     protected ConnectionHandler newHandler()
183             throws Exception {
184         RemoteManagerHandler theHandler = (RemoteManagerHandler)theHandlerPool.get();
185         theHandler.enableLogging(getLogger());
186 
187         Watchdog theWatchdog = theWatchdogFactory.getWatchdog(theHandler.getWatchdogTarget());
188 
189         theHandler.setConfigurationData(theConfigData);
190         theHandler.setWatchdog(theWatchdog);
191         return theHandler;
192     }
193 
194     /***
195      * @see org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory#releaseConnectionHandler(ConnectionHandler)
196      */
197     public void releaseConnectionHandler( ConnectionHandler connectionHandler ) {
198         if (!(connectionHandler instanceof RemoteManagerHandler)) {
199             throw new IllegalArgumentException("Attempted to return non-RemoteManagerHandler to pool.");
200         }
201         theHandlerPool.put((Poolable)connectionHandler);
202     }
203 
204     /***
205      * The factory for producing handlers.
206      */
207     private static class RemoteManagerHandlerFactory
208         implements ObjectFactory {
209 
210         /***
211          * @see org.apache.avalon.excalibur.pool.ObjectFactory#newInstance()
212          */
213         public Object newInstance() throws Exception {
214             return new RemoteManagerHandler();
215         }
216 
217         /***
218          * @see org.apache.avalon.excalibur.pool.ObjectFactory#getCreatedClass()
219          */
220         public Class getCreatedClass() {
221             return RemoteManagerHandler.class;
222         }
223 
224         /***
225          * @see org.apache.avalon.excalibur.pool.ObjectFactory#decommision(Object)
226          */
227         public void decommission( Object object ) throws Exception {
228             return;
229         }
230     }
231 
232     /***
233      * A class to provide RemoteManager handler configuration to the handlers
234      */
235     private class RemoteManagerHandlerConfigurationDataImpl
236         implements RemoteManagerHandlerConfigurationData {
237 
238         /***
239          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getHelloName()
240          */
241         public String getHelloName() {
242             return RemoteManager.this.helloName;
243         }
244 
245         /***
246          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getMailServer()
247          */
248         public MailServer getMailServer() {
249             return RemoteManager.this.mailServer;
250         }
251 
252         /***
253          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersRepository()
254          */
255         public UsersRepository getUsersRepository() {
256             return RemoteManager.this.users;
257         }
258 
259         /***
260          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersStore()
261          */
262         public UsersStore getUserStore() {
263             return RemoteManager.this.usersStore;
264         }
265 
266         /***
267          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getAdministrativeAccountData()
268          */
269         public HashMap getAdministrativeAccountData() {
270             return RemoteManager.this.adminAccounts;
271         }
272 
273         /***
274          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getPrompt()
275          */
276         public String getPrompt() {
277             return RemoteManager.this.prompt;
278         }
279 
280     }
281 }