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

📄 moduleclassloader.java

📁 精通tomcat书籍原代码,希望大家共同学习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        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 {
        return findResources2(name, true);
    }
    
    Enumeration findResources2(String name, boolean del2repo) throws IOException {
        if( del2repo ) {
            return ((RepositoryClassLoader)repository.getClassLoader()).findResources(name);
        } else {
            return super.findResources(name);
        }
    }

    // Next methods implement the search alghoritm - parent, repo, delegation, etc 

    /** getResource() - modified to implement the search alghoritm 
     * 
     */
    public URL getResource(String name) {
        return getResource2( name, null, true);
    }

    /** getResource() - same thing, but don't delegate to repo if called 
     * from repo 
     * 
     */
    URL getResource2(String name, ClassLoader originator, boolean delegate2repo ) {

        URL url = null;

        // (1) Delegate to parent if requested
        if (delegate) {
            url=getResourceParentDelegate(name);
            if(url!=null ) return url;
        }

        // (2) Search local repositories
        url = findResource(name);
        if (url != null) {
            // TODO: antijar locking - WebappClassLoader is making a copy ( is it ??)
            if (DEBUG)
                log("getResource() found locally " + delegate + " " + name + " " + url);
            return (url);
        }

        // Finally, try the group loaders ( via super() in StandardClassLoader ).
        // not found using normal loading mechanism. load from one of the classes in the group
        if( delegate2repo && repository!=null ) {
            url=repository.findResource(this, name);
            if(url!=null ) {
                if( DEBUG )
                    log("getResource() FOUND from group " + repository.getName() + " " + name + " " + url);
                return url;
            }
        }

        // (3) Delegate to parent unconditionally if not already attempted
        if( !delegate ) {
            url=getResourceParentDelegate(name);
            if(url!=null ) return url;
        }

        
        // (4) Resource was not found
        if (DEBUGNF)
            log("getResource() NOTFOUND  " + delegate + " " + name + " " + url);
        return (null);

    }

    
    // to avoid duplication - get resource from parent, when delegating
    private URL getResourceParentDelegate(String name) {
        URL url=null;
        ClassLoader loader = getParent();
        
        if (loader == null) {
            loader = getSystemClassLoader();
            if (url != null) {
                if (DEBUG)
                    log("getResource() found by system " +  delegate + " " + name + " " + url);
                return (url);
            }
        } else {
            url = loader.getResource(name);
            if (url != null) {
                if (DEBUG)
                    log("getResource() found by parent " +  delegate + " " + name + " " + url);
                return (url);
            }
        }
        if( DEBUG ) log("getResource not found by parent " + loader);

        return url;
    }
    
    /**
     * Load the class with the specified name, searching using the following
     * algorithm until it finds and returns the class.  If the class cannot
     * be found, returns <code>ClassNotFoundException</code>.
     * <ul>
     * <li>Call <code>findLoadedClass(String)</code> to check if the
     *     class has already been loaded.  If it has, the same
     *     <code>Class</code> object is returned.</li>
     * <li>If the <code>delegate</code> property is set to <code>true</code>,
     *     call the <code>loadClass()</code> method of the parent class
     *     loader, if any.</li>
     * <li>Call <code>findClass()</code> to find this class in our locally
     *     defined repositories.</li>
     * <li>Call the <code>loadClass()</code> method of our parent
     *     class loader, if any.</li>
     * </ul>
     * If the class was found using the above steps, and the
     * <code>resolve</code> flag is <code>true</code>, this method will then
     * call <code>resolveClass(Class)</code> on the resulting Class object.
     *
     * @param name Name of the class to be loaded
     * @param resolve If <code>true</code> then resolve the class
     *
     * @exception ClassNotFoundException if the class was not found
     */
    public Class loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
        return loadClass2( name, resolve, true );
    }
    
    public Class loadClass2(String name, boolean resolve, boolean del2repo)
        throws ClassNotFoundException
    {

        Class clazz = null;

        // Don't load classes if class loader is stopped
        if (!started) {
            log("Not started " + this + " " + module);
            //throw new ThreadDeath();
            start();
        }

        // (0) Check our previously loaded local class cache
        clazz = findLoadedClass(name);
        if (clazz != null) {
            if (DEBUG)
                log("loadClass() FOUND findLoadedClass " + name + " , " + resolve);
            if (resolve) resolveClass(clazz);
            return (clazz);
        }

        // (0.2) Try loading the class with the system class loader, to prevent
        //       the webapp from overriding J2SE classes
        try {
            clazz = getSystemClassLoader().loadClass(name);
            if (clazz != null) {
                // enabling this can result in ClassCircularityException
//                if (DEBUG)
//                    log("loadClass() FOUND system " + name + " , " + resolve);
                if (resolve) resolveClass(clazz);
                return (clazz);
            }
        } catch (ClassNotFoundException e) {
            // Ignore
        }

        // TODO: delegate based on filter
        boolean delegateLoad = delegate;// || filter(name);

        // (1) Delegate to our parent if requested
        if (delegateLoad) {

            ClassLoader loader = getParent();
            if( loader != null ) {
                try {
                    clazz = loader.loadClass(name);
                    if (clazz != null) {
                        if (DEBUG)
                            log("loadClass() FOUND by parent " + delegate + " " + name + " , " + resolve);
                        if (resolve)
                            resolveClass(clazz);
                        return (clazz);
                    }
                } catch (ClassNotFoundException e) {
                    ;
                }
            }
        }

        // (2) Search local repositories
        try {
            clazz = findClass(name);
            if (clazz != null) {
                if (DEBUG)
                    log("loadClass - FOUND findClass " + delegate + " " + name + " , " + resolve);
                if (resolve) resolveClass(clazz);
                return (clazz);
            }
        } catch (ClassNotFoundException e) {
            ;
        }

        // Finally, try the group loaders ( via super() in StandardClassLoader ).
        // not found using normal loading mechanism. load from one of the classes in the group
        if( del2repo && repository!=null ) {
            Class cls=repository.findClass(this, name);
            if(cls!=null ) {
                if( DEBUG )
                    log("loadClass(): FOUND from group " + repository.getName() + " " + name);
                if (resolve) resolveClass(clazz);
                return cls;
            }
        }

        // (3) Delegate to parent unconditionally
        if (!delegateLoad) {
            ClassLoader loader = getParent();
            if( loader != null ) {
                try {
                    clazz = loader.loadClass(name);
                    if (clazz != null) {
                        if (DEBUG)
                            log("loadClass() FOUND parent " + delegate + " " + name + " , " + resolve);
                        if (resolve) resolveClass(clazz);
                        return (clazz);
                    }
                } catch (ClassNotFoundException e) {
                    ;
                }
            }
        }

        if( DEBUGNF ) log("loadClass(): NOTFOUND " + name + " xxx " + getParent() +  " " + repository.getName() );
        throw new ClassNotFoundException(name);
    }


    // ------------------------------------------------------ Lifecycle Methods



    /**
     * Start the class loader.
     *
     * @exception LifecycleException if a lifecycle error occurs
     */
    void start()  {

        started = true;

    }

    /** Support for "disabled" state.
    *
    * @return
    */
    boolean isStarted() {
        return started;
    }


    /**
     * Stop the class loader.
     *
     * @exception LifecycleException if a lifecycle error occurs
     */
    void stop() {

        started = false;

    }




    /**
     * Validate a classname. As per SRV.9.7.2, we must restict loading of 
     * classes from J2SE (java.*) and classes of the servlet API 
     * (javax.servlet.*). That should enhance robustness and prevent a number
     * of user error (where an older version of servlet.jar would be present
     * in /WEB-INF/lib).
     * 
     * @param name class name
     * @return true if the name is valid
     */
    protected boolean validate(String name) {

        if (name == null)
            return false;
        if (name.startsWith("java."))
            return false;

        return true;

    }


    // ------------------ Local methods ------------------------

    private void log(String s ) {
        System.err.println("ModuleClassLoader: " + s);
    }
    private void log(String s, Throwable t ) {
        System.err.println("ModuleClassLoader: " + s);
        t.printStackTrace();
    }
    
    Object debugObj=new Object();

    /**
     * Render a String representation of this object.
     */
    public String toString() {

        StringBuffer sb = new StringBuffer("ModuleCL ");
        sb.append(debugObj).append(" delegate: ");
        sb.append(delegate);
        //sb.append("\r\n");
        sb.append(" cp: ");
        URL cp[]=super.getURLs();
        if (cp != null ) {
            for (int i = 0; i <cp.length; i++) {
                sb.append("  ");
                sb.append(cp[i].getFile());
            }
        }
        if (getParent() != null) {
            sb.append("\r\n----------> Parent: ");
            sb.append(getParent().toString());
            sb.append("\r\n");
        }
        return (sb.toString());
    }
}

⌨️ 快捷键说明

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