
org.apache.xbean.classloader.JarFileClassLoader Maven / Gradle / Ivy
/**
* 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.xbean.classloader;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.cert.Certificate;
import java.util.Collection;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
/**
* The JarFileClassLoader that loads classes and resources from a list of JarFiles. This method is similar to
* URLClassLoader except it properly closes JarFiles when the classloader is destroyed so that the file read lock will
* be released, and the jar file can be modified and deleted.
*
* @author Dain Sundstrom
*
* @see NonLockingJarFile
*/
public class JarFileClassLoader extends MultiParentClassLoader {
private static final URL[] EMPTY_URLS = new URL[0];
private UrlResourceFinder resourceFinder;
private AccessControlContext accessControlContext;
/**
* Creates a JarFileClassLoader that is a child of the system class loader.
* @param name the name of this class loader
* @param urls a list of URLs from which classes and resources should be loaded
*/
public JarFileClassLoader(String name, URL[] urls) {
super(name, EMPTY_URLS);
initialize(urls);
}
/**
* Creates a JarFileClassLoader that is a child of the specified class loader.
* @param name the name of this class loader
* @param urls a list of URLs from which classes and resources should be loaded
* @param parent the parent of this class loader
*/
public JarFileClassLoader(String name, URL[] urls, ClassLoader parent) {
super(name, EMPTY_URLS, parent);
initialize(urls);
}
/**
* Creates a JarFileClassLoader that is a child of the specified class loader.
* @param name
* @param urls
* @param parent
* @param inverseClassLoading
* @param hiddenClasses
* @param nonOverridableClasses
*/
public JarFileClassLoader(String name, URL[] urls, ClassLoader parent, boolean inverseClassLoading,
String[] hiddenClasses, String[] nonOverridableClasses) {
super(name, EMPTY_URLS, parent, inverseClassLoading, hiddenClasses, nonOverridableClasses);
initialize(urls);
}
/**
* Creates a named class loader as a child of the specified parents.
* @param name the name of this class loader
* @param urls the urls from which this class loader will classes and resources
* @param parents the parents of this class loader
*/
public JarFileClassLoader(String name, URL[] urls, ClassLoader[] parents) {
super(name, EMPTY_URLS, parents);
initialize(urls);
}
/**
* Creates a JarFileClassLoader that is a child of the specified class loaders.
* @param name
* @param urls
* @param parents
* @param inverseClassLoading
* @param hiddenClasses
* @param nonOverridableClasses
*/
public JarFileClassLoader(String name, URL[] urls, ClassLoader[] parents, boolean inverseClassLoading,
Collection hiddenClasses, Collection nonOverridableClasses) {
super(name, EMPTY_URLS, parents, inverseClassLoading, hiddenClasses, nonOverridableClasses);
initialize(urls);
}
/**
* Creates a JarFileClassLoader that is a child of the specified class loaders.
* @param name
* @param urls
* @param parents
* @param inverseClassLoading
* @param hiddenClasses
* @param nonOverridableClasses
*/
public JarFileClassLoader(String name, URL[] urls, ClassLoader[] parents, boolean inverseClassLoading,
String[] hiddenClasses, String[] nonOverridableClasses) {
super(name, EMPTY_URLS, parents, inverseClassLoading, hiddenClasses, nonOverridableClasses);
initialize(urls);
}
/**
* Determine if URLs should be added during {@link #initialize(URL[])}. The default implementation returns
* true, subclasses can override if they need to perform additional setups before {@link #addURLs(URL[])}
* is called.
*
* @return true if URLs should be added on {@link #initialize(URL[])}
*/
protected boolean addUrlsOnInitialize() {
return true;
}
/**
* Initialize method called from all constructors.
*
* @param urls
*/
protected void initialize(URL[] urls) {
this.accessControlContext = AccessController.getContext();
setResourceFinder(newResourceFinder());
if (addUrlsOnInitialize()) {
addURLs(urls);
}
}
/**
* Factory method used to create the resource finder used with this class loader. The default implementation returns
* a {@link UrlResourceFinder}.
*
* @return A {@link UrlResourceFinder}.
* @see #setResourceFinder(UrlResourceFinder)
*/
protected UrlResourceFinder newResourceFinder() {
return new UrlResourceFinder(null);
}
/**
* Set the resource finder that will be used to perform all resource operations. Generally this method will not be
* called directly, instead the {@link #newResourceFinder()} factory method should be overridden. This method is
* primarily intended for test cases to use.
*
* @param resourceFinder
*/
protected final void setResourceFinder(UrlResourceFinder resourceFinder) {
this.resourceFinder = resourceFinder;
}
/**
* {@inheritDoc}
*/
public URL[] getURLs() {
return resourceFinder.getUrls();
}
/**
* {@inheritDoc}
*/
public void addURL(final URL url) {
AccessController.doPrivileged(new PrivilegedAction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy