📄 standardclassloader.java
字号:
*
* @param path file directory path
*/
protected void setPermissions(String path) {
if( securityManager != null ) {
if( path.startsWith("jndi:") || path.startsWith("jar:jndi:") ) {
permissionList.add(new JndiPermission(path + "*"));
} else {
permissionList.add(new FilePermission(path + "-","read"));
}
}
}
/**
* If there is a Java SecurityManager add a read FilePermission
* or JndiPermission for URL.
*
* @param url URL for a file or directory on local system
*/
protected void setPermissions(URL url) {
setPermissions(url.toString());
}
// ------------------------------------------------------- Reloader Methods
/**
* Add a new repository to the set of places this ClassLoader can look for
* classes to be loaded.
*
* @param repository Name of a source of classes to be loaded, such as a
* directory pathname, a JAR file pathname, or a ZIP file pathname
*
* @exception IllegalArgumentException if the specified repository is
* invalid or does not exist
*/
public void addRepository(String repository) {
if (debug >= 1)
log("addRepository(" + repository + ")");
// Add this repository to our underlying class loader
try {
URLStreamHandler streamHandler = null;
String protocol = parseProtocol(repository);
if (factory != null)
streamHandler = factory.createURLStreamHandler(protocol);
URL url = new URL(null, repository, streamHandler);
super.addURL(url);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e.toString());
}
// Add this repository to our internal list
addRepositoryInternal(repository);
}
/**
* Return a String array of the current repositories for this class
* loader. If there are no repositories, a zero-length array is
* returned. For security reason, returns a clone of the Array (since
* String are immutable).
*/
public String[] findRepositories() {
return ((String[])repositories.clone());
}
/**
* This class loader doesn't check for reloading.
*/
public boolean modified() {
return (false);
}
/**
* Render a String representation of this object.
*/
public String toString() {
StringBuffer sb = new StringBuffer("StandardClassLoader\r\n");
sb.append(" delegate: ");
sb.append(delegate);
sb.append("\r\n");
sb.append(" repositories:\r\n");
for (int i = 0; i < repositories.length; i++) {
sb.append(" ");
sb.append(repositories[i]);
sb.append("\r\n");
}
if (this.parent != null) {
sb.append("----------> Parent Classloader:\r\n");
sb.append(this.parent.toString());
sb.append("\r\n");
}
return (sb.toString());
}
// ---------------------------------------------------- ClassLoader Methods
/**
* Find the specified class in our local repositories, if possible. If
* not found, throw <code>ClassNotFoundException</code>.
*
* @param name Name of the class to be loaded
*
* @exception ClassNotFoundException if the class was not found
*/
public Class findClass(String name) throws ClassNotFoundException {
if (debug >= 3)
log(" findClass(" + name + ")");
// (1) Permission to define this class when using a SecurityManager
if (securityManager != null) {
int i = name.lastIndexOf('.');
if (i >= 0) {
try {
if (debug >= 4)
log(" securityManager.checkPackageDefinition");
securityManager.checkPackageDefinition(name.substring(0,i));
} catch (Exception se) {
if (debug >= 4)
log(" -->Exception-->ClassNotFoundException", se);
throw new ClassNotFoundException(name);
}
}
}
// Ask our superclass to locate this class, if possible
// (throws ClassNotFoundException if it is not found)
Class clazz = null;
try {
if (debug >= 4)
log(" super.findClass(" + name + ")");
try {
synchronized (this) {
clazz = findLoadedClass(name);
if (clazz != null)
return clazz;
clazz = super.findClass(name);
}
} catch(AccessControlException ace) {
throw new ClassNotFoundException(name);
} catch (RuntimeException e) {
if (debug >= 4)
log(" -->RuntimeException Rethrown", e);
throw e;
}
if (clazz == null) {
if (debug >= 3)
log(" --> Returning ClassNotFoundException");
throw new ClassNotFoundException(name);
}
} catch (ClassNotFoundException e) {
if (debug >= 3)
log(" --> Passing on ClassNotFoundException", e);
throw e;
}
// Return the class we have located
if (debug >= 4)
log(" Returning class " + clazz);
if ((debug >= 4) && (clazz != null))
log(" Loaded by " + clazz.getClassLoader());
return (clazz);
}
/**
* Find the specified resource in our local repository, and return a
* <code>URL</code> refering to it, or <code>null</code> if this resource
* cannot be found.
*
* @param name Name of the resource to be found
*/
public URL findResource(String name) {
if (debug >= 3)
log(" findResource(" + name + ")");
URL url = super.findResource(name);
if (debug >= 3) {
if (url != null)
log(" --> Returning '" + url.toString() + "'");
else
log(" --> Resource not found, returning null");
}
return (url);
}
/**
* Return an enumeration of <code>URLs</code> representing all of the
* resources with the given name. If no resources with this name are
* found, return an empty enumeration.
*
* @param name Name of the resources to be found
*
* @exception IOException if an input/output error occurs
*/
public Enumeration findResources(String name) throws IOException {
if (debug >= 3)
log(" findResources(" + name + ")");
return (super.findResources(name));
}
/**
* Find the resource with the given name. A resource is some data
* (images, audio, text, etc.) that can be accessed by class code in a
* way that is independent of the location of the code. The name of a
* resource is a "/"-separated path name that identifies the resource.
* If the resource cannot be found, return <code>null</code>.
* <p>
* This method searches according to the following algorithm, returning
* as soon as it finds the appropriate URL. If the resource cannot be
* found, returns <code>null</code>.
* <ul>
* <li>If the <code>delegate</code> property is set to <code>true</code>,
* call the <code>getResource()</code> method of the parent class
* loader, if any.</li>
* <li>Call <code>findResource()</code> to find this resource in our
* locally defined repositories.</li>
* <li>Call the <code>getResource()</code> method of the parent class
* loader, if any.</li>
* </ul>
*
* @param name Name of the resource to return a URL for
*/
public URL getResource(String name) {
if (debug >= 2)
log("getResource(" + name + ")");
URL url = null;
// (1) Delegate to parent if requested
if (delegate) {
if (debug >= 3)
log(" Delegating to parent classloader");
ClassLoader loader = parent;
if (loader == null)
loader = system;
url = loader.getResource(name);
if (url != null) {
if (debug >= 2)
log(" --> Returning '" + url.toString() + "'");
return (url);
}
}
// (2) Search local repositories
if (debug >= 3)
log(" Searching local repositories");
url = findResource(name);
if (url != null) {
if (debug >= 2)
log(" --> Returning '" + url.toString() + "'");
return (url);
}
// (3) Delegate to parent unconditionally if not already attempted
if( !delegate ) {
ClassLoader loader = parent;
if (loader == null)
loader = system;
url = loader.getResource(name);
if (url != null) {
if (debug >= 2)
log(" --> Returning '" + url.toString() + "'");
return (url);
}
}
// (4) Resource was not found
if (debug >= 2)
log(" --> Resource not found, returning null");
return (null);
}
/**
* Find the resource with the given name, and return an input stream
* that can be used for reading it. The search order is as described
* for <code>getResource()</code>, after checking to see if the resource
* data has been previously cached. If the resource cannot be found,
* return <code>null</code>.
*
* @param name Name of the resource to return an input stream for
*/
public InputStream getResourceAsStream(String name) {
if (debug >= 2)
log("getResourceAsStream(" + name + ")");
InputStream stream = null;
// (0) Check for a cached copy of this resource
stream = findLoadedResource(name);
if (stream != null) {
if (debug >= 2)
log(" --> Returning stream from cache");
return (stream);
}
// (1) Delegate to parent if requested
if (delegate) {
if (debug >= 3)
log(" Delegating to parent classloader");
ClassLoader loader = parent;
if (loader == null)
loader = system;
stream = loader.getResourceAsStream(name);
if (stream != null) {
// FIXME - cache???
if (debug >= 2)
log(" --> Returning stream from parent");
return (stream);
}
}
// (2) Search local repositories
if (debug >= 3)
log(" Searching local repositories");
URL url = findResource(name);
if (url != null) {
// FIXME - cache???
if (debug >= 2)
log(" --> Returning stream from local");
try {
return (url.openStream());
} catch (IOException e) {
log("url.openStream(" + url.toString() + ")", e);
return (null);
}
}
// (3) Delegate to parent unconditionally
if (!delegate) {
if (debug >= 3)
log(" Delegating to parent classloader");
ClassLoader loader = parent;
if (loader == null)
loader = system;
stream = loader.getResourceAsStream(name);
if (stream != null) {
// FIXME - cache???
if (debug >= 2)
log(" --> Returning stream from parent");
return (stream);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -