01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/package org.apache.james.transport;
19:
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.MatcherConfigImpl;
25: import org.apache.james.services.MatcherLoader;
26: import org.apache.mailet.MailetException;
27: import org.apache.mailet.Matcher;
28:
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)
38: throws ConfigurationException {
39: getPackages(conf, MATCHER_PACKAGE);
40: }
41:
42: /* (non-Javadoc)
43: * @see org.apache.james.transport.MatcherLoader#getMatcher(java.lang.String)
44: */
45: public Matcher getMatcher(String matchName)
46: throws MessagingException {
47: try {
48: String condition = (String) null;
49: int i = matchName.indexOf('=');
50: if (i != -1) {
51: condition = matchName.substring(i + 1);
52: matchName = matchName.substring(0, i);
53: }
54: for (i = 0; i < packages.size(); i++) {
55: String className = (String) packages.elementAt(i)
56: + matchName;
57: try {
58: MatcherConfigImpl configImpl = new MatcherConfigImpl();
59: configImpl.setMatcherName(matchName);
60: configImpl.setCondition(condition);
61: configImpl.setMailetContext(mailetContext);
62: Matcher matcher = (Matcher) Thread.currentThread()
63: .getContextClassLoader().loadClass(
64: className).newInstance();
65: matcher.init(configImpl);
66: return matcher;
67: } catch (ClassNotFoundException cnfe) {
68: //do this so we loop through all the packages
69: }
70: }
71: StringBuffer exceptionBuffer = new StringBuffer(128)
72: .append("Requested matcher not found: ").append(
73: matchName).append(". looked in ").append(
74: packages.toString());
75: throw new ClassNotFoundException(exceptionBuffer.toString());
76: } catch (MessagingException me) {
77: throw me;
78: } catch (Exception e) {
79: StringBuffer exceptionBuffer = new StringBuffer(128)
80: .append("Could not load matcher (").append(
81: matchName).append(")");
82: throw new MailetException(exceptionBuffer.toString(), e);
83: }
84: }
85: }
|