1 17 18 package org.apache.james.transport; 19 import java.io.File ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.Vector ; 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 37 public class Loader implements Contextualizable, Component { 38 protected ClassLoader mailetClassLoader = null; 39 protected String baseDirectory = null; 40 protected Logger logger; 41 protected final String MAILET_PACKAGE = "mailetpackage"; 42 protected final String MATCHER_PACKAGE = "matcherpackage"; 43 46 protected Vector packages; 47 48 51 public void contextualize(final Context context) throws ContextException 52 { 53 try 54 { 55 baseDirectory = ((File )context.get( "app.home") ).getCanonicalPath(); 56 } 57 catch (Throwable 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 68 public void setLogger(Logger logger) { 69 this.logger = logger; 70 } 71 72 protected void getPackages(Configuration conf, String packageType) 73 throws ConfigurationException { 74 packages = new Vector (); 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 packageName = c.getValue(); 80 if (!packageName.endsWith(".")) { 81 packageName += "."; 82 } 83 packages.addElement(packageName); 84 } 85 } 86 89 protected void configureMailetClassLoader() { 90 File base = new File (baseDirectory + "/SAR-INF/lib"); 91 String [] flist = base.list(); 92 Vector jarlist = new Vector (); 93 URL [] classPath = null; 94 try { 95 jarlist.add(new URL ("file:///" + baseDirectory + "/SAR-INF/classes/")); 96 } catch (MalformedURLException 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 ("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 e) { 111 logger.error("can't add file:///" + baseDirectory +"/SAR-INF/lib/"+ flist[i] + " to mailet classloader"); 112 } 113 } 114 } 115 classPath = (URL []) jarlist.toArray(new URL [jarlist.size()]); 116 mailetClassLoader = new URLClassLoader (classPath, this.getClass().getClassLoader()); 117 } 118 } 119 | Popular Tags |