KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > classloader > RepositoryClassLoader


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 /**
34  * A class loader with a growable list of path search directories.
35  * BL: Changed to extend URLClassLoader for both maintenance and
36  * compatibility reasons. It doesn't hurt that it runs quicker
37  * now as well.
38  *
39  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
40  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
41  * @version $Id: RepositoryClassLoader.java 161488 2005-04-15 15:54:05Z vgritsenko $
42  */

43 public class RepositoryClassLoader extends URLClassLoader JavaDoc
44                                    implements LogEnabled {
45
46     /**
47      * The logger
48      */

49     protected Logger log;
50
51     /**
52      * Create an empty new class loader.
53      */

54     public RepositoryClassLoader() {
55         super(new URL JavaDoc[]{}, ClassUtils.getClassLoader());
56     }
57
58     /**
59      * Create an empty new class loader.
60      */

61     public RepositoryClassLoader(URL JavaDoc[] urls) {
62         super(urls, ClassUtils.getClassLoader());
63     }
64
65     /**
66      * Create an empty new class loader.
67      */

68     public RepositoryClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parentClassLoader) {
69         super(urls, parentClassLoader);
70     }
71
72     /**
73      * Create a class loader from a list of directories
74      *
75      * @param repositories List of searchable directories
76      */

77     protected RepositoryClassLoader(Vector JavaDoc repositories) {
78         this((Collection JavaDoc) repositories);
79     }
80
81     /**
82      * Create a class loader from a list of directories
83      *
84      * @param repositories List of searchable directories
85      */

86     protected RepositoryClassLoader(Collection JavaDoc repositories) {
87         this();
88         Iterator JavaDoc i = repositories.iterator();
89         while (i.hasNext()) {
90             try {
91                 this.addDirectory((File JavaDoc) i.next());
92             } catch (IOException ioe) {
93                 log.error("Repository could not be added", ioe);
94             }
95         }
96     }
97
98     /**
99      * Provide component with a logger.
100      *
101      * @param logger the logger
102      */

103     public void enableLogging(Logger logger) {
104         if (this.log == null) {
105             this.log = logger;
106         }
107     }
108
109     /**
110      * Add a directory to the list of searchable repositories. This methods ensures that no directory is specified more
111      * than once.
112      *
113      * @param repository The directory path
114      * @throws IOException Non-existent, non-readable or non-directory repository
115      */

116     public void addDirectory(File JavaDoc repository) throws IOException {
117         try {
118             this.addURL(repository.getCanonicalFile().toURL());
119         } catch (MalformedURLException JavaDoc mue) {
120             log.error("The repository had a bad URL", mue);
121             throw new CascadingIOException("Could not add repository", mue);
122         }
123     }
124
125     /**
126      * Add a directory to the list of searchable repositories. This methods ensures that no directory is specified more
127      * than once.
128      *
129      * @param repository The directory path
130      * @throws IOException Non-existent, non-readable or non-directory repository
131      */

132     public void addDirectory(String JavaDoc repository) throws IOException {
133         try {
134             File JavaDoc file = new File JavaDoc(repository);
135             this.addURL(file.getCanonicalFile().toURL());
136         } catch (MalformedURLException JavaDoc mue) {
137             log.error("The repository had a bad URL", mue);
138             throw new CascadingIOException("Could not add repository", mue);
139         }
140     }
141
142     /**
143      * Add a url to the list of searchable repositories
144      */

145     public void addURL(URL JavaDoc url) {
146         super.addURL(url);
147     }
148
149     /**
150      * Create a Class from a byte array
151      */

152     public Class JavaDoc defineClass(byte[] b) throws ClassFormatError JavaDoc {
153         return super.defineClass(null, b, 0, b.length);
154     }
155 }
156
Popular Tags