bloritsch 00/12/15 09:57:55
Modified: src/org/apache/cocoon/components/classloader Tag:
xml-cocoon2 RepositoryClassLoader.java
Log:
RepositoryClassLoader extends URLClassLoader.
The URLClassLoader is _much_ quicker about resolving classes.
Revision Changes Path
No revision
No revision
1.1.2.14 +21 -157 xml-cocoon/src/org/apache/cocoon/components/classloader/Attic/RepositoryClassLoader.java
Index: RepositoryClassLoader.java
===================================================================
RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/components/classloader/Attic/RepositoryClassLoader.java,v
retrieving revision 1.1.2.13
retrieving revision 1.1.2.14
diff -u -r1.1.2.13 -r1.1.2.14
--- RepositoryClassLoader.java 2000/12/13 16:44:06 1.1.2.13
+++ RepositoryClassLoader.java 2000/12/15 17:57:54 1.1.2.14
@@ -8,12 +8,17 @@
package org.apache.cocoon.components.classloader;
import java.util.Vector;
+import java.util.Iterator;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.MalformedURLException;
+
import org.apache.cocoon.util.IOUtils;
import org.apache.cocoon.util.ClassUtils;
@@ -24,24 +29,19 @@
* A class loader with a growable list of path search directories
*
* @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
- * @version CVS $Revision: 1.1.2.13 $ $Date: 2000/12/13 16:44:06 $
+ * @version CVS $Revision: 1.1.2.14 $ $Date: 2000/12/15 17:57:54 $
*/
-class RepositoryClassLoader extends ClassLoader {
+class RepositoryClassLoader extends URLClassLoader {
/**
* The logger
*/
protected Logger log = LogKit.getLoggerFor("cocoon");
- /**
- * The list of searchable directories
- */
- protected Vector repositories;
/**
* Create an empty new class loader.
*/
public RepositoryClassLoader() {
- super(RepositoryClassLoader.class.getClassLoader());
- this.repositories = new Vector();
+ super(new URL[] {}, ClassUtils.getClassLoader());
}
/**
@@ -50,7 +50,15 @@
* @param repositories List of searchable directories
*/
protected RepositoryClassLoader(Vector repositories) {
- this.repositories = repositories;
+ this();
+ Iterator i = repositories.iterator();
+ while (i.hasNext()) {
+ try {
+ this.addDirectory((File) i.next());
+ } catch (IOException ioe) {
+ log.error("Repository could not be added", ioe);
+ }
+ }
}
/**
@@ -62,155 +70,11 @@
* repository
*/
public void addDirectory(File repository) throws IOException {
- String fullFilename = null;
-
- if (repository == null) {
- throw new IOException("You cannot add a null directory");
- }
-
- // Ensure the same directory isn't specified twice
- try {
- int count = this.repositories.size();
- fullFilename = IOUtils.getFullFilename(repository);
-
- for (int i = 0; i < count; i++) {
- File directory = (File) this.repositories.elementAt(i);
- if (fullFilename.equals(IOUtils.getFullFilename(directory))) {
- return;
- }
- }
-
- if (!repository.exists()) {
- throw new IOException("Non-existent: " + fullFilename);
- }
-
- if (!repository.isDirectory()) {
- throw new IOException("Not a directory: " + fullFilename);
- }
-
- if (!(repository.canRead() && repository.canWrite())) {
- throw new IOException("Not readable/writable: " + fullFilename);
- }
-
- this.repositories.addElement(repository);
- } catch (SecurityException se) {
- log.debug("RepositoryClassLoader:SecurityException", se);
- throw new IOException("Cannot access directory");
- }
- }
-
- /**
- * Load a class using the parent class loader or, failing that,
- * from one of the stored repositories in definition order
- *
- * @param name The class name
- * @param resolve Whether the class name must be resolved
- * @return The loaded class
- * @exception ClassNotFoundException If the class is not found in any of the
- * repositories
- */
- protected Class loadClass (String name, boolean resolve)
- throws ClassNotFoundException
- {
- Class c = findLoadedClass(name);
-
- if (c == null) {
try {
- c = findSystemClass(name);
- } catch (ClassNotFoundException e) {
- log.debug("Could not load class " + name + " trying to load from the repository");
- byte[] bits = this.loadClassData (name);
-
- if (bits == null) {
- ClassLoader cl = ClassUtils.getClassLoader();
-
- if (cl != null) {
- c = cl.loadClass (name);
- }
- } else {
- c = defineClass (null, bits, 0, bits.length);
-
- if (resolve) {
- resolveClass (c);
- }
- }
+ this.addURL(repository.toURL());
+ } catch (MalformedURLException mue) {
+ log.error("The repository had a bad URL", mue);
+ throw new IOException("Could not add repository");
}
-
- if (c == null) {
- throw new ClassNotFoundException (name);
- }
- }
-
- return c;
- }
-
- /**
- * Load class from a file contained in a repository.
- *
- * @param className The class name
- * @return An array of byes containing the class data or <code>null</code>
if
- * not founfd
- */
- protected byte[] loadClassData (String className) {
- int count = this.repositories.size();
- for (int i = 0; i < count; i++) {
- File repository = (File) this.repositories.elementAt(i);
- File file = new File(repository, this.getClassFilename(className));
-
- if (file.exists() && file.isFile() && file.canRead()) {
- byte[] buffer = null;
- FileInputStream in = null;
-
- int n = 0;
- int pos = 0;
- buffer = new byte [(int) file.length ()];
-
- try {
- boolean process = true;
- log.debug("Loading file: " + file.getCanonicalPath());
-
- in = new FileInputStream(file);
-
- while (process) {
- if (pos < buffer.length) {
- n = in.read (buffer, pos, buffer.length - pos);
- if (n != -1) {
- pos += n;
- } else {
- process = false;
- }
- } else {
- process = false;
- }
- }
-
- log.debug(n + " Bytes read.");
-
- return buffer;
- } catch (IOException e) {
- log.warn("RepositoryClassLoader.IOException", e);
- } finally {
- if (in != null) {
- try { in.close(); }
- catch (IOException e) { log.warn("Could not close stream", e);
}
- }
- }
- }
- }
-
- return null;
- }
-
- /**
- * Return the filename associated with a given class name in a given
- * directory
- *
- * @param className The class name for which a filename is to be generated
- * @param repository The directory containing the class file
- * @return The filename associated with a given class name in a given
- * directory
- */
- protected String getClassFilename(String className) {
- return className.replace('.', File.separatorChar) + ".class";
}
}
|