View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 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  package org.apache.james.transport;
18  import javax.mail.MessagingException;
19  
20  import org.apache.avalon.framework.configuration.Configuration;
21  import org.apache.avalon.framework.configuration.ConfigurationException;
22  import org.apache.james.core.MailetConfigImpl;
23  import org.apache.james.services.MailetLoader;
24  import org.apache.mailet.Mailet;
25  import org.apache.mailet.MailetException;
26  /***
27   * Loads Mailets for use inside James.
28   *
29   */
30  public class JamesMailetLoader extends Loader implements MailetLoader {
31      /***
32       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
33       */
34      public void configure(Configuration conf) throws ConfigurationException {
35          getPackages(conf,MAILET_PACKAGE);
36      }
37  
38      /***
39       * @see org.apache.james.services.MailetLoader#getMailet(java.lang.String, org.apache.avalon.framework.configuration.Configuration)
40       */
41      public Mailet getMailet(String mailetName, Configuration configuration)
42          throws MessagingException {
43          try {
44              for (int i = 0; i < packages.size(); i++) {
45                  String className = (String) packages.elementAt(i) + mailetName;
46                  try {
47                      MailetConfigImpl configImpl = new MailetConfigImpl();
48                      configImpl.setMailetName(mailetName);
49                      configImpl.setConfiguration(configuration);
50                      configImpl.setMailetContext(mailetContext);
51                      Mailet mailet = (Mailet) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
52                      mailet.init(configImpl);
53                      return mailet;
54                  } catch (ClassNotFoundException cnfe) {
55                      //do this so we loop through all the packages
56                  }
57              }
58              StringBuffer exceptionBuffer =
59                  new StringBuffer(128)
60                      .append("Requested mailet not found: ")
61                      .append(mailetName)
62                      .append(".  looked in ")
63                      .append(packages.toString());
64              throw new ClassNotFoundException(exceptionBuffer.toString());
65          } catch (MessagingException me) {
66              throw me;
67          } catch (Exception e) {
68              StringBuffer exceptionBuffer =
69                  new StringBuffer(128).append("Could not load mailet (").append(mailetName).append(
70                      ")");
71              throw new MailetException(exceptionBuffer.toString(), e);
72          }
73      }
74  }