01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/package org.apache.james.transport;
19:
20: import javax.mail.MessagingException;
21:
22: import org.apache.avalon.framework.configuration.Configuration;
23: import org.apache.avalon.framework.configuration.ConfigurationException;
24: import org.apache.james.core.MailetConfigImpl;
25: import org.apache.james.services.MailetLoader;
26: import org.apache.mailet.Mailet;
27: import org.apache.mailet.MailetException;
28:
29: /**
30: * Loads Mailets for use inside James.
31: *
32: */
33: public class JamesMailetLoader extends Loader implements MailetLoader {
34: /**
35: * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
36: */
37: public void configure(Configuration conf)
38: throws ConfigurationException {
39: getPackages(conf, MAILET_PACKAGE);
40: }
41:
42: /**
43: * @see org.apache.james.services.MailetLoader#getMailet(java.lang.String, org.apache.avalon.framework.configuration.Configuration)
44: */
45: public Mailet getMailet(String mailetName,
46: Configuration configuration) throws MessagingException {
47: try {
48: for (int i = 0; i < packages.size(); i++) {
49: String className = (String) packages.elementAt(i)
50: + mailetName;
51: try {
52: MailetConfigImpl configImpl = new MailetConfigImpl();
53: configImpl.setMailetName(mailetName);
54: configImpl.setConfiguration(configuration);
55: configImpl.setMailetContext(mailetContext);
56: Mailet mailet = (Mailet) Thread.currentThread()
57: .getContextClassLoader().loadClass(
58: className).newInstance();
59: mailet.init(configImpl);
60: return mailet;
61: } catch (ClassNotFoundException cnfe) {
62: //do this so we loop through all the packages
63: }
64: }
65: StringBuffer exceptionBuffer = new StringBuffer(128)
66: .append("Requested mailet not found: ").append(
67: mailetName).append(". looked in ").append(
68: packages.toString());
69: throw new ClassNotFoundException(exceptionBuffer.toString());
70: } catch (MessagingException me) {
71: throw me;
72: } catch (Exception e) {
73: StringBuffer exceptionBuffer = new StringBuffer(128)
74: .append("Could not load mailet (").append(
75: mailetName).append(")");
76: throw new MailetException(exceptionBuffer.toString(), e);
77: }
78: }
79: }
|