KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.context.Context;
28 import org.apache.avalon.framework.context.ContextException;
29 import org.apache.avalon.framework.context.Contextualizable;
30 import org.apache.avalon.framework.logger.Logger;
31 import org.apache.avalon.framework.component.Component;
32
33 /**
34  *
35  * $Id: Loader.java,v 1.8.2.3 2004/03/15 03:54:18 noel Exp $
36  */

37 public class Loader implements Contextualizable, Component {
38     protected ClassLoader JavaDoc mailetClassLoader = null;
39     protected String JavaDoc baseDirectory = null;
40     protected Logger logger;
41     protected final String JavaDoc MAILET_PACKAGE = "mailetpackage";
42     protected final String JavaDoc MATCHER_PACKAGE = "matcherpackage";
43       /**
44      * The list of packages that may contain Mailets or matchers
45      */

46     protected Vector JavaDoc packages;
47
48     /**
49      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
50      */

51     public void contextualize(final Context context) throws ContextException
52     {
53         try
54         {
55             baseDirectory = ((File JavaDoc)context.get( "app.home") ).getCanonicalPath();
56         }
57         catch (Throwable JavaDoc e)
58         {
59             logger.error( "can't get base directory for mailet loader" );
60             throw new ContextException("can't contextualise mailet loader " + e.getMessage(), e);
61         }
62     }
63
64     /**
65      * Method setLogger.
66      * @param logger
67      */

68     public void setLogger(Logger logger) {
69         this.logger = logger;
70     }
71
72     protected void getPackages(Configuration conf, String JavaDoc packageType)
73         throws ConfigurationException {
74         packages = new Vector JavaDoc();
75         packages.addElement("");
76         final Configuration[] pkgConfs = conf.getChildren(packageType);
77         for (int i = 0; i < pkgConfs.length; i++) {
78             Configuration c = pkgConfs[i];
79             String JavaDoc packageName = c.getValue();
80             if (!packageName.endsWith(".")) {
81                 packageName += ".";
82             }
83             packages.addElement(packageName);
84         }
85     }
86     /**
87      * Method getMailetClassLoader.
88      */

89     protected void configureMailetClassLoader() {
90         File JavaDoc base = new File JavaDoc(baseDirectory + "/SAR-INF/lib");
91         String JavaDoc[] flist = base.list();
92         Vector JavaDoc jarlist = new Vector JavaDoc();
93         URL JavaDoc[] classPath = null;
94         try {
95             jarlist.add(new URL JavaDoc("file:///" + baseDirectory + "/SAR-INF/classes/"));
96         } catch (MalformedURLException JavaDoc e) {
97             logger.error(
98                 "can't add "
99                     + "file:///"
100                     + baseDirectory
101                     + "/SAR-INF/classes/ to mailet classloader");
102         }
103         if (flist != null) {
104             for (int i = 0; i < flist.length; i++) {
105                 try {
106                     if (flist[i].indexOf("jar") == flist[i].length() - 3) {
107                         jarlist.add(new URL JavaDoc("file:///" + baseDirectory +"/SAR-INF/lib/"+ flist[i]));
108                         logger.debug("added file:///" + baseDirectory +"/SAR-INF/lib/" + flist[i] + " to mailet Classloader");
109                     }
110                 } catch (MalformedURLException JavaDoc e) {
111                     logger.error("can't add file:///" + baseDirectory +"/SAR-INF/lib/"+ flist[i] + " to mailet classloader");
112                 }
113             }
114         }
115         classPath = (URL JavaDoc[]) jarlist.toArray(new URL JavaDoc[jarlist.size()]);
116         mailetClassLoader = new URLClassLoader JavaDoc(classPath, this.getClass().getClassLoader());
117     }
118 }
119
Popular Tags