View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.remotemanager;
23  
24  import org.apache.avalon.cornerstone.services.store.Store;
25  import org.apache.avalon.framework.configuration.Configurable;
26  import org.apache.avalon.framework.configuration.Configuration;
27  import org.apache.avalon.framework.configuration.ConfigurationException;
28  import org.apache.avalon.framework.logger.Logger;
29  import org.apache.avalon.framework.service.ServiceException;
30  import org.apache.avalon.framework.service.ServiceManager;
31  import org.apache.james.api.user.UsersRepository;
32  import org.apache.james.api.user.UsersStore;
33  import org.apache.james.api.vut.management.VirtualUserTableManagementService;
34  import org.apache.james.management.BayesianAnalyzerManagementService;
35  import org.apache.james.management.DomainListManagementService;
36  import org.apache.james.management.ProcessorManagementService;
37  import org.apache.james.management.SpoolManagementService;
38  import org.apache.james.services.MailServer;
39  import org.apache.james.socket.AbstractJamesService;
40  import org.apache.james.socket.ProtocolHandler;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.HashMap;
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 RemoteManagerMBean {
56  
57      /**
58       * A HashMap of (user id, passwords) for James administrators
59       */
60      private HashMap adminAccounts = new HashMap();
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 spool management service
74       */
75      private SpoolManagementService spoolManagement;
76  
77      /**
78       * The service prompt to be displayed when waiting for input.
79       */
80      private String prompt = "";
81  
82      /**
83       * The reference to the internal MailServer service
84       */
85      private MailServer mailServer;
86  
87      /**
88       * The reference to the Store
89       */
90      private Store store;
91      
92      private Command[] commands = {};
93      
94      /**
95       * reference to administration of Bayesian analyzer
96       */
97      private BayesianAnalyzerManagementService bayesianAnalyzerManagement;
98      
99      /**
100      * reference to administration of Processors
101      */
102     private ProcessorManagementService processorManagementService;
103 
104     private VirtualUserTableManagementService vutManagemenet;
105     
106     private DomainListManagementService domListManagement;
107     
108     /**
109      * Set the UserStore 
110      * 
111      * @param usersStore the UserStore
112      */
113     public void setUsersStore(UsersStore usersStore) {
114         this.usersStore = usersStore;
115     }
116 
117     /**
118      * Set the UsersRepository
119      * 
120      * @param users the UsersRepository
121      */
122     public void setUsers(UsersRepository users) {
123         this.users = users;
124     }
125 
126     /**
127      * Set the SpoolManagementService
128      * 
129      * @param spoolManagement the SpoolManagementService
130      */
131     public void setSpoolManagement(SpoolManagementService spoolManagement) {
132         this.spoolManagement = spoolManagement;
133     }
134 
135     /**
136      * Set the MailServer 
137      * 
138      * @param mailServer the MailServer
139      */
140     public void setMailServer(MailServer mailServer) {
141         this.mailServer = mailServer;
142     }
143 
144     /**
145      * Set the Store
146      * 
147      * @param store the Store
148      */
149     public void setStore(Store store) {
150         this.store = store;
151     }
152     
153     /**
154      * Set the BayesianAnalyzerManagementService
155      * 
156      * @param bayesianAnalyzerManagement the BayesianAnalyzerManagementService
157      */
158     public void setBayesianAnalyzerManagement(BayesianAnalyzerManagementService bayesianAnalyzerManagement) {
159         this.bayesianAnalyzerManagement = bayesianAnalyzerManagement;
160     }
161     
162     /**
163      * Set the ProcessorManagementService
164      * 
165      * @param processorManagement the ProcessorManagementService
166      */
167     public void setProcessorManagement(ProcessorManagementService processorManagement) {
168         this.processorManagementService = processorManagement;
169     }
170     
171     /**
172      * Set the VirtualUserTableManagementService
173      * 
174      * @param vutManagement the VirtualUserTableManagementService 
175      */
176     public void setVirtualUserTableManagement(VirtualUserTableManagementService vutManagement) {
177         this.vutManagemenet = vutManagement;
178     }
179     
180     /**
181      * Set the DomainListManagementService
182      * 
183      * @param domListManagement the DomainListManagementService 
184      */
185     public void setDomainListManagement(DomainListManagementService domListManagement) {
186         this.domListManagement = domListManagement;
187     }
188     
189     /**
190      * The configuration data to be passed to the handler
191      */
192     private RemoteManagerHandlerConfigurationData theConfigData
193         = new RemoteManagerHandlerConfigurationDataImpl();
194 
195     /**
196      * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
197      */
198     public void service( final ServiceManager componentManager )
199         throws ServiceException {
200         super.service(componentManager);
201         MailServer mailServer = (MailServer)componentManager.lookup(MailServer.ROLE );
202         setMailServer(mailServer);
203         Store store = (Store)componentManager.
204             lookup( Store.ROLE );
205         setStore(store);
206         UsersStore usersStore = (UsersStore)componentManager. lookup(UsersStore.ROLE );
207         setUsersStore(usersStore);
208         UsersRepository users = (UsersRepository) componentManager.lookup(UsersRepository.ROLE);
209         if (users == null) {
210             throw new ServiceException("","The user repository could not be found.");
211         }
212         setUsers(users);
213         SpoolManagementService spoolManagement = 
214             (SpoolManagementService) componentManager.lookup(SpoolManagementService.ROLE);
215         setSpoolManagement(spoolManagement);
216         
217         setBayesianAnalyzerManagement((BayesianAnalyzerManagementService) componentManager.lookup(BayesianAnalyzerManagementService.ROLE));     
218         setProcessorManagement((ProcessorManagementService) componentManager.lookup(ProcessorManagementService.ROLE)); 
219         setVirtualUserTableManagement((VirtualUserTableManagementService) componentManager.lookup(VirtualUserTableManagementService.ROLE));
220         setDomainListManagement((DomainListManagementService) componentManager.lookup(DomainListManagementService.ROLE));
221     }
222 
223     /**
224      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
225      */
226     public void configure( final Configuration configuration )
227         throws ConfigurationException {
228 
229         super.configure(configuration);
230         if (isEnabled()) {
231             Configuration handlerConfiguration = configuration.getChild("handler");
232             Configuration admin = handlerConfiguration.getChild( "administrator_accounts" );
233             Configuration[] accounts = admin.getChildren( "account" );
234             for ( int i = 0; i < accounts.length; i++ ) {
235                 adminAccounts.put( accounts[ i ].getAttribute( "login" ),
236                                    accounts[ i ].getAttribute( "password" ) );
237             }
238             Configuration promtConfiguration = handlerConfiguration.getChild("prompt", false);
239             if (promtConfiguration != null) prompt = promtConfiguration.getValue();
240             if (prompt == null) prompt = ""; 
241             else if (!prompt.equals("") && !prompt.endsWith(" ")) prompt += " "; 
242             configureCommands(configuration);
243         }
244     }
245   
246     private void configureCommands(final Configuration configuration) throws ConfigurationException {
247         Collection commands = new ArrayList();
248         Configuration[] commandConfigurations = configuration.getChildren( "command" );
249         if (commandConfigurations != null) {
250             for(int i=0;i<commandConfigurations.length;i++) {
251                 final Configuration commandConfiguration = commandConfigurations[i];
252                 Configuration classConfiguration 
253                 = commandConfiguration.getChild( "class-name" );
254                 String className = classConfiguration.getValue();
255                 if (className != null) {
256                     try {
257                         Command command 
258                         = (Command) Class.forName(className).newInstance();
259                         if (command instanceof Configurable) {
260                             Configurable configurable = (Configurable) command;
261                             configurable.configure(commandConfiguration);
262                         }
263                         commands.add(command);
264                     } catch (Exception e) {
265                         final Logger logger = getLogger();
266                         if (logger != null) {
267                             logger.error("Failed to load custom command", e);
268                         }
269                     }
270                 }
271             }
272         }
273         this.commands = (Command[]) commands.toArray(this.commands);
274     }
275     
276     /**
277      * @see org.apache.james.socket.AbstractJamesService#getDefaultPort()
278      */
279      protected int getDefaultPort() {
280         return 4555;
281      }
282 
283     /**
284      * @see org.apache.james.socket.AbstractJamesService#getServiceType()
285      */
286     public String getServiceType() {
287         return "Remote Manager Service";
288     }
289     
290     /**
291      * @see org.apache.james.socket.AbstractJamesService#newProtocolHandlerInstance()
292      */
293     public ProtocolHandler newProtocolHandlerInstance() {
294         return new RemoteManagerHandler();
295     }
296 
297     /**
298      * A class to provide RemoteManager handler configuration to the handlers
299      */
300     private class RemoteManagerHandlerConfigurationDataImpl
301         implements RemoteManagerHandlerConfigurationData {
302 
303         /**
304          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getHelloName()
305          */
306         public String getHelloName() {
307             if (RemoteManager.this.helloName == null) {
308                 return RemoteManager.this.mailServer.getHelloName();
309             } else {
310                 return RemoteManager.this.helloName;
311             }
312         }
313 
314         /**
315          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getMailServer()
316          */
317         public MailServer getMailServer() {
318             return RemoteManager.this.mailServer;
319         }
320         
321         /**
322          * 
323          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getStore()
324          */
325         public Store getStore() {
326             return RemoteManager.this.store;
327         }
328         
329         /**
330          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersRepository()
331          */
332         public UsersRepository getUsersRepository() {
333             return RemoteManager.this.users;
334         }
335 
336         /**
337          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUserStore() 
338          */
339         public UsersStore getUserStore() {
340             return RemoteManager.this.usersStore;
341         }
342 
343         public SpoolManagementService getSpoolManagement() {
344             return RemoteManager.this.spoolManagement;
345         }
346 
347         /**
348          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getAdministrativeAccountData()
349          */
350         public HashMap getAdministrativeAccountData() {
351             return RemoteManager.this.adminAccounts;
352         }
353 
354         /**
355          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getPrompt()
356          */
357         public String getPrompt() {
358             return RemoteManager.this.prompt;
359         }
360         
361         /**
362          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getBayesianAnalyzerManagement()
363          */
364         public BayesianAnalyzerManagementService getBayesianAnalyzerManagement() {
365             return RemoteManager.this.bayesianAnalyzerManagement;
366         }
367 
368         /**
369          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getProcessorManagement()
370          */
371         public ProcessorManagementService getProcessorManagement() {
372             return RemoteManager.this.processorManagementService;
373         }
374 
375         /**
376          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getVirtualUserTableManagement()
377          */
378         public VirtualUserTableManagementService getVirtualUserTableManagement() {
379             return RemoteManager.this.vutManagemenet;
380         }
381 
382         /**
383          * @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getDomainListManagement()
384          */
385         public DomainListManagementService getDomainListManagement() {
386             return RemoteManager.this.domListManagement;
387         }
388         
389         /**
390          * @see org.apache.james.neo.remotemanager.RemoteManagerHandlerConfigurationData#getCommands()
391          */
392         public Command[] getCommands() {
393             return RemoteManager.this.commands;
394         }
395     }
396 
397     /**
398      * @see org.apache.james.socket.AbstractJamesService#getConfigurationData()
399      */
400     protected Object getConfigurationData() {
401         return theConfigData;
402     }
403 }