KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > MatchLoader


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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 JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configurable;
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.mailet.MailetContext;
26 import org.apache.mailet.MailetException;
27 import org.apache.mailet.Matcher;
28 /**
29  * Loads Matchers for use inside James.
30  *
31  */

32 public class MatchLoader extends Loader implements Configurable {
33         /**
34      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
35      */

36     public void configure(Configuration conf) throws ConfigurationException {
37            getPackages(conf,MATCHER_PACKAGE);
38            configureMailetClassLoader();
39     }
40
41
42     /**
43      * Get a new Matcher with the specified name acting
44      * in the specified context.
45      *
46      * @param matchName the name of the matcher to be loaded
47      * @param context the MailetContext to be passed to the new
48      * matcher
49      * @throws MessagingException if an error occurs
50      */

51     public Matcher getMatcher(String JavaDoc matchName, MailetContext context) throws MessagingException JavaDoc {
52         try {
53             String JavaDoc condition = (String JavaDoc) null;
54             int i = matchName.indexOf('=');
55             if (i != -1) {
56                 condition = matchName.substring(i + 1);
57                 matchName = matchName.substring(0, i);
58             }
59             for (i = 0; i < packages.size(); i++) {
60                 String JavaDoc className = (String JavaDoc) packages.elementAt(i) + matchName;
61                 try {
62                     MatcherConfigImpl configImpl = new MatcherConfigImpl();
63                     configImpl.setMatcherName(matchName);
64                     configImpl.setCondition(condition);
65                     configImpl.setMailetContext(context);
66                     Matcher matcher = (Matcher) mailetClassLoader.loadClass(className).newInstance();
67                     matcher.init(configImpl);
68                     return matcher;
69                 } catch (ClassNotFoundException JavaDoc cnfe) {
70                     //do this so we loop through all the packages
71
}
72             }
73             StringBuffer JavaDoc exceptionBuffer =
74                 new StringBuffer JavaDoc(128)
75                     .append("Requested matcher not found: ")
76                     .append(matchName)
77                     .append(". looked in ")
78                     .append(packages.toString());
79             throw new ClassNotFoundException JavaDoc(exceptionBuffer.toString());
80         } catch (MessagingException JavaDoc me) {
81             throw me;
82         } catch (Exception JavaDoc e) {
83             StringBuffer JavaDoc exceptionBuffer =
84                 new StringBuffer JavaDoc(128).append("Could not load matcher (").append(matchName).append(
85                     ")");
86             throw new MailetException(exceptionBuffer.toString(), e);
87         }
88     }
89 }
90
Popular Tags