Example usage for java.util.jar JarFile getInputStream

List of usage examples for java.util.jar JarFile getInputStream

Introduction

In this page you can find the example usage for java.util.jar JarFile getInputStream.

Prototype

public synchronized InputStream getInputStream(ZipEntry ze) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:com.icesoft.faces.webapp.parser.JspPageToDocument.java

/**
 * @param context//w  w w  .ja v  a  2  s.c  o  m
 * @param namespaceURL
 * @return InputStream
 * @throws IOException
 */
public static InputStream getTldInputStream(ExternalContext context, String namespaceURL) throws IOException {

    // InputStream in = null;
    JarFile jarFile = null;
    String[] location = null;

    namespaceURL = String.valueOf(namespaceURL);

    // "jsp" is only a placeholder for standard JSP tags that are
    // not supported, so just return null

    if ("jsp".equals(namespaceURL)) {
        return null;
    }

    if ("http://java.sun.com/JSP/Page".equals(namespaceURL)) {
        return null;
    }

    // TldLocationsCache may fail esp. with SecurityException on SUN app server
    TldLocationsCache tldCache = new TldLocationsCache(context);
    try {
        location = tldCache.getLocation(namespaceURL);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug(e.getMessage(), e);
        }
    }

    if (null == location) {
        if (namespaceURL.startsWith("/") && namespaceURL.endsWith(".tld")) {
            location = new String[] { namespaceURL };
        }
    }

    if (null == location) {
        location = scanJars(context, namespaceURL);
    }

    if (null == location) {
        //look for Sun implementation
        URL tagURL = JspPageToDocument.class.getClassLoader().getResource(SUN_TAG_CLASS);
        if (null != tagURL) {

            // Bug 876
            // Not all app servers (ie WebLogic 8.1) return
            // an actual JarURLConnection.
            URLConnection conn = tagURL.openConnection();

            //ICE-3683: Special processing for JBoss 5 micro-container with VFS
            if (tagURL.getProtocol().equals("vfszip")) {
                String tagPath = tagURL.toExternalForm();
                String jarPath = tagPath.substring(0, tagPath.indexOf(SUN_TAG_CLASS));

                String tldPath = jarPath;
                if (namespaceURL.endsWith("html")) {
                    tldPath += HTML_TLD_SUFFIX;
                } else if (namespaceURL.endsWith("core")) {
                    tldPath += CORE_TLD_SUFFIX;
                }

                URL tldURL = new URL(tldPath);
                return tldURL.openConnection().getInputStream();
            }

            if (conn instanceof JarURLConnection) {
                location = scanJar((JarURLConnection) conn, namespaceURL);
            } else {
                //OSGi-based servers (such as GlassFishv3 and WebSphere7)
                //do not provide JarURLConnection to their resources so
                //we handle the JSF TLDs as a special case.
                if (namespaceURL.endsWith("html")) {
                    location = getBundleLocation(tagURL, HTML_TLD_SUFFIX);
                } else if (namespaceURL.endsWith("core")) {
                    location = getBundleLocation(tagURL, CORE_TLD_SUFFIX);
                }
            }
        }
    }

    if (null == location) {
        try {
            // scan WebSphere dirs for JSF jars
            String separator = System.getProperty("path.separator");
            String wsDirs = System.getProperty("ws.ext.dirs");

            String[] dirs = null;
            if (null != wsDirs) {
                dirs = wsDirs.split(separator);
            } else {
                dirs = new String[] {};
            }
            Iterator theDirs = Arrays.asList(dirs).iterator();
            while (theDirs.hasNext()) {
                String dir = (String) theDirs.next();
                try {
                    location = scanJars(dir, namespaceURL);
                } catch (Exception e) {
                    //catch all possible exceptions including runtime exception
                    //so that the rest of jars still can be scanned.
                }
                if (null != location) {
                    break;
                }
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug(e.getMessage(), e);
            }
        }
    }

    if (null == location) {
        //look for MyFaces implementation
        URL tagURL = JspPageToDocument.class.getClassLoader().getResource(MYFACES_TAG_CLASS);

        if (null != tagURL) {
            URLConnection conn = tagURL.openConnection();

            if (conn instanceof JarURLConnection) {
                location = scanJar((JarURLConnection) conn, namespaceURL);
            } else {
                //OSGi-based servers (such as GlassFishv3 and WebSphere7)
                //do not provide JarURLConnection to their resources so
                //we handle the JSF TLDs as a special case.
                if (namespaceURL.endsWith("html")) {
                    location = getBundleLocation(tagURL, HTML_TLD_SUFFIX);
                } else if (namespaceURL.endsWith("core")) {
                    location = getBundleLocation(tagURL, CORE_TLD_SUFFIX);
                }
            }
        }
    }

    if (null == location) {
        String msg = "Can't find TLD for location [" + namespaceURL
                + "]. JAR containing the TLD may not be in the classpath";
        log.error(msg);
        return null;
    } else {
        if (log.isTraceEnabled()) {
            for (int i = 0; i < location.length; i++) {
                log.trace("Found TLD location for " + namespaceURL + " = " + location[i]);
            }
        }
    }

    if (!location[0].endsWith("jar")) {
        InputStream tldStream = context.getResourceAsStream(location[0]);
        if (null == tldStream) {
            tldStream = (new URL(location[0])).openConnection().getInputStream();
        }
        return tldStream;
    } else {
        // Tag library is packaged in JAR file
        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]);
        return jarFile.getInputStream(jarEntry);
    }

}