1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
48
49
50
51
52
53
54 public class RemoteManager
55 extends AbstractJamesService implements RemoteManagerMBean {
56
57
58
59
60 private HashMap adminAccounts = new HashMap();
61
62
63
64
65 private UsersStore usersStore;
66
67
68
69
70 private UsersRepository users;
71
72
73
74
75 private SpoolManagementService spoolManagement;
76
77
78
79
80 private String prompt = "";
81
82
83
84
85 private MailServer mailServer;
86
87
88
89
90 private Store store;
91
92 private Command[] commands = {};
93
94
95
96
97 private BayesianAnalyzerManagementService bayesianAnalyzerManagement;
98
99
100
101
102 private ProcessorManagementService processorManagementService;
103
104 private VirtualUserTableManagementService vutManagemenet;
105
106 private DomainListManagementService domListManagement;
107
108
109
110
111
112
113 public void setUsersStore(UsersStore usersStore) {
114 this.usersStore = usersStore;
115 }
116
117
118
119
120
121
122 public void setUsers(UsersRepository users) {
123 this.users = users;
124 }
125
126
127
128
129
130
131 public void setSpoolManagement(SpoolManagementService spoolManagement) {
132 this.spoolManagement = spoolManagement;
133 }
134
135
136
137
138
139
140 public void setMailServer(MailServer mailServer) {
141 this.mailServer = mailServer;
142 }
143
144
145
146
147
148
149 public void setStore(Store store) {
150 this.store = store;
151 }
152
153
154
155
156
157
158 public void setBayesianAnalyzerManagement(BayesianAnalyzerManagementService bayesianAnalyzerManagement) {
159 this.bayesianAnalyzerManagement = bayesianAnalyzerManagement;
160 }
161
162
163
164
165
166
167 public void setProcessorManagement(ProcessorManagementService processorManagement) {
168 this.processorManagementService = processorManagement;
169 }
170
171
172
173
174
175
176 public void setVirtualUserTableManagement(VirtualUserTableManagementService vutManagement) {
177 this.vutManagemenet = vutManagement;
178 }
179
180
181
182
183
184
185 public void setDomainListManagement(DomainListManagementService domListManagement) {
186 this.domListManagement = domListManagement;
187 }
188
189
190
191
192 private RemoteManagerHandlerConfigurationData theConfigData
193 = new RemoteManagerHandlerConfigurationDataImpl();
194
195
196
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
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
278
279 protected int getDefaultPort() {
280 return 4555;
281 }
282
283
284
285
286 public String getServiceType() {
287 return "Remote Manager Service";
288 }
289
290
291
292
293 public ProtocolHandler newProtocolHandlerInstance() {
294 return new RemoteManagerHandler();
295 }
296
297
298
299
300 private class RemoteManagerHandlerConfigurationDataImpl
301 implements RemoteManagerHandlerConfigurationData {
302
303
304
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
316
317 public MailServer getMailServer() {
318 return RemoteManager.this.mailServer;
319 }
320
321
322
323
324
325 public Store getStore() {
326 return RemoteManager.this.store;
327 }
328
329
330
331
332 public UsersRepository getUsersRepository() {
333 return RemoteManager.this.users;
334 }
335
336
337
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
349
350 public HashMap getAdministrativeAccountData() {
351 return RemoteManager.this.adminAccounts;
352 }
353
354
355
356
357 public String getPrompt() {
358 return RemoteManager.this.prompt;
359 }
360
361
362
363
364 public BayesianAnalyzerManagementService getBayesianAnalyzerManagement() {
365 return RemoteManager.this.bayesianAnalyzerManagement;
366 }
367
368
369
370
371 public ProcessorManagementService getProcessorManagement() {
372 return RemoteManager.this.processorManagementService;
373 }
374
375
376
377
378 public VirtualUserTableManagementService getVirtualUserTableManagement() {
379 return RemoteManager.this.vutManagemenet;
380 }
381
382
383
384
385 public DomainListManagementService getDomainListManagement() {
386 return RemoteManager.this.domListManagement;
387 }
388
389
390
391
392 public Command[] getCommands() {
393 return RemoteManager.this.commands;
394 }
395 }
396
397
398
399
400 protected Object getConfigurationData() {
401 return theConfigData;
402 }
403 }