001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/ClassLoaderFactory.java,v 1.8 2002/02/17 08:26:02 remm Exp $
003: * $Revision: 1.8 $
004: * $Date: 2002/02/17 08:26:02 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.startup;
065:
066: import java.io.File;
067: import java.io.IOException;
068: import java.net.URL;
069: import java.util.ArrayList;
070: import java.util.jar.JarEntry;
071: import java.util.jar.JarFile;
072: import org.apache.catalina.loader.StandardClassLoader;
073:
074: /**
075: * <p>Utility class for building class loaders for Catalina. The factory
076: * method requires the following parameters in order to build a new class
077: * loader (with suitable defaults in all cases):</p>
078: * <ul>
079: * <li>A set of directories containing unpacked classes (and resources)
080: * that should be included in the class loader's
081: * repositories.</li>
082: * <li>A set of directories containing classes and resources in JAR files.
083: * Each readable JAR file discovered in these directories will be
084: * added to the class loader's repositories.</li>
085: * <li><code>ClassLoader</code> instance that should become the parent of
086: * the new class loader.</li>
087: * </ul>
088: *
089: * @author Craig R. McClanahan
090: * @version $Revision: 1.8 $ $Date: 2002/02/17 08:26:02 $
091: */
092:
093: public final class ClassLoaderFactory {
094:
095: // ------------------------------------------------------- Static Variables
096:
097: /**
098: * Debugging detail level for processing the startup.
099: */
100: private static int debug = 0;
101:
102: // ------------------------------------------------------ Static Properties
103:
104: /**
105: * Return the debugging detail level.
106: */
107: public static int getDebug() {
108:
109: return (debug);
110:
111: }
112:
113: /**
114: * Set the debugging detail level.
115: *
116: * @param newDebug The new debugging detail level
117: */
118: public static void setDebug(int newDebug) {
119:
120: debug = newDebug;
121:
122: }
123:
124: // --------------------------------------------------------- Public Methods
125:
126: /**
127: * Create and return a new class loader, based on the configuration
128: * defaults and the specified directory paths:
129: *
130: * @param unpacked Array of pathnames to unpacked directories that should
131: * be added to the repositories of the class loader, or <code>null</code>
132: * for no unpacked directories to be considered
133: * @param packed Array of pathnames to directories containing JAR files
134: * that should be added to the repositories of the class loader,
135: * or <code>null</code> for no directories of JAR files to be considered
136: * @param parent Parent class loader for the new class loader, or
137: * <code>null</code> for the system class loader.
138: *
139: * @exception Exception if an error occurs constructing the class loader
140: */
141: public static ClassLoader createClassLoader(File unpacked[],
142: File packed[], ClassLoader parent) throws Exception {
143:
144: if (debug >= 1)
145: log("Creating new class loader");
146:
147: // Construct the "class path" for this class loader
148: ArrayList list = new ArrayList();
149:
150: // Add unpacked directories
151: if (unpacked != null) {
152: for (int i = 0; i < unpacked.length; i++) {
153: File file = unpacked[i];
154: if (!file.isDirectory() || !file.exists()
155: || !file.canRead())
156: continue;
157: if (debug >= 1)
158: log(" Including directory "
159: + file.getAbsolutePath());
160: URL url = new URL("file", null, file.getCanonicalPath()
161: + File.separator);
162: list.add(url.toString());
163: }
164: }
165:
166: // Add packed directory JAR files
167: if (packed != null) {
168: for (int i = 0; i < packed.length; i++) {
169: File directory = packed[i];
170: if (!directory.isDirectory() || !directory.exists()
171: || !directory.canRead())
172: continue;
173: String filenames[] = directory.list();
174: for (int j = 0; j < filenames.length; j++) {
175: String filename = filenames[j].toLowerCase();
176: if (!filename.endsWith(".jar"))
177: continue;
178: File file = new File(directory, filenames[j]);
179: if (debug >= 1)
180: log(" Including jar file "
181: + file.getAbsolutePath());
182: URL url = new URL("file", null, file
183: .getCanonicalPath());
184: list.add(url.toString());
185: }
186: }
187: }
188:
189: // Construct the class loader itself
190: String array[] = (String[]) list
191: .toArray(new String[list.size()]);
192: StandardClassLoader classLoader = null;
193: if (parent == null)
194: classLoader = new StandardClassLoader(array);
195: else
196: classLoader = new StandardClassLoader(array, parent);
197: classLoader.setDelegate(true);
198: return (classLoader);
199:
200: }
201:
202: // -------------------------------------------------------- Private Methods
203:
204: /**
205: * Log a message for this class.
206: *
207: * @param message Message to be logged
208: */
209: private static void log(String message) {
210:
211: System.out.print("ClassLoaderFactory: ");
212: System.out.println(message);
213:
214: }
215:
216: /**
217: * Log a message and exception for this class.
218: *
219: * @param message Message to be logged
220: * @param exception Exception to be logged
221: */
222: private static void log(String message, Throwable exception) {
223:
224: log(message);
225: exception.printStackTrace(System.out);
226:
227: }
228:
229: }
|