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