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