1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.james.transport;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.Vector;
27
28 import org.apache.avalon.framework.activity.Initializable;
29 import org.apache.avalon.framework.configuration.Configurable;
30 import org.apache.avalon.framework.configuration.Configuration;
31 import org.apache.avalon.framework.configuration.ConfigurationException;
32 import org.apache.avalon.framework.logger.AbstractLogEnabled;
33 import org.apache.avalon.framework.logger.Logger;
34 import org.apache.avalon.framework.service.DefaultServiceManager;
35 import org.apache.avalon.framework.service.ServiceException;
36 import org.apache.avalon.framework.service.ServiceManager;
37 import org.apache.avalon.framework.service.Serviceable;
38 import org.apache.james.services.FileSystem;
39 import org.apache.mailet.Mail;
40 import org.apache.mailet.MailAddress;
41 import org.apache.mailet.MailetContext;
42
43 import javax.mail.MessagingException;
44 import javax.mail.internet.MimeMessage;
45
46
47
48
49
50 public abstract class Loader extends AbstractLogEnabled implements Serviceable, Configurable, Initializable {
51
52 protected String baseDirectory = null;
53 protected final String MAILET_PACKAGE = "mailetpackage";
54 protected final String MATCHER_PACKAGE = "matcherpackage";
55
56
57
58 protected Vector packages;
59
60
61
62
63 private ServiceManager serviceManager;
64
65
66
67
68 protected MailetContext mailetContext;
69
70
71
72
73
74
75 public void setMailetContext(MailetContext mailetContext) {
76 this.mailetContext = mailetContext;
77 }
78
79 protected void getPackages(Configuration conf, String packageType)
80 throws ConfigurationException {
81 packages = new Vector();
82 packages.addElement("");
83 final Configuration[] pkgConfs = conf.getChildren(packageType);
84 for (int i = 0; i < pkgConfs.length; i++) {
85 Configuration c = pkgConfs[i];
86 String packageName = c.getValue();
87 if (!packageName.endsWith(".")) {
88 packageName += ".";
89 }
90 packages.addElement(packageName);
91 }
92 }
93
94
95
96
97 public void service(ServiceManager sm) throws ServiceException {
98 serviceManager = new DefaultServiceManager(sm);
99 try {
100 baseDirectory = ((FileSystem) serviceManager.lookup(FileSystem.ROLE)).getBasedir().getCanonicalPath();
101 } catch (FileNotFoundException e) {
102 throw new ServiceException(FileSystem.ROLE, "Cannot find the base directory of the application", e);
103 } catch (IOException e) {
104 throw new ServiceException(FileSystem.ROLE, "Cannot find the base directory of the application", e);
105 }
106 }
107
108
109
110
111 public void initialize() throws Exception {
112 setMailetContext((MailetContext) serviceManager.lookup(MailetContext.class.getName()));
113 }
114
115
116
117
118 public abstract void configure(Configuration arg0) throws ConfigurationException;
119
120
121
122
123 protected final static class MailetContextWrapper implements MailetContext {
124
125
126 private MailetContext mailetContext;
127
128 private Logger logger;
129
130
131
132
133
134
135
136
137 public MailetContextWrapper(MailetContext mailetContext, Logger logger) {
138 this.mailetContext = mailetContext;
139 this.logger = logger;
140 }
141
142
143
144
145 public void bounce(Mail mail, String message) throws MessagingException {
146 mailetContext.bounce(mail, message);
147 }
148
149
150
151
152 public void bounce(Mail mail, String message, MailAddress bouncer) throws MessagingException {
153 mailetContext.bounce(mail, message, bouncer);
154 }
155
156
157
158
159 public Object getAttribute(String name) {
160 return mailetContext.getAttribute(name);
161 }
162
163
164
165
166 public Iterator getAttributeNames() {
167 return mailetContext.getAttributeNames();
168 }
169
170
171
172
173 public Collection getMailServers(String host) {
174 return mailetContext.getMailServers(host);
175 }
176
177
178
179
180 public int getMajorVersion() {
181 return mailetContext.getMajorVersion();
182 }
183
184
185
186
187 public int getMinorVersion() {
188 return mailetContext.getMinorVersion();
189 }
190
191
192
193
194 public MailAddress getPostmaster() {
195 return mailetContext.getPostmaster();
196 }
197
198
199
200
201 public Iterator getSMTPHostAddresses(String domainName) {
202 return mailetContext.getSMTPHostAddresses(domainName);
203 }
204
205
206
207
208 public String getServerInfo() {
209 return mailetContext.getServerInfo();
210 }
211
212
213
214
215 public boolean isLocalEmail(MailAddress mailAddress) {
216 return mailetContext.isLocalEmail(mailAddress);
217 }
218
219
220
221
222 public boolean isLocalServer(String serverName) {
223 return mailetContext.isLocalServer(serverName);
224 }
225
226
227
228
229 public boolean isLocalUser(String userAccount) {
230 return mailetContext.isLocalUser(userAccount);
231 }
232
233
234
235
236 public void log(String message) {
237 logger.info(message);
238 }
239
240
241
242
243 public void log(String message, Throwable t) {
244 logger.info(message, t);
245 }
246
247
248
249
250 public void removeAttribute(String name) {
251 mailetContext.removeAttribute(name);
252 }
253
254
255
256
257 public void sendMail(MimeMessage msg) throws MessagingException {
258 mailetContext.sendMail(msg);
259 }
260
261
262
263
264 public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg) throws MessagingException {
265 mailetContext.sendMail(sender, recipients, msg);
266 }
267
268
269
270
271 public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg, String state) throws MessagingException {
272 mailetContext.sendMail(sender, recipients, msg, state);
273 }
274
275
276
277
278 public void sendMail(Mail mail) throws MessagingException {
279 mailetContext.sendMail(mail);
280 }
281
282
283
284
285 public void setAttribute(String name, Object object) {
286 mailetContext.setAttribute(name, object);
287 }
288
289
290
291
292 public void storeMail(MailAddress sender, MailAddress recipient, MimeMessage msg) throws MessagingException {
293 mailetContext.storeMail(sender, recipient, msg);
294 }
295 }
296
297 }