001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.classloader;
018:
019: import org.apache.avalon.framework.logger.LogEnabled;
020: import org.apache.avalon.framework.logger.Logger;
021:
022: import org.apache.cocoon.CascadingIOException;
023: import org.apache.cocoon.util.ClassUtils;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.net.URLClassLoader;
030: import java.util.Collection;
031: import java.util.Iterator;
032: import java.util.Vector;
033:
034: /**
035: * A class loader with a growable list of path search directories.
036: * BL: Changed to extend URLClassLoader for both maintenance and
037: * compatibility reasons. It doesn't hurt that it runs quicker
038: * now as well.
039: *
040: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
041: * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
042: * @version $Id: RepositoryClassLoader.java 433543 2006-08-22 06:22:54Z crossley $
043: */
044: public class RepositoryClassLoader extends URLClassLoader implements
045: LogEnabled {
046:
047: /**
048: * The logger
049: */
050: protected Logger log;
051:
052: /**
053: * Create an empty new class loader.
054: */
055: public RepositoryClassLoader() {
056: super (new URL[] {}, ClassUtils.getClassLoader());
057: }
058:
059: /**
060: * Create an empty new class loader.
061: */
062: public RepositoryClassLoader(URL[] urls) {
063: super (urls, ClassUtils.getClassLoader());
064: }
065:
066: /**
067: * Create an empty new class loader.
068: */
069: public RepositoryClassLoader(URL[] urls,
070: ClassLoader parentClassLoader) {
071: super (urls, parentClassLoader);
072: }
073:
074: /**
075: * Create a class loader from a list of directories
076: *
077: * @param repositories List of searchable directories
078: */
079: protected RepositoryClassLoader(Vector repositories) {
080: this ((Collection) repositories);
081: }
082:
083: /**
084: * Create a class loader from a list of directories
085: *
086: * @param repositories List of searchable directories
087: */
088: protected RepositoryClassLoader(Collection repositories) {
089: this ();
090: Iterator i = repositories.iterator();
091: while (i.hasNext()) {
092: try {
093: this .addDirectory((File) i.next());
094: } catch (IOException ioe) {
095: log.error("Repository could not be added", ioe);
096: }
097: }
098: }
099:
100: /**
101: * Provide component with a logger.
102: *
103: * @param logger the logger
104: */
105: public void enableLogging(Logger logger) {
106: if (this .log == null) {
107: this .log = logger;
108: }
109: }
110:
111: /**
112: * Add a directory to the list of searchable repositories. This methods ensures that no directory is specified more
113: * than once.
114: *
115: * @param repository The directory path
116: * @throws IOException Non-existent, non-readable or non-directory repository
117: */
118: public void addDirectory(File repository) throws IOException {
119: try {
120: this .addURL(repository.getCanonicalFile().toURL());
121: } catch (MalformedURLException mue) {
122: log.error("The repository had a bad URL", mue);
123: throw new CascadingIOException("Could not add repository",
124: mue);
125: }
126: }
127:
128: /**
129: * Add a directory to the list of searchable repositories. This methods ensures that no directory is specified more
130: * than once.
131: *
132: * @param repository The directory path
133: * @throws IOException Non-existent, non-readable or non-directory repository
134: */
135: public void addDirectory(String repository) throws IOException {
136: try {
137: File file = new File(repository);
138: this .addURL(file.getCanonicalFile().toURL());
139: } catch (MalformedURLException mue) {
140: log.error("The repository had a bad URL", mue);
141: throw new CascadingIOException("Could not add repository",
142: mue);
143: }
144: }
145:
146: /**
147: * Add a url to the list of searchable repositories
148: */
149: public void addURL(URL url) {
150: super .addURL(url);
151: }
152:
153: /**
154: * Create a Class from a byte array
155: */
156: public Class defineClass(byte[] b) throws ClassFormatError {
157: return super .defineClass(null, b, 0, b.length);
158: }
159: }
|