Example usage for java.net JarURLConnection connect

List of usage examples for java.net JarURLConnection connect

Introduction

In this page you can find the example usage for java.net JarURLConnection connect.

Prototype

public abstract void connect() throws IOException;

Source Link

Document

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

Usage

From source file:org.apache.jasper.compiler.TagLibraryInfoImpl.java

/**
 * Constructor.//w  w  w .j  av a2s.c  o m
 */
public TagLibraryInfoImpl(JspCompilationContext ctxt, ParserController pc, String prefix, String uriIn,
        String[] location, ErrorDispatcher err) throws JasperException {
    super(prefix, uriIn);

    this.ctxt = ctxt;
    this.parserController = pc;
    this.err = err;
    InputStream in = null;
    JarFile jarFile = null;

    if (location == null) {
        // The URI points to the TLD itself or to a JAR file in which the
        // TLD is stored
        location = generateTLDLocation(uri, ctxt);
    }

    try {
        if (!location[0].endsWith("jar")) {
            // Location points to TLD file
            try {
                in = getResourceAsStream(location[0]);
                if (in == null) {
                    throw new FileNotFoundException(location[0]);
                }
            } catch (FileNotFoundException ex) {
                err.jspError("jsp.error.file.not.found", location[0]);
            }

            parseTLD(ctxt, location[0], in, null);
            // Add TLD to dependency list
            PageInfo pageInfo = ctxt.createCompiler().getPageInfo();
            if (pageInfo != null) {
                pageInfo.addDependant(location[0]);
            }
        } else {
            // Tag library is packaged in JAR file
            try {
                URL jarFileUrl = new URL("jar:" + location[0] + "!/");
                JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
                conn.setUseCaches(false);
                conn.connect();
                jarFile = conn.getJarFile();
                ZipEntry jarEntry = jarFile.getEntry(location[1]);
                in = jarFile.getInputStream(jarEntry);
                parseTLD(ctxt, location[0], in, jarFileUrl);
            } catch (Exception ex) {
                err.jspError("jsp.error.tld.unable_to_read", location[0], location[1], ex.toString());
            }
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Throwable t) {
            }
        }
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Throwable t) {
            }
        }
    }

}

From source file:org.tinygroup.jspengine.compiler.TagLibraryInfoImpl.java

/**
 * Constructor which builds a TagLibraryInfoImpl by parsing a TLD.
 *//* w  w  w  . ja  v a2  s . c om*/
public TagLibraryInfoImpl(JspCompilationContext ctxt, ParserController pc, String prefix, String uriIn,
        String[] location, ErrorDispatcher err) throws JasperException {
    super(prefix, uriIn);

    this.ctxt = ctxt;
    this.parserController = pc;
    this.pageInfo = pc.getCompiler().getPageInfo();
    this.err = err;
    InputStream in = null;
    JarFile jarFile = null;

    if (location == null) {
        // The URI points to the TLD itself or to a JAR file in which the
        // TLD is stored
        location = generateTLDLocation(uri, ctxt);
    }

    try {
        if (!location[0].endsWith("jar")) {
            // Location points to TLD file
            try {
                in = getResourceAsStream(location[0]);
                if (in == null) {
                    throw new FileNotFoundException(location[0]);
                }
            } catch (FileNotFoundException ex) {
                err.jspError("jsp.error.file.not.found", location[0]);
            }
            parseTLD(ctxt, location[0], in, null);
            // Add TLD to dependency list
            PageInfo pageInfo = ctxt.createCompiler(false).getPageInfo();
            if (pageInfo != null) {
                pageInfo.addDependant(location[0]);
            }
        } else {
            // Tag library is packaged in JAR file
            try {
                URL jarFileUrl = new URL("jar:" + location[0] + "!/");
                JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
                conn.setUseCaches(false);
                conn.connect();
                jarFile = conn.getJarFile();
                ZipEntry jarEntry = jarFile.getEntry(location[1]);
                in = jarFile.getInputStream(jarEntry);
                parseTLD(ctxt, location[0], in, jarFileUrl);
            } catch (Exception ex) {
                err.jspError("jsp.error.tld.unable_to_read", location[0], location[1], ex.toString());
            }
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Throwable t) {
            }
        }
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Throwable t) {
            }
        }
    }

}