KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > MailetLoader


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;
19 import javax.mail.MessagingException JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configurable;
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.mailet.Mailet;
26 import org.apache.mailet.MailetContext;
27 import org.apache.mailet.MailetException;
28 /**
29  * Loads Mailets for use inside James.
30  *
31  */

32 public class MailetLoader extends Loader implements Configurable {
33            /**
34      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
35      */

36     public void configure(Configuration conf) throws ConfigurationException {
37            getPackages(conf,MAILET_PACKAGE);
38            configureMailetClassLoader();
39     }
40     /**
41      * Get a new Mailet with the specified name acting
42      * in the specified context.
43      *
44      * @param matchName the name of the mailet to be loaded
45      * @param context the MailetContext to be passed to the new
46      * mailet
47      * @throws MessagingException if an error occurs
48      */

49     public Mailet getMailet(String JavaDoc mailetName, MailetContext context, Configuration configuration)
50         throws MessagingException JavaDoc {
51         try {
52             for (int i = 0; i < packages.size(); i++) {
53                 String JavaDoc className = (String JavaDoc) packages.elementAt(i) + mailetName;
54                 try {
55                     MailetConfigImpl configImpl = new MailetConfigImpl();
56                     configImpl.setMailetName(mailetName);
57                     configImpl.setConfiguration(configuration);
58                     configImpl.setMailetContext(context);
59                     Mailet mailet = (Mailet) mailetClassLoader.loadClass(className).newInstance();
60                     mailet.init(configImpl);
61                     return mailet;
62                 } catch (ClassNotFoundException JavaDoc cnfe) {
63                     //do this so we loop through all the packages
64
}
65             }
66             StringBuffer JavaDoc exceptionBuffer =
67                 new StringBuffer JavaDoc(128)
68                     .append("Requested mailet not found: ")
69                     .append(mailetName)
70                     .append(". looked in ")
71                     .append(packages.toString());
72             throw new ClassNotFoundException JavaDoc(exceptionBuffer.toString());
73         } catch (MessagingException JavaDoc me) {
74             throw me;
75         } catch (Exception JavaDoc e) {
76             StringBuffer JavaDoc exceptionBuffer =
77                 new StringBuffer JavaDoc(128).append("Could not load mailet (").append(mailetName).append(
78                     ")");
79             throw new MailetException(exceptionBuffer.toString(), e);
80         }
81     }
82 }
83
Popular Tags