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

📄 moduleclassloader.java

📁 OSGI 的 源码实现,采用JAVA书写
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    // not be holding any locks before making the call.                    URL url = m_mgr.getURLPolicy().createCodeSourceURL(                        m_mgr, m_module);                    // If we have a valid code source URL, then use it to                    // define the class for security purposes, otherwise                    // define the class without a code source.                    if (url != null)                    {                        CodeSource cs = new CodeSource(url, (Certificate[]) null);                        clazz = defineClass(name, bytes, 0, bytes.length, cs);                    }                    else                    {                        clazz = defineClass(name, bytes, 0, bytes.length);                    }                }            }        }        if (clazz != null)        {            return clazz;        }        throw new ClassNotFoundException(name);    }    /**     * <p>     * This method is used by <tt>SearchPolicy</tt> instances when they want     * to load a class from a module. The search policy is initially invoked when     * <tt>ModuleClassLoader.loadClass()</tt> delegates a class loading     * request to it. In general, the ultimate goal of the search policy is to     * return a class from another module if possible. Unfortunately, if a search     * policy tries to directly load a class from another module's class loader, an     * infinite loop will result because the module's class loader will delegate the     * request back to the search policy. To avoid this situation, search policies     * must use this method when trying to load a class from a module.     * </p>     * @param name the name of the class to load.     * @return the loaded class or <tt>null</tt>.    **/    public Class searchForClass(String name)    {        try        {            return findClass(name);        } catch (Throwable th) {            // Not much we can do.// TODO: Do something with this error message.//            System.err.println("ModuleClassLoader: " + th.getMessage());        }        return null;    }    /**     * <p>     * This method is nearly an exact copy of the ClassLoader.getResource()     * method. The main difference is that it delegates to its associated     * <tt>ModuleManager</tt>'s search policy before calling the     * <tt>ClassLoader.findResource()</tt> method.     * </p>     * @param name the class to be loaded.     * @return a URL to the resource or <tt>null</tt> if the resource was not found.    **/    public URL getResource(String name)    {        URL url = null;        // First try the parent class loader.        if (getParent() != null)        {            url = getParent().getResource(name);        }//        else//        {//            url = getBootstrapResource(name);//        }        // Then try the search policy.        if (url == null)        {            // Try the search policy if no resource is            // found in the parent or bootstrap loader.            if (m_mgr.getSearchPolicy() != null)            {                try                {                    url = m_mgr.getSearchPolicy().findResource(m_module, name);                }                catch (ResourceNotFoundException ex)                {                    // We return null here because if SearchPolicy.findResource()                    // throws an exception we interpret that to mean that the                    // search should be stopped.                    return null;                }            }        }        // Finally, look locally.        if (url == null)        {            url = findResource(name);        }        return url;    }    /**     * <p>     * This method overriden from from <tt>ClassLoader</tt>.     * It is implemented such that it loads resources from the set of     * <tt>ResourceSource</tt>s from its associated module.     * </p>     * @param name the name of the resource to load.     * @return the <tt>URL</tt> associated with the resource or <tt>null</tt>.    **/    protected URL findResource(String name)    {        // Remove leading slash, if present.        if (name.startsWith("/"))        {            name = name.substring(1);        }        URL url = null;        // Try to load the resource from the module's resource        // sources.        if (url == null)        {            ResourceSource[] sources = m_module.getResourceSources();            for (int i = 0;                (url == null) && (sources != null) && (i < sources.length);                i++)            {                if (sources[i].hasResource(name))                {                    url = m_mgr.getURLPolicy().createResourceURL(m_mgr, m_module, i, name);                }            }        }        return url;    }    /**     * <p>     * This method is used by <tt>SearchPolicy</tt> instances when they want     * to load a resource from a module. The search policy is initially invoked when     * <tt>ModuleClassLoader.loadClass()</tt> delegates a resource loading     * request to it. In general, the ultimate goal of the search policy is to     * return a resource from another module if possible. Unfortunately, if a search     * policy tries to directly load a resource from another module's class loader, an     * infinite loop will result because the module's class loader will delegate the     * request back to the search policy. To avoid this situation, search policies     * must use this method when trying to load a resource from a module.     * </p>     * @param name the name of the resource to load.     * @return a URL to the resource or <tt>null</tt>.    **/    public URL searchForResource(String name)    {        try        {            URL url = findResource(name);            return url;        }        catch (Throwable th)        {            // Ignore and just return null.        }        return null;    }    protected Enumeration findResources(String name)    {        Vector v = new Vector();        // Remove leading slash, if present.        if (name.startsWith("/"))        {            name = name.substring(1);        }        // Try to load the resource from the module's resource        // sources.        ResourceSource[] sources = m_module.getResourceSources();        for (int i = 0; (sources != null) && (i < sources.length); i++)        {            if (sources[i].hasResource(name))            {                v.addElement(m_mgr.getURLPolicy().createResourceURL(m_mgr, m_module, i, name));            }        }        return v.elements();    }    /**     * <p>     * This method overriden from from <tt>ClassLoader</tt>. It maps a library     * name to a library path by consulting the <tt>LibrarySource</tt>s of the     * class loader's module.     * </p>     * @param name the name of the library to find.     * @return the file system path of library or <tt>null</tt>    **/    protected String findLibrary(String name)    {        // Remove leading slash, if present.        if (name.startsWith("/"))        {            name = name.substring(1);        }        LibrarySource[] sources = m_module.getLibrarySources();        for (int i = 0;            (sources != null) && (i < sources.length);            i++)        {            String path = sources[i].getPath(name);            if (path != null)            {                return path;            }        }        return null;    }}

⌨️ 快捷键说明

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