View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
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   ****************************************************************/
19  package org.apache.james.transport;
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   * Loads Mailets for use inside James.
30   *
31   */
32  public class JamesMailetLoader extends Loader implements MailetLoader {
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      }
39  
40      /***
41       * @see org.apache.james.services.MailetLoader#getMailet(java.lang.String, org.apache.avalon.framework.configuration.Configuration)
42       */
43      public Mailet getMailet(String mailetName, Configuration configuration)
44          throws MessagingException {
45          try {
46              for (int i = 0; i < packages.size(); i++) {
47                  String className = (String) packages.elementAt(i) + mailetName;
48                  try {
49                      MailetConfigImpl configImpl = new MailetConfigImpl();
50                      configImpl.setMailetName(mailetName);
51                      configImpl.setConfiguration(configuration);
52                      configImpl.setMailetContext(mailetContext);
53                      Mailet mailet = (Mailet) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
54                      mailet.init(configImpl);
55                      return mailet;
56                  } catch (ClassNotFoundException cnfe) {
57                      //do this so we loop through all the packages
58                  }
59              }
60              StringBuffer exceptionBuffer =
61                  new StringBuffer(128)
62                      .append("Requested mailet not found: ")
63                      .append(mailetName)
64                      .append(".  looked in ")
65                      .append(packages.toString());
66              throw new ClassNotFoundException(exceptionBuffer.toString());
67          } catch (MessagingException me) {
68              throw me;
69          } catch (Exception e) {
70              StringBuffer exceptionBuffer =
71                  new StringBuffer(128).append("Could not load mailet (").append(mailetName).append(
72                      ")");
73              throw new MailetException(exceptionBuffer.toString(), e);
74          }
75      }
76  }