📄 standardpluginclassloader.java
字号:
* @see java.lang.ClassLoader#findResources(java.lang.String)
*/
public Enumeration findResources(final String name) throws IOException {
List result = new LinkedList();
findResources(result, name, this, null);
return Collections.enumeration(result);
}
protected URL findResource(final String name,
final StandardPluginClassLoader requestor, final Set seenPlugins) {
/*log.debug("findResource(String,...): name=" + name //$NON-NLS-1$
+ ", this=" + this); //$NON-NLS-1$*/
Set seen = seenPlugins;
if ((seen != null) && seen.contains(getPluginDescriptor().getId())) {
return null;
}
URL result = super.findResource(name);
if (result != null) { // found resource in this plug-in class path
if (log.isDebugEnabled()) {
log.debug("findResource(...): resource found in classpath, name=" //$NON-NLS-1$
+ name + " URL=" + result + ", this=" //$NON-NLS-1$ //$NON-NLS-2$
+ this + ", requestor=" + requestor); //$NON-NLS-1$
}
if (isResourceVisible(name, result, requestor)) {
return result;
}
return null;
}
if (resourceLoader != null) {
result = resourceLoader.findResource(name);
if (result != null) { // found resource in this plug-in resource libraries
if (log.isDebugEnabled()) {
log.debug("findResource(...): resource found in libraries, name=" //$NON-NLS-1$
+ name + ", URL=" + result + ", this=" //$NON-NLS-1$ //$NON-NLS-2$
+ this + ", requestor=" + requestor); //$NON-NLS-1$
}
if (isResourceVisible(name, result, requestor)) {
return result;
}
return null;
}
}
if (seen == null) {
seen = new HashSet();
}
if (log.isDebugEnabled()) {
log.debug("findResource(...): resource not found, name=" //$NON-NLS-1$
+ name + ", this=" //$NON-NLS-1$
+ this + ", requestor=" + requestor); //$NON-NLS-1$
}
seen.add(getPluginDescriptor().getId());
for (int i = 0; i < publicImports.length; i++) {
if (seen.contains(publicImports[i].getId())) {
continue;
}
result = ((StandardPluginClassLoader) getPluginManager()
.getPluginClassLoader(publicImports[i])).findResource(
name, requestor, seen);
if (result != null) {
break; // found resource in publicly imported plug-in
}
}
if ((this == requestor) && (result == null)) {
for (int i = 0; i < privateImports.length; i++) {
if (seen.contains(privateImports[i].getId())) {
continue;
}
result = ((StandardPluginClassLoader) getPluginManager()
.getPluginClassLoader(privateImports[i])).findResource(
name, requestor, seen);
if (result != null) {
break; // found resource in privately imported plug-in
}
}
}
if ((this == requestor) && (result == null)) {
for (int i = 0; i < reverseLookups.length; i++) {
if (seen.contains(reverseLookups[i].getId())) {
continue;
}
result = ((StandardPluginClassLoader) getPluginManager()
.getPluginClassLoader(reverseLookups[i])).findResource(
name, requestor, seen);
if (result != null) {
break; // found resource in plug-in that marks itself as
// allowed reverse look up
}
}
}
return result;
}
protected void findResources(final List result, final String name,
final StandardPluginClassLoader requestor, final Set seenPlugins)
throws IOException {
Set seen = seenPlugins;
if ((seen != null) && seen.contains(getPluginDescriptor().getId())) {
return;
}
for (Enumeration enm = super.findResources(name);
enm.hasMoreElements();) {
URL url = (URL) enm.nextElement();
if (isResourceVisible(name, url, requestor)) {
result.add(url);
}
}
if (resourceLoader != null) {
for (Enumeration enm = resourceLoader.findResources(name);
enm.hasMoreElements();) {
URL url = (URL) enm.nextElement();
if (isResourceVisible(name, url, requestor)) {
result.add(url);
}
}
}
if (seen == null) {
seen = new HashSet();
}
seen.add(getPluginDescriptor().getId());
for (int i = 0; i < publicImports.length; i++) {
if (seen.contains(publicImports[i].getId())) {
continue;
}
((StandardPluginClassLoader) getPluginManager().getPluginClassLoader(
publicImports[i])).findResources(result, name,
requestor, seen);
}
if (this == requestor) {
for (int i = 0; i < privateImports.length; i++) {
if (seen.contains(privateImports[i].getId())) {
continue;
}
((StandardPluginClassLoader) getPluginManager().getPluginClassLoader(
privateImports[i])).findResources(result, name,
requestor, seen);
}
for (int i = 0; i < reverseLookups.length; i++) {
if (seen.contains(reverseLookups[i].getId())) {
continue;
}
((StandardPluginClassLoader) getPluginManager().getPluginClassLoader(
reverseLookups[i])).findResources(result, name,
requestor, seen);
}
}
}
protected boolean isResourceVisible(final String name, final URL url,
final StandardPluginClassLoader requestor) {
/*log.debug("isResourceVisible(URL, PluginClassLoader): URL=" + url //$NON-NLS-1$
+ ", requestor=" + requestor); //$NON-NLS-1$*/
if (this == requestor) {
return true;
}
URL lib;
try {
String file = url.getFile();
lib = new URL(url.getProtocol(), url.getHost(),
file.substring(0, file.length() - name.length()));
} catch (MalformedURLException mue) {
log.error("can't get resource library URL", mue); //$NON-NLS-1$
return false;
}
ResourceFilter filter =
(ResourceFilter) resourceFilters.get(lib.toExternalForm());
if (filter == null) {
log.warn("no resource filter found for library " //$NON-NLS-1$
+ lib + ", name=" + name //$NON-NLS-1$
+ ", URL=" + url + ", this=" + this //$NON-NLS-1$ //$NON-NLS-2$
+ ", requestor=" + requestor); //$NON-NLS-1$
return false;
}
if (!filter.isResourceVisible(name)) {
log.warn("resource not visible, name=" + name //$NON-NLS-1$
+ ", URL=" + url + ", this=" + this //$NON-NLS-1$ //$NON-NLS-2$
+ ", requestor=" + requestor); //$NON-NLS-1$
return false;
}
return true;
}
protected static final class ResourceFilter {
private boolean isPublic;
private Set entries;
protected ResourceFilter(final Library lib) {
entries = new HashSet();
for (Iterator it = lib.getExports().iterator(); it.hasNext();) {
String exportPrefix = (String) it.next();
if ("*".equals(exportPrefix)) { //$NON-NLS-1$
isPublic = true;
entries.clear();
break;
}
if (!lib.isCodeLibrary()) {
exportPrefix = exportPrefix.replace('\\', '.')
.replace('/', '.');
if (exportPrefix.startsWith(".")) { //$NON-NLS-1$
exportPrefix = exportPrefix.substring(1);
}
}
entries.add(exportPrefix);
}
}
protected boolean isClassVisible(final String className) {
if (isPublic) {
return true;
}
if (entries.isEmpty()) {
return false;
}
if (entries.contains(className)) {
return true;
}
int p = className.lastIndexOf('.');
if (p == -1) {
return false;
}
return entries.contains(className.substring(0, p) + ".*"); //$NON-NLS-1$
}
protected boolean isResourceVisible(final String resPath) {
// quick check
if (isPublic) {
return true;
}
if (entries.isEmpty()) {
return false;
}
// translate "path spec" -> "full class name"
String str = resPath.replace('\\', '.').replace('/', '.');
if (str.startsWith(".")) { //$NON-NLS-1$
str = str.substring(1);
}
if (str.endsWith(".")) { //$NON-NLS-1$
str = str.substring(0, str.length() - 1);
}
return isClassVisible(str);
}
}
protected static class PluginResourceLoader extends URLClassLoader {
private static Log logger =
LogFactory.getLog(PluginResourceLoader.class);
static PluginResourceLoader get(final PluginManager manager,
final PluginDescriptor descr) {
final List urls = new LinkedList();
for (Iterator it = descr.getLibraries().iterator(); it.hasNext();) {
Library lib = (Library) it.next();
if (lib.isCodeLibrary()) {
continue;
}
urls.add(manager.getPathResolver().resolvePath(lib,
lib.getPath()));
}
if (logger.isDebugEnabled()) {
StringBuffer buf = new StringBuffer();
buf.append("Resource URL's populated for plug-in " + descr //$NON-NLS-1$
+ ":\r\n"); //$NON-NLS-1$
for (Iterator it = urls.iterator(); it.hasNext();) {
buf.append("\t"); //$NON-NLS-1$
buf.append(it.next());
buf.append("\r\n"); //$NON-NLS-1$
}
logger.trace(buf.toString());
}
if (urls.isEmpty()) {
return null;
}
/*return new PluginResourceLoader((URL[]) urls.toArray(
new URL[urls.size()]));*/
return (PluginResourceLoader) AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
return new PluginResourceLoader(
(URL[]) urls.toArray(new URL[urls.size()]));
}
});
}
/**
* Creates loader instance configured to load resources only from given
* URLs.
* @param urls array of resource URLs
*/
PluginResourceLoader(final URL[] urls) {
super(urls);
}
/**
* @see java.lang.ClassLoader#findClass(java.lang.String)
*/
protected Class findClass(final String name)
throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
/**
* @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
*/
protected Class loadClass(final String name, final boolean resolve)
throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -