⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 standardpluginclassloader.java

📁 java插件系统源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                result = loadClass(name, resolve, this, null);
            } catch (ClassNotFoundException cnfe) {
                result = getParent().loadClass(name);
            }
            if (result == null) {
                result = getParent().loadClass(name);
            }
        } else {
            try {
                result = getParent().loadClass(name);
            } catch (ClassNotFoundException cnfe) {
                result = loadClass(name, resolve, this, null);
            }
        }
        if (result != null) {
            return result;
        }
        throw new ClassNotFoundException(name);
    }
    
    protected Class loadClass(final String name, final boolean resolve,
            final StandardPluginClassLoader requestor, final Set seenPlugins)
            throws ClassNotFoundException {
        /*log.debug("loadClass(String, boolean, ...): name=" + name + ", this="
                + this);*/
        Set seen = seenPlugins;
        if ((seen != null) && seen.contains(getPluginDescriptor().getId())) {
            return null;
        }
        if ((this != requestor)
                && !getPluginManager().isPluginActivated(getPluginDescriptor())
                && !getPluginManager().isPluginActivating(
                        getPluginDescriptor())) {
            String msg = "can't load class " + name + ", plug-in " //$NON-NLS-1$ //$NON-NLS-2$
                + getPluginDescriptor() + " is not activated yet"; //$NON-NLS-1$
            log.warn(msg);
            throw new ClassNotFoundException(msg);
        }
        Class result = null;
        synchronized (this) {
            result = findLoadedClass(name);
            if (result != null) {
                if (log.isDebugEnabled()) {
                    log.debug("loadClass(...): found loaded class, class=" //$NON-NLS-1$
                            + result + ", this=" //$NON-NLS-1$
                            + this + ", requestor=" + requestor); //$NON-NLS-1$
                }
                checkClassVisibility(result, requestor);
                /*if (resolve) {
                    resolveClass(result);
                }*/
                return result; // found already loaded class in this plug-in
            }
            try {
                synchronized (getClass()) {
                    result = findClass(name);
                }
            } catch (LinkageError le) {
                if (log.isDebugEnabled()) {
                    log.debug("loadClass(...): class loading failed," //$NON-NLS-1$
                            + " name=" + name + ", this=" //$NON-NLS-1$ //$NON-NLS-2$
                            + this + ", requestor=" + requestor, le); //$NON-NLS-1$
                }
                throw le;
             } catch (ClassNotFoundException cnfe) {
                // ignore
            }
            if (result != null) {
                if (log.isDebugEnabled()) {
                    log.debug("loadClass(...): found class, class=" //$NON-NLS-1$
                            + result + ", this=" //$NON-NLS-1$
                            + this + ", requestor=" + requestor); //$NON-NLS-1$
                }
                checkClassVisibility(result, requestor);
                if (resolve) {
                    resolveClass(result);
                }
                return result; // found class in this plug-in
            }
        }
        if (seen == null) {
            seen = new HashSet();
        }
        if (log.isDebugEnabled()) {
            log.debug("loadClass(...): class 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])).loadClass(
                            name, resolve, requestor, seen);
            if (result != null) {
                /*if (resolve) {
                    resolveClass(result);
                }*/
                break; // found class 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])).loadClass(
                                name, resolve, requestor, seen);
                if (result != null) {
                    /*if (resolve) {
                        resolveClass(result);
                    }*/
                    break; // found class in privately imported plug-in
                }
            }
        }
        if ((this == requestor) && (result == null)) {
            for (int i = 0; i < reverseLookups.length; i++) {
                PluginDescriptor descr = reverseLookups[i];
                if (seen.contains(descr.getId())) {
                    continue;
                }
                if (!getPluginManager().isPluginActivated(descr)
                        && !getPluginManager().isPluginActivating(descr)) {
                    continue;
                }
                result = ((StandardPluginClassLoader) getPluginManager()
                        .getPluginClassLoader(descr)).loadClass(
                                name, resolve, requestor, seen);
                if (result != null) {
                    /*if (resolve) {
                        resolveClass(result);
                    }*/
                    break; // found class in plug-in that marks itself as
                           // allowed reverse look up
                }
            }
        }
        return result;
    }
    
    protected void checkClassVisibility(final Class cls,
            final StandardPluginClassLoader requestor)
            throws ClassNotFoundException {
        /*log.debug("checkClassVisibility(Class, PluginClassLoader): class=" //$NON-NLS-1$
                + cls.getName() + ", requestor=" + requestor //$NON-NLS-1$
                + ", this=" + this); //$NON-NLS-1$*/
        if (this == requestor) {
            return;
        }
        URL lib = getClassBaseUrl(cls);
        if (lib == null) {
            return; // cls is a system class
        }
        ClassLoader loader = cls.getClassLoader();
        if (!(loader instanceof StandardPluginClassLoader)) {
            return;
        }
        if (loader != this) {
            ((StandardPluginClassLoader) loader).checkClassVisibility(cls,
                    requestor);
        } else {
            ResourceFilter filter =
                (ResourceFilter) resourceFilters.get(lib.toExternalForm());
            if (filter == null) {
                log.warn("class not visible, no class filter found, lib=" + lib //$NON-NLS-1$
                        + ", class=" + cls + ", this=" + this //$NON-NLS-1$ //$NON-NLS-2$
                        + ", requestor=" + requestor); //$NON-NLS-1$
                throw new ClassNotFoundException("class " //$NON-NLS-1$
                        + cls.getName() + " is not visible for plug-in " //$NON-NLS-1$
                        + requestor.getPluginDescriptor().getId()
                        + ", no filter found for library " + lib); //$NON-NLS-1$
            }
            if (!filter.isClassVisible(cls.getName())) {
                log.warn("class not visible, lib=" + lib //$NON-NLS-1$
                        + ", class=" + cls + ", this=" + this //$NON-NLS-1$ //$NON-NLS-2$
                        + ", requestor=" + requestor); //$NON-NLS-1$
                throw new ClassNotFoundException("class " //$NON-NLS-1$
                        + cls.getName() + " is not visible for plug-in " //$NON-NLS-1$
                        + requestor.getPluginDescriptor().getId());
            }
        }
    }

    /**
     * @see java.lang.ClassLoader#findLibrary(java.lang.String)
     */
    protected String findLibrary(final String name) {
        if ((name == null) || "".equals(name.trim())) { //$NON-NLS-1$
            return null;
        }
        if (log.isDebugEnabled()) {
            log.debug("findLibrary(String): name=" + name //$NON-NLS-1$
                    + ", this=" + this); //$NON-NLS-1$
        }
        String libname = System.mapLibraryName(name);
        String result = null;
        PathResolver pathResolver = getPluginManager().getPathResolver();
        for (Iterator it = getPluginDescriptor().getLibraries().iterator();
                it.hasNext();) {
            Library lib = (Library) it.next();
            if (lib.isCodeLibrary()) {
                continue;
            }
            URL libUrl = pathResolver.resolvePath(lib, lib.getPath() + libname);
            if (log.isDebugEnabled()) {
                log.debug("findLibrary(String): trying URL " + libUrl); //$NON-NLS-1$
            }
            File libFile = IoUtil.url2file(libUrl);
            if (libFile != null) {
                if (log.isDebugEnabled()) {
                    log.debug("findLibrary(String): URL " + libUrl //$NON-NLS-1$
                            + " resolved as local file " + libFile); //$NON-NLS-1$
                }
                if (libFile.isFile()) {
                    result = libFile.getAbsolutePath();
                    break;
                }
                continue;
            }
            // we have some kind of non-local URL
            // try to copy it to local temporary file
            libFile = (File) libraryCache.get(libUrl.toExternalForm());
            if (libFile != null) {
                if (libFile.isFile()) {
                    result = libFile.getAbsolutePath();
                    break;
                }
                libraryCache.remove(libUrl.toExternalForm());
            }
            if (libraryCache.containsKey(libUrl.toExternalForm())) {
                // already tried to cache this library
                break;
            }
            libFile = cacheLibrary(libUrl, libname);
            if (libFile != null) {
                result = libFile.getAbsolutePath();
                break;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("findLibrary(String): name=" + name //$NON-NLS-1$
                    + ", libname=" + libname //$NON-NLS-1$
                    + ", result=" + result //$NON-NLS-1$
                    + ", this=" + this); //$NON-NLS-1$
        }
        return result;
    }

    protected synchronized File cacheLibrary(final URL libUrl,
            final String libname) {
        File cacheFolder = getLibCacheFolder();
        String libUrlStr = libUrl.toExternalForm();
        if (libraryCache.containsKey(libUrlStr)) {
            return (File) libraryCache.get(libUrlStr);
        }
        File result = null;
        try {
            if (cacheFolder == null) {
                throw new IOException(
                        "can't initialize libraries cache folder"); //$NON-NLS-1$
            }
            File libCachePluginFolder = new File(cacheFolder,
                    getPluginDescriptor().getUniqueId());
            if (!libCachePluginFolder.exists()
                    && !libCachePluginFolder.mkdirs()) {
                throw new IOException("can't create cache folder " //$NON-NLS-1$
                        + libCachePluginFolder);
            }
            result = new File(libCachePluginFolder, libname);
            InputStream in = IoUtil.getResourceInputStream(libUrl);
            try {
                OutputStream out = new BufferedOutputStream(
                        new FileOutputStream(result));
                try {
                    IoUtil.copyStream(in, out, 512);
                } finally {
                    out.close();
                }
            } finally {
                in.close();
            }
            libraryCache.put(libUrlStr, result);
            if (log.isDebugEnabled()) {
                log.debug("library " + libname //$NON-NLS-1$
                        + " successfully cached from URL " + libUrl //$NON-NLS-1$
                        + " and saved to local file " + result); //$NON-NLS-1$
            }
        } catch (IOException ioe) {
            log.error("can't cache library " + libname //$NON-NLS-1$
                    + " from URL " + libUrl, ioe); //$NON-NLS-1$
            libraryCache.put(libUrlStr, null);
            result = null;
        }
        return result;
    }

    /**
     * @see java.lang.ClassLoader#findResource(java.lang.String)
     */
    public URL findResource(final String name) {
        //log.debug("findResource(String): name=" + name); //$NON-NLS-1$
        URL result = findResource(name, this, null);
        //log.debug("findResource(String): result=" + result); //$NON-NLS-1$
        return result;
    }

    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -