1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.startup;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.catalina.loader.StandardClassLoader;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
/**
* <p>Utility class for building class loaders for Catalina. The factory
* method requires the following parameters in order to build a new class
* loader (with suitable defaults in all cases):</p>
* <ul>
* <li>A set of directories containing unpacked classes (and resources)
* that should be included in the class loader's
* repositories.</li>
* <li>A set of directories containing classes and resources in JAR files.
* Each readable JAR file discovered in these directories will be
* added to the class loader's repositories.</li>
* <li><code>ClassLoader</code> instance that should become the parent of
* the new class loader.</li>
* </ul>
*
* @author Craig R. McClanahan
*
*/
public final class ClassLoaderFactory {
private static Log log = LogFactory.getLog(ClassLoaderFactory.class);
protected static final Integer IS_DIR = new Integer(0);
protected static final Integer IS_JAR = new Integer(1);
protected static final Integer IS_GLOB = new Integer(2);
protected static final Integer IS_URL = new Integer(3);
// --------------------------------------------------------- Public Methods
/**
* Create and return a new class loader, based on the configuration
* defaults and the specified directory paths:
*
* @param unpacked Array of pathnames to unpacked directories that should
* be added to the repositories of the class loader, or <code>null</code>
* for no unpacked directories to be considered
* @param packed Array of pathnames to directories containing JAR files
* that should be added to the repositories of the class loader,
* or <code>null</code> for no directories of JAR files to be considered
* @param parent Parent class loader for the new class loader, or
* <code>null</code> for the system class loader.
*
* @exception Exception if an error occurs constructing the class loader
*/
public static ClassLoader createClassLoader(File unpacked[],
File packed[],
ClassLoader parent)
throws Exception {
return createClassLoader(unpacked, packed, null, parent);
}
/**
* Create and return a new class loader, based on the configuration
* defaults and the specified directory paths:
*
* @param unpacked Array of pathnames to unpacked directories that should
* be added to the repositories of the class loader, or <code>null</code>
* for no unpacked directories to be considered
* @param packed Array of pathnames to directories containing JAR files
* that should be added to the repositories of the class loader,
* or <code>null</code> for no directories of JAR files to be considered
* @param urls Array of URLs to remote repositories, designing either JAR
* resources or uncompressed directories that should be added to
* the repositories of the class loader, or <code>null</code> for no
* directories of JAR files to be considered
* @param parent Parent class loader for the new class loader, or
* <code>null</code> for the system class loader.
*
* @exception Exception if an error occurs constructing the class loader
*/
public static ClassLoader createClassLoader(File unpacked[],
File packed[],
URL urls[],
ClassLoader parent)
throws Exception {
if (log.isDebugEnabled())
log.debug("Creating new class loader");
// Construct the "class path" for this class loader
Set<URL> set = new LinkedHashSet<URL>();
// Add unpacked directories
if (unpacked != null) {
for (int i = 0; i < unpacked.length; i++) {
File file = unpacked[i];
if (!file.exists() || !file.canRead())
continue;
file = new File(file.getCanonicalPath() + File.separator);
URL url = file.toURI().toURL();
if (log.isDebugEnabled())
log.debug(" Including directory " + url);
set.add(url);
}
}
// Add packed directory JAR files
if (packed != null) {
for (int i = 0; i < packed.length; i++) {
File directory = packed[i];
if (!directory.isDirectory() || !directory.exists() ||
!directory.canRead())
continue;
String filenames[] = directory.list();
for (int j = 0; j < filenames.length; j++) {
String filename = filenames[j].toLowerCase();
if (!filename.endsWith(".jar"))
continue;
File file = new File(directory, filenames[j]);
if (log.isDebugEnabled())
log.debug(" Including jar file " + file.getAbsolutePath());
URL url = file.toURI().toURL();
set.add(url);
}
}
}
// Construct the class loader itself
URL[] array = set.toArray(new URL[set.size()]);
StandardClassLoader classLoader = null;
if (parent == null)
classLoader = new StandardClassLoader(array);
else
classLoader = new StandardClassLoader(array, parent);
return (classLoader);
}
/**
* Create and return a new class loader, based on the configuration
* defaults and the specified directory paths:
*
* @param locations Array of strings containing class directories, jar files,
* jar directories or URLS that should be added to the repositories of
* the class loader. The type is given by the member of param types.
* @param types Array of types for the members of param locations.
* Possible values are IS_DIR (class directory), IS_JAR (single jar file),
* IS_GLOB (directory of jar files) and IS_URL (URL).
* @param parent Parent class loader for the new class loader, or
* <code>null</code> for the system class loader.
*
* @exception Exception if an error occurs constructing the class loader
*/
public static ClassLoader createClassLoader(String locations[],
Integer types[],
ClassLoader parent)
throws Exception {
if (log.isDebugEnabled())
log.debug("Creating new class loader");
// Construct the "class path" for this class loader
Set<URL> set = new LinkedHashSet<URL>();
if (locations != null && types != null && locations.length == types.length) {
for (int i = 0; i < locations.length; i++) {
String location = locations[i];
if ( types[i] == IS_URL ) {
URL url = new URL(location);
if (log.isDebugEnabled())
log.debug(" Including URL " + url);
set.add(url);
} else if ( types[i] == IS_DIR ) {
File directory = new File(location);
directory = directory.getCanonicalFile();
if (!validateFile(directory, IS_DIR)) {
continue;
}
URL url = directory.toURI().toURL();
if (log.isDebugEnabled())
log.debug(" Including directory " + url);
set.add(url);
} else if ( types[i] == IS_JAR ) {
File file=new File(location);
file = file.getCanonicalFile();
if (!validateFile(file, IS_JAR)) {
continue;
}
URL url = file.toURI().toURL();
if (log.isDebugEnabled())
log.debug(" Including jar file " + url);
set.add(url);
} else if ( types[i] == IS_GLOB ) {
File directory=new File(location);
directory = directory.getCanonicalFile();
if (!validateFile(directory, IS_GLOB)) {
continue;
}
if (log.isDebugEnabled())
log.debug(" Including directory glob "
+ directory.getAbsolutePath());
String filenames[] = directory.list();
for (int j = 0; j < filenames.length; j++) {
String filename = filenames[j].toLowerCase();
if (!filename.endsWith(".jar"))
continue;
File file = new File(directory, filenames[j]);
file = file.getCanonicalFile();
if (!validateFile(file, IS_JAR)) {
continue;
}
if (log.isDebugEnabled())
log.debug(" Including glob jar file "
+ file.getAbsolutePath());
URL url = file.toURI().toURL();
set.add(url);
}
}
}
}
// Construct the class loader itself
URL[] array = set.toArray(new URL[set.size()]);
if (log.isDebugEnabled())
for (int i = 0; i < array.length; i++) {
log.debug(" location " + i + " is " + array[i]);
}
StandardClassLoader classLoader = null;
if (parent == null)
classLoader = new StandardClassLoader(array);
else
classLoader = new StandardClassLoader(array, parent);
return (classLoader);
}
private static boolean validateFile(File file,
Integer type) throws IOException {
if (type == IS_DIR || type == IS_GLOB) {
if (!file.exists() || !file.isDirectory() || !file.canRead()) {
String msg = "Problem with directory [" + file +
"], exists: [" + file.exists() +
"], isDirectory: [" + file.isDirectory() +
"], canRead: [" + file.canRead() + "]";
File home = new File (Bootstrap.getCatalinaHome());
home = home.getCanonicalFile();
File base = new File (Bootstrap.getCatalinaBase());
base = base.getCanonicalFile();
File defaultValue = new File(base, "lib");
// Existence of ${catalina.base}/lib directory is optional.
// Hide the warning if Tomcat runs with separate catalina.home
// and catalina.base and that directory is absent.
if (!home.getPath().equals(base.getPath())
&& file.getPath().equals(defaultValue.getPath())
&& !file.exists()) {
log.debug(msg);
} else {
log.warn(msg);
}
return false;
}
} else if (type == IS_JAR) {
if (!file.exists() || !file.canRead()) {
log.warn("Problem with JAR file [" + file +
"], exists: [" + file.exists() +
"], canRead: [" + file.canRead() + "]");
return false;
}
}
return true;
}
}
|