1 16 package org.apache.cocoon.components.classloader; 17 18 import org.apache.avalon.framework.logger.LogEnabled; 19 import org.apache.avalon.framework.logger.Logger; 20 21 import org.apache.cocoon.CascadingIOException; 22 import org.apache.cocoon.util.ClassUtils; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.Vector ; 32 33 43 public class RepositoryClassLoader extends URLClassLoader 44 implements LogEnabled { 45 46 49 protected Logger log; 50 51 54 public RepositoryClassLoader() { 55 super(new URL []{}, ClassUtils.getClassLoader()); 56 } 57 58 61 public RepositoryClassLoader(URL [] urls) { 62 super(urls, ClassUtils.getClassLoader()); 63 } 64 65 68 public RepositoryClassLoader(URL [] urls, ClassLoader parentClassLoader) { 69 super(urls, parentClassLoader); 70 } 71 72 77 protected RepositoryClassLoader(Vector repositories) { 78 this((Collection ) repositories); 79 } 80 81 86 protected RepositoryClassLoader(Collection repositories) { 87 this(); 88 Iterator i = repositories.iterator(); 89 while (i.hasNext()) { 90 try { 91 this.addDirectory((File ) i.next()); 92 } catch (IOException ioe) { 93 log.error("Repository could not be added", ioe); 94 } 95 } 96 } 97 98 103 public void enableLogging(Logger logger) { 104 if (this.log == null) { 105 this.log = logger; 106 } 107 } 108 109 116 public void addDirectory(File repository) throws IOException { 117 try { 118 this.addURL(repository.getCanonicalFile().toURL()); 119 } catch (MalformedURLException mue) { 120 log.error("The repository had a bad URL", mue); 121 throw new CascadingIOException("Could not add repository", mue); 122 } 123 } 124 125 132 public void addDirectory(String repository) throws IOException { 133 try { 134 File file = new File (repository); 135 this.addURL(file.getCanonicalFile().toURL()); 136 } catch (MalformedURLException mue) { 137 log.error("The repository had a bad URL", mue); 138 throw new CascadingIOException("Could not add repository", mue); 139 } 140 } 141 142 145 public void addURL(URL url) { 146 super.addURL(url); 147 } 148 149 152 public Class defineClass(byte[] b) throws ClassFormatError { 153 return super.defineClass(null, b, 0, b.length); 154 } 155 } 156 | Popular Tags |