KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > listservcommands > Info


1 /***********************************************************************
2  * Copyright (c) 2000-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.transport.mailets.listservcommands;
19
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.james.services.UsersRepository;
23 import org.apache.james.transport.mailets.ICommandListservManager;
24 import org.apache.james.util.XMLResources;
25 import org.apache.mailet.Mail;
26
27 import javax.mail.MessagingException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * Info handles the info command.
33  * It is configured by:
34  * <pre>&lt;command name="info" class="Info"/&gt;</pre>
35  *
36  * <br />
37  * <br />
38  *
39  * It uses the formatted text-based resources for its return mail body:
40  * <ul>
41  * <li>header
42  * <li>info
43  * <li>admincommands
44  * </ul>
45  *
46  * <br />
47  * <br />
48  * After formatting the text, the message is delivered with {@link #sendStandardReply}
49  *
50  * Todo: make displaying the current member list optional
51  *
52  * @version CVS $Revision: 1.1.2.3 $ $Date: 2004/03/15 03:54:20 $
53  * @since 2.2.0
54  */

55 public class Info extends BaseCommand {
56
57     //For resources
58
protected XMLResources[] xmlResources;
59
60     protected static final int HEADER = 0;
61     protected static final int INFO = 1;
62     protected static final int ADMIN_COMMANDS = 2;
63
64     public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
65         super.init(commandListservManager, configuration);
66         xmlResources = initXMLResources(new String JavaDoc[]{"header", "info", "admincommands"});
67     }
68
69     /**
70      * Process the info command using the following text resources:
71      * <ul>
72      * <li>{@link #HEADER}
73      * <li>{@link #INFO}
74      * <li>{@link #ADMIN_COMMANDS}
75      * </ul>
76      *
77      * @param mail
78      */

79     public void onCommand(Mail mail) throws MessagingException JavaDoc {
80         //send info mail
81
Properties JavaDoc props = getStandardProperties();
82         props.put("MEMBER_LIST", getMemberList());
83
84         StringBuffer JavaDoc plainTextMessage = new StringBuffer JavaDoc();
85         String JavaDoc header = xmlResources[HEADER].getString("text", props);
86         plainTextMessage.append(header);
87
88         String JavaDoc infoMail = xmlResources[INFO].getString("text", props);
89         plainTextMessage.append(infoMail);
90
91         String JavaDoc adminCommands = xmlResources[ADMIN_COMMANDS].getString("text", props);
92         plainTextMessage.append(adminCommands);
93
94         String JavaDoc subject = xmlResources[INFO].getString("info.subject", props);
95
96         sendStandardReply(mail, subject, plainTextMessage.toString(), null);
97     }
98
99     /**
100      * Retrieve the current member list
101      * @return the formatted member list
102      *
103      * @see #getUsersRepository
104      */

105     protected String JavaDoc getMemberList() {
106
107         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(0x1000);
108         buffer.append("\r\n");
109         UsersRepository usersRepository = getUsersRepository();
110         for (Iterator JavaDoc it = usersRepository.list(); it.hasNext();) {
111             String JavaDoc userName = (String JavaDoc) it.next();
112             buffer.append(" ").append(userName);
113             buffer.append("\r\n");
114         }
115
116         return buffer.toString();
117     }
118 }
119
Popular Tags