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

📄 antclassloader.java

📁 java ant的源码!非常值得看的源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        }    }    /**     * Get the manifest from the given jar, if it is indeed a jar and it has a     * manifest     *     * @param container the File from which a manifest is required.     *     * @return the jar's manifest or null is the container is not a jar or it     *         has no manifest.     *     * @exception IOException if the manifest cannot be read.     */    private Manifest getJarManifest(File container) throws IOException {        if (container.isDirectory()) {            return null;        }        JarFile jarFile = null;        try {            jarFile = new JarFile(container);            return jarFile.getManifest();        } finally {            if (jarFile != null) {                jarFile.close();            }        }    }    /**     * Define the package information when the class comes from a     * jar with a manifest     *     * @param container the jar file containing the manifest     * @param packageName the name of the package being defined.     * @param manifest the jar's manifest     */    protected void definePackage(File container, String packageName,                                 Manifest manifest) {        String sectionName = packageName.replace('.', '/') + "/";        String specificationTitle = null;        String specificationVendor = null;        String specificationVersion = null;        String implementationTitle = null;        String implementationVendor = null;        String implementationVersion = null;        String sealedString = null;        URL sealBase = null;        Attributes sectionAttributes = manifest.getAttributes(sectionName);        if (sectionAttributes != null) {            specificationTitle                = sectionAttributes.getValue(Name.SPECIFICATION_TITLE);            specificationVendor                = sectionAttributes.getValue(Name.SPECIFICATION_VENDOR);            specificationVersion                = sectionAttributes.getValue(Name.SPECIFICATION_VERSION);            implementationTitle                = sectionAttributes.getValue(Name.IMPLEMENTATION_TITLE);            implementationVendor                = sectionAttributes.getValue(Name.IMPLEMENTATION_VENDOR);            implementationVersion                = sectionAttributes.getValue(Name.IMPLEMENTATION_VERSION);            sealedString                = sectionAttributes.getValue(Name.SEALED);        }        Attributes mainAttributes = manifest.getMainAttributes();        if (mainAttributes != null) {            if (specificationTitle == null) {                specificationTitle                    = mainAttributes.getValue(Name.SPECIFICATION_TITLE);            }            if (specificationVendor == null) {                specificationVendor                    = mainAttributes.getValue(Name.SPECIFICATION_VENDOR);            }            if (specificationVersion == null) {                specificationVersion                    = mainAttributes.getValue(Name.SPECIFICATION_VERSION);            }            if (implementationTitle == null) {                implementationTitle                    = mainAttributes.getValue(Name.IMPLEMENTATION_TITLE);            }            if (implementationVendor == null) {                implementationVendor                    = mainAttributes.getValue(Name.IMPLEMENTATION_VENDOR);            }            if (implementationVersion == null) {                implementationVersion                    = mainAttributes.getValue(Name.IMPLEMENTATION_VERSION);            }            if (sealedString == null) {                sealedString                    = mainAttributes.getValue(Name.SEALED);            }        }        if (sealedString != null && sealedString.equalsIgnoreCase("true")) {            try {                sealBase = new URL(FileUtils.getFileUtils().toURI(container.getAbsolutePath()));            } catch (MalformedURLException e) {                // ignore            }        }        definePackage(packageName, specificationTitle, specificationVersion,                      specificationVendor, implementationTitle,                      implementationVersion, implementationVendor, sealBase);    }    /**     * Reads a class definition from a stream.     *     * @param stream The stream from which the class is to be read.     *               Must not be <code>null</code>.     * @param classname The name of the class in the stream.     *                  Must not be <code>null</code>.     * @param container the file or directory containing the class.     *     * @return the Class object read from the stream.     *     * @exception IOException if there is a problem reading the class from the     * stream.     * @exception SecurityException if there is a security problem while     * reading the class from the stream.     */    private Class getClassFromStream(InputStream stream, String classname,                                     File container)        throws IOException, SecurityException {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        int bytesRead = -1;        byte[] buffer = new byte[BUFFER_SIZE];        while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) {            baos.write(buffer, 0, bytesRead);        }        byte[] classData = baos.toByteArray();        return defineClassFromData(container, classData, classname);    }    /**     * Searches for and load a class on the classpath of this class loader.     *     * @param name The name of the class to be loaded. Must not be     *             <code>null</code>.     *     * @return the required Class object     *     * @exception ClassNotFoundException if the requested class does not exist     *                                   on this loader's classpath.     */    public Class findClass(String name) throws ClassNotFoundException {        log("Finding class " + name, Project.MSG_DEBUG);        return findClassInComponents(name);    }    /**     * Indicate if the given file is in this loader's path     *     * @param component the file which is to be checked     *     * @return true if the file is in the class path     */    protected boolean isInPath(File component) {        for (Enumeration e = pathComponents.elements(); e.hasMoreElements();) {            File pathComponent = (File) e.nextElement();            if (pathComponent.equals(component)) {                return true;            }        }        return false;    }    /**     * Finds a class on the given classpath.     *     * @param name The name of the class to be loaded. Must not be     *             <code>null</code>.     *     * @return the required Class object     *     * @exception ClassNotFoundException if the requested class does not exist     * on this loader's classpath.     */    private Class findClassInComponents(String name)        throws ClassNotFoundException {        // we need to search the components of the path to see if        // we can find the class we want.        InputStream stream = null;        String classFilename = getClassFilename(name);        try {            Enumeration e = pathComponents.elements();            while (e.hasMoreElements()) {                File pathComponent = (File) e.nextElement();                try {                    stream = getResourceStream(pathComponent, classFilename);                    if (stream != null) {                        log("Loaded from " + pathComponent + " "                            + classFilename, Project.MSG_DEBUG);                        return getClassFromStream(stream, name, pathComponent);                    }                } catch (SecurityException se) {                    throw se;                } catch (IOException ioe) {                    // ioe.printStackTrace();                    log("Exception reading component " + pathComponent                        + " (reason: " + ioe.getMessage() + ")",                        Project.MSG_VERBOSE);                }            }            throw new ClassNotFoundException(name);        } finally {            try {                if (stream != null) {                    stream.close();                }            } catch (IOException e) {                //ignore            }        }    }    /**     * Finds a system class (which should be loaded from the same classloader     * as the Ant core).     *     * For JDK 1.1 compatibility, this uses the findSystemClass method if     * no parent classloader has been specified.     *     * @param name The name of the class to be loaded.     *             Must not be <code>null</code>.     *     * @return the required Class object     *     * @exception ClassNotFoundException if the requested class does not exist     * on this loader's classpath.     */    private Class findBaseClass(String name) throws ClassNotFoundException {        if (parent == null) {            return findSystemClass(name);        } else {            return parent.loadClass(name);        }    }    /**     * Cleans up any resources held by this classloader. Any open archive     * files are closed.     */    public synchronized void cleanup() {        for (Enumeration e = zipFiles.elements(); e.hasMoreElements();) {            ZipFile zipFile = (ZipFile) e.nextElement();            try {                zipFile.close();            } catch (IOException ioe) {                // ignore            }        }        zipFiles = new Hashtable();        if (project != null) {            project.removeBuildListener(this);        }        project = null;    }    /**     * Empty implementation to satisfy the BuildListener interface.     *     * @param event the buildStarted event     */    public void buildStarted(BuildEvent event) {        // Not significant for the class loader.    }    /**     * Cleans up any resources held by this classloader at the end     * of a build.     *     * @param event the buildFinished event     */    public void buildFinished(BuildEvent event) {        cleanup();    }    /**     * Cleans up any resources held by this classloader at the end of     * a subbuild if it has been created for the subbuild's project     * instance.     *     * @param event the buildFinished event     *     * @since Ant 1.6.2     */    public void subBuildFinished(BuildEvent event) {        if (event.getProject() == project) {            cleanup();        }    }    /**     * Empty implementation to satisfy the BuildListener interface.     *     * @param event the buildStarted event     *     * @since Ant 1.6.2     */    public void subBuildStarted(BuildEvent event) {        // Not significant for the class loader.    }    /**     * Empty implementation to satisfy the BuildListener interface.     *     * @param event the targetStarted event     */    public void targetStarted(BuildEvent event) {        // Not significant for the class loader.    }    /**     * Empty implementation to satisfy the BuildListener interface.     *     * @param event the targetFinished event     */    public void targetFinished(BuildEvent event) {        // Not significant for the class loader.    }    /**     * Empty implementation to satisfy the BuildListener interface.     *     * @param event the taskStarted event     */    public void taskStarted(BuildEvent event) {        // Not significant for the class loader.    }    /**     * Empty implementation to satisfy the BuildListener interface.     *     * @param event the taskFinished event     */    public void taskFinished(BuildEvent event) {        // Not significant for the class loader.    }    /**     * Empty implementation to satisfy the BuildListener interface.     *     * @param event the messageLogged event     */    public void messageLogged(BuildEvent event) {        // Not significant for the class loader.    }    /**     * add any libraries that come with different java versions     * here     */    public void addJavaLibraries() {        Vector packages = JavaEnvUtils.getJrePackages();        Enumeration e = packages.elements();        while (e.hasMoreElements()) {            String packageName = (String) e.nextElement();            addSystemPackageRoot(packageName);        }    }    /**     * Returns a <code>String</code> representing this loader.     * @return the path that this classloader has.     */    public String toString() {        return "AntClassLoader[" + getClasspath() + "]";    }}

⌨️ 快捷键说明

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