| Constructor: |
public URLClassLoader(URL[] urls) {
this(urls, ClassLoader.getSystemClassLoader(), null);
}
Constructs a new {@code URLClassLoader} instance. The newly created
instance will have the system ClassLoader as its parent. URLs that end
with "/" are assumed to be directories, otherwise they are assumed to be
JAR files. Parameters:
urls -
the list of URLs where a specific class or file could be
found.
Throws:
SecurityException -
if a security manager exists and its {@code
checkCreateClassLoader()} method doesn't allow creation of
new ClassLoaders.
|
public URLClassLoader(URL[] urls,
ClassLoader parent) {
this(urls, parent, null);
}
Constructs a new URLClassLoader instance. The newly created instance will
have the system ClassLoader as its parent. URLs that end with "/" are
assumed to be directories, otherwise they are assumed to be JAR files. Parameters:
urls -
the list of URLs where a specific class or file could be
found.
parent -
the class loader to assign as this loader's parent.
Throws:
SecurityException -
if a security manager exists and its {@code
checkCreateClassLoader()} method doesn't allow creation of
new class loaders.
|
public URLClassLoader(URL[] searchUrls,
ClassLoader parent,
URLStreamHandlerFactory factory) {
super(parent);
// Required for pre-v1.2 security managers to work
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkCreateClassLoader();
}
this.factory = factory;
// capture the context of the thread that creates this URLClassLoader
currentContext = AccessController.getContext();
int nbUrls = searchUrls.length;
originalUrls = new ArrayList< URL >(nbUrls);
handlerList = new ArrayList< URLHandler >(nbUrls);
searchList = Collections.synchronizedList(new ArrayList< URL >(nbUrls));
for (int i = 0; i < nbUrls; i++) {
originalUrls.add(searchUrls[i]);
try {
searchList.add(createSearchURL(searchUrls[i]));
} catch (MalformedURLException e) {
}
}
}
Constructs a new {@code URLClassLoader} instance. The newly created
instance will have the specified {@code ClassLoader} as its parent and
use the specified factory to create stream handlers. URLs that end with
"/" are assumed to be directories, otherwise they are assumed to be JAR
files. Parameters:
searchUrls -
the list of URLs where a specific class or file could be
found.
parent -
the {@code ClassLoader} to assign as this loader's parent.
factory -
the factory that will be used to create protocol-specific
stream handlers.
Throws:
SecurityException -
if a security manager exists and its {@code
checkCreateClassLoader()} method doesn't allow creation of
new {@code ClassLoader}s.
|
| Method from java.net.URLClassLoader Detail: |
protected void addURL(URL url) {
try {
originalUrls.add(url);
searchList.add(createSearchURL(url));
} catch (MalformedURLException e) {
}
}
Adds the specified URL to the search list. |
protected Package definePackage(String packageName,
Manifest manifest,
URL url) throws IllegalArgumentException {
Attributes mainAttributes = manifest.getMainAttributes();
String dirName = packageName.replace('.', '/') + "/"; //$NON-NLS-1$
Attributes packageAttributes = manifest.getAttributes(dirName);
boolean noEntry = false;
if (packageAttributes == null) {
noEntry = true;
packageAttributes = mainAttributes;
}
String specificationTitle = packageAttributes
.getValue(Attributes.Name.SPECIFICATION_TITLE);
if (specificationTitle == null && !noEntry) {
specificationTitle = mainAttributes
.getValue(Attributes.Name.SPECIFICATION_TITLE);
}
String specificationVersion = packageAttributes
.getValue(Attributes.Name.SPECIFICATION_VERSION);
if (specificationVersion == null && !noEntry) {
specificationVersion = mainAttributes
.getValue(Attributes.Name.SPECIFICATION_VERSION);
}
String specificationVendor = packageAttributes
.getValue(Attributes.Name.SPECIFICATION_VENDOR);
if (specificationVendor == null && !noEntry) {
specificationVendor = mainAttributes
.getValue(Attributes.Name.SPECIFICATION_VENDOR);
}
String implementationTitle = packageAttributes
.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
if (implementationTitle == null && !noEntry) {
implementationTitle = mainAttributes
.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
}
String implementationVersion = packageAttributes
.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
if (implementationVersion == null && !noEntry) {
implementationVersion = mainAttributes
.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
}
String implementationVendor = packageAttributes
.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
if (implementationVendor == null && !noEntry) {
implementationVendor = mainAttributes
.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
}
return definePackage(packageName, specificationTitle,
specificationVersion, specificationVendor, implementationTitle,
implementationVersion, implementationVendor, isSealed(manifest,
dirName) ? url : null);
}
Defines a new package using the information extracted from the specified
manifest. |
protected Class<?> findClass(String clsName) throws ClassNotFoundException {
Class< ? > cls = AccessController.doPrivileged(
new PrivilegedAction< Class< ? > >() {
public Class< ? > run() {
return findClassImpl(clsName);
}
}, currentContext);
if (cls != null) {
return cls;
}
throw new ClassNotFoundException(clsName);
}
Tries to locate and load the specified class using the known URLs. If the
class could be found, a class object representing the loaded class will
be returned. |
Class<?> findClassImpl(String className) {
String partialName = className.replace('.', '/');
final String classFileName = new StringBuilder(partialName).append(".class").toString(); //$NON-NLS-1$
String packageName = null;
int position = partialName.lastIndexOf('/');
if ((position = partialName.lastIndexOf('/')) != -1) {
packageName = partialName.substring(0, position);
}
int n = 0;
while (true) {
URLHandler handler = getHandler(n++);
if (handler == null) {
break;
}
Class< ? > res = handler.findClass(packageName, classFileName, className);
if (res != null) {
return res;
}
}
return null;
}
|
public URL findResource(String name) {
if (name == null) {
return null;
}
URL result = AccessController.doPrivileged(new PrivilegedAction< URL >() {
public URL run() {
return findResourceImpl(name);
}
}, currentContext);
SecurityManager sm;
if (result != null && (sm = System.getSecurityManager()) != null) {
try {
sm.checkPermission(result.openConnection().getPermission());
} catch (IOException e) {
return null;
} catch (SecurityException e) {
return null;
}
}
return result;
}
Returns an URL referencing the specified resource or {@code null} if the
resource could not be found. |
URL findResourceImpl(String resName) {
int n = 0;
while (true) {
URLHandler handler = getHandler(n++);
if (handler == null) {
break;
}
URL res = handler.findResource(resName);
if (res != null) {
return res;
}
}
return null;
}
Returns a URL among the given ones referencing the specified resource or
null if no resource could be found. |
public Enumeration<URL> findResources(String name) throws IOException {
if (name == null) {
return null;
}
ArrayList< URL > result = AccessController.doPrivileged(
new PrivilegedAction< ArrayList< URL > >() {
public ArrayList< URL > run() {
ArrayList< URL > results = new ArrayList< URL >();
findResourcesImpl(name, results);
return results;
}
}, currentContext);
SecurityManager sm;
int length = result.size();
if (length > 0 && (sm = System.getSecurityManager()) != null) {
ArrayList< URL > reduced = new ArrayList< URL >(length);
for (int i = 0; i < length; i++) {
URL url = result.get(i);
try {
sm.checkPermission(url.openConnection().getPermission());
reduced.add(url);
} catch (IOException e) {
} catch (SecurityException e) {
}
}
result = reduced;
}
return Collections.enumeration(result);
}
Returns all known URLs which point to the specified resource. |
void findResourcesImpl(String name,
ArrayList<URL> result) {
int n = 0;
while (true) {
URLHandler handler = getHandler(n++);
if (handler == null) {
break;
}
handler.findResources(name, result);
}
}
|
URLHandler getHandler(int num) {
if (num < handlerList.size()) {
return handlerList.get(num);
}
makeNewHandler();
if (num < handlerList.size()) {
return handlerList.get(num);
}
return null;
}
|
protected PermissionCollection getPermissions(CodeSource codesource) {
PermissionCollection pc = super.getPermissions(codesource);
URL u = codesource.getLocation();
if (u.getProtocol().equals("jar")) { //$NON-NLS-1$
try {
// Create a URL for the resource the jar refers to
u = ((JarURLConnection) u.openConnection()).getJarFileURL();
} catch (IOException e) {
// This should never occur. If it does continue using the jar
// URL
}
}
if (u.getProtocol().equals("file")) { //$NON-NLS-1$
String path = u.getFile();
String host = u.getHost();
if (host != null && host.length() > 0) {
path = "//" + host + path; //$NON-NLS-1$
}
if (File.separatorChar != '/') {
path = path.replace('/', File.separatorChar);
}
if (isDirectory(u)) {
pc.add(new FilePermission(path + "-", "read")); //$NON-NLS-1$ //$NON-NLS-2$
} else {
pc.add(new FilePermission(path, "read")); //$NON-NLS-1$
}
} else {
String host = u.getHost();
if (host.length() == 0) {
host = "localhost"; //$NON-NLS-1$
}
pc.add(new SocketPermission(host, "connect, accept")); //$NON-NLS-1$
}
return pc;
}
Gets all permissions for the specified {@code codesource}. First, this
method retrieves the permissions from the system policy. If the protocol
is "file:/" then a new permission, {@code FilePermission}, granting the
read permission to the file is added to the permission collection.
Otherwise, connecting to and accepting connections from the URL is
granted. |
public URL[] getURLs() {
return originalUrls.toArray(new URL[originalUrls.size()]);
}
Returns the search list of this {@code URLClassLoader}. |
public static URLClassLoader newInstance(URL[] urls) {
URLClassLoader sub = AccessController
.doPrivileged(new PrivilegedAction< URLClassLoader >() {
public URLClassLoader run() {
return new SubURLClassLoader(urls);
}
});
sub.currentContext = AccessController.getContext();
return sub;
}
Returns a new {@code URLClassLoader} instance for the given URLs and the
system {@code ClassLoader} as its parent. The method {@code loadClass()}
of the new instance will call {@code
SecurityManager.checkPackageAccess()} before loading a class. |
public static URLClassLoader newInstance(URL[] urls,
ClassLoader parentCl) {
URLClassLoader sub = AccessController
.doPrivileged(new PrivilegedAction< URLClassLoader >() {
public URLClassLoader run() {
return new SubURLClassLoader(urls, parentCl);
}
});
sub.currentContext = AccessController.getContext();
return sub;
}
Returns a new {@code URLClassLoader} instance for the given URLs and the
specified {@code ClassLoader} as its parent. The method {@code
loadClass()} of the new instance will call the SecurityManager's {@code
checkPackageAccess()} before loading a class. |