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.transport.mailets;
23
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.james.Constants;
28 import org.apache.james.api.user.UsersRepository;
29 import org.apache.james.api.user.UsersStore;
30 import org.apache.james.transport.mailets.listservcommands.ErrorCommand;
31 import org.apache.james.transport.mailets.listservcommands.IListServCommand;
32 import org.apache.james.util.XMLResources;
33 import org.apache.mailet.base.GenericMailet;
34 import org.apache.mailet.Mail;
35 import org.apache.mailet.MailAddress;
36
37 import javax.mail.MessagingException;
38 import java.io.File;
39 import java.lang.reflect.Field;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Locale;
45 import java.util.Map;
46 import java.util.Properties;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public class CommandListservManager extends GenericMailet implements ICommandListservManager {
102
103 protected Map commandMap = new HashMap();
104 protected List commandPackages = new ArrayList();
105 protected UsersRepository usersRepository;
106 protected String listName;
107 protected String displayName;
108 protected String listOwner;
109 protected String listDomain;
110 protected XMLResources xmlResources;
111
112
113
114
115
116
117
118
119
120 public String getListName(boolean displayFormat) {
121 return displayFormat ? displayName : listName;
122 }
123
124
125
126
127
128
129
130
131 public String getListOwner() {
132 return listOwner;
133 }
134
135
136
137
138
139
140
141
142 public String getListDomain() {
143 return listDomain;
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public IListServCommand getCommand(String name) {
164 return (IListServCommand) commandMap.get(name.toLowerCase(Locale.US));
165 }
166
167
168
169
170
171
172 public Map getCommands() {
173 return commandMap;
174 }
175
176
177
178
179
180 public UsersRepository getUsersRepository() {
181 return usersRepository;
182 }
183
184
185
186
187
188
189
190 public void onError(Mail mail, String subject, String errorMessage) throws MessagingException {
191 ErrorCommand errorCommand = (ErrorCommand) getCommand("error");
192 errorCommand.onError(mail, subject, errorMessage);
193 }
194
195
196
197
198 public String getResourcesFile() {
199 return getInitParameter("resources");
200 }
201
202
203
204
205
206 public Properties getStandardProperties() {
207 Properties standardProperties = new Properties();
208 standardProperties.put("LIST_NAME", getListName(false));
209 standardProperties.put("DISPLAY_NAME", getListName(true));
210 standardProperties.put("DOMAIN_NAME", getListDomain());
211 return standardProperties;
212 }
213
214
215
216
217
218
219
220 public XMLResources[] initXMLResources(String[] names) throws ConfigurationException {
221 try {
222 File xmlFile = new File(getResourcesFile());
223
224 Properties props = getStandardProperties();
225 String listName = props.getProperty("LIST_NAME");
226
227 XMLResources[] xmlResources = new XMLResources[names.length];
228 for (int index = 0; index < names.length; index++) {
229 xmlResources[index] = new XMLResources();
230 xmlResources[index].init(xmlFile, names[index], listName, props);
231 }
232 return xmlResources;
233 } catch (Exception e) {
234 log(e.getMessage(), e);
235 throw new ConfigurationException("Can't initialize:", e);
236 }
237 }
238
239 public void init() throws MessagingException {
240
241 try {
242
243
244 Configuration configuration = (Configuration) getField(getMailetConfig(), "configuration");
245
246
247 listName = configuration.getChild("listName").getValue();
248 displayName = configuration.getChild("displayName").getValue();
249 listOwner = configuration.getChild("listOwner").getValue();
250 listDomain = configuration.getChild("listDomain").getValue();
251
252
253 initializeResources();
254
255
256 initUsersRepository();
257
258
259 loadCommandPackages(configuration);
260
261
262 loadCommands(configuration);
263
264
265 getMailetContext().setAttribute(ICommandListservManager.ID + listName, this);
266 } catch (Exception e) {
267 throw new MessagingException(e.getMessage(), e);
268 }
269 }
270
271
272
273
274
275
276 public IListServCommand getCommandTarget(MailAddress mailAddress) {
277 String commandName = getCommandName(mailAddress);
278 return getCommand(commandName);
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292 public void service(Mail mail) throws MessagingException {
293 if (mail.getRecipients().size() != 1) {
294 getMailetContext().bounce(mail, "You can only send one command at a time to this listserv manager.");
295 return;
296 }
297 MailAddress mailAddress = (MailAddress) mail.getRecipients().iterator().next();
298 IListServCommand command = getCommandTarget(mailAddress);
299
300 if (command == null) {
301
302 Properties props = getStandardProperties();
303 props.setProperty("COMMAND", getCommandName(mailAddress));
304 onError(mail, "unknown command", xmlResources.getString("command.not.understood", props));
305 } else {
306 command.onCommand(mail);
307 }
308
309
310
311
312
313 mail.setState(Mail.GHOST);
314 }
315
316
317
318
319
320
321 protected String getCommandName(MailAddress mailAddress) {
322 String user = mailAddress.getUser();
323 int index = user.indexOf('-', listName.length());
324 String commandName = user.substring(++index);
325 return commandName;
326 }
327
328
329
330
331
332 protected void initializeResources() throws Exception {
333 xmlResources = initXMLResources(new String[]{"List Manager"})[0];
334 }
335
336
337
338
339 protected void initUsersRepository() {
340 ServiceManager compMgr = (ServiceManager) getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
341 try {
342 UsersStore usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE);
343 String repName = getInitParameter("repositoryName");
344
345 usersRepository = usersStore.getRepository(repName);
346 } catch (Exception e) {
347 log("Failed to retrieve Store component:" + e.getMessage());
348 }
349 }
350
351
352
353
354
355
356 protected void loadCommands(Configuration configuration) throws Exception {
357 final Configuration commandConfigurations = configuration.getChild("commands");
358 final Configuration[] commandConfs = commandConfigurations.getChildren("command");
359 for (int index = 0; index < commandConfs.length; index++) {
360 Configuration commandConf = commandConfs[index];
361 String commandName = commandConf.getAttribute("name").toLowerCase();
362 String className = commandConf.getAttribute("class");
363 loadCommand(commandName, className, commandConf);
364 }
365 }
366
367
368
369
370
371
372
373
374
375 protected void loadCommand(String commandName,
376 String className,
377 Configuration configuration)
378 throws ConfigurationException, ClassNotFoundException, IllegalAccessException, InstantiationException {
379 ClassLoader theClassLoader = Thread.currentThread().getContextClassLoader();
380 for (Iterator it = commandPackages.iterator(); it.hasNext();) {
381 String packageName = (String) it.next();
382
383 IListServCommand listServCommand = null;
384 try {
385 listServCommand = (IListServCommand) theClassLoader.loadClass(packageName + className).newInstance();
386 } catch (Exception e) {
387
388 continue;
389 }
390 listServCommand.init(this, configuration);
391 commandMap.put(commandName, listServCommand);
392 return;
393 }
394
395 throw new ConfigurationException("Unable to load listservcommand: " + commandName);
396 }
397
398
399
400
401
402
403
404 protected void loadCommandPackages(Configuration configuration) throws ConfigurationException {
405 commandPackages.add("");
406 final Configuration packageConfiguration = configuration.getChild("commandpackages");
407 final Configuration[] pkgConfs = packageConfiguration.getChildren("commandpackage");
408 for (int index = 0; index < pkgConfs.length; index++) {
409 Configuration conf = pkgConfs[index];
410 String packageName = conf.getValue().trim();
411 if (!packageName.endsWith(".")) {
412 packageName += ".";
413 }
414 commandPackages.add(packageName);
415 }
416 }
417
418
419
420
421
422 protected static Object getField(Object instance, String name) throws IllegalAccessException {
423 Class clazz = instance.getClass();
424 Field[] fields;
425 while (clazz != null) {
426 fields = clazz.getDeclaredFields();
427 for (int index = 0; index < fields.length; index++) {
428 Field field = fields[index];
429 if (field.getName().equals(name)) {
430 field.setAccessible(true);
431 return field.get(instance);
432 }
433 }
434 clazz = clazz.getSuperclass();
435 }
436
437 return null;
438 }
439 }