Example usage for java.net JarURLConnection getJarFile

List of usage examples for java.net JarURLConnection getJarFile

Introduction

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

Prototype

public abstract JarFile getJarFile() throws IOException;

Source Link

Document

Return the JAR file for this connection.

Usage

From source file:org.ops4j.pax.url.assembly.internal.ResourceAssembly.java

private void scan(final Source source, final MergePolicy policy) throws IOException {
    // try out an url
    LOGGER.trace("Searching for [" + source + "]");
    final String path = source.path();
    URL url = null;// w  w  w .j  a v a 2 s.  co m
    try {
        url = new URL(path);
    } catch (MalformedURLException ignore) {
        // ignore this as the spec may be resolved other way
        LOGGER.trace(String.format("Path [%s] is not a valid url. Reason: %s. Continue discovery...", path,
                ignore.getMessage()));
    }
    File file = null;
    if (url != null && "file".equals(url.getProtocol()))
    // if we have an url and it's a file url
    {
        try {
            final URI uri = new URI(url.toExternalForm().replaceAll(" ", "%20"));
            file = new File(uri);
        } catch (URISyntaxException ignore) {
            // ignore this as the spec may be resolved other way
            LOGGER.trace(String.format("Path [%s] is not a valid url. Reason: %s. Continue discovery...", path,
                    ignore.getMessage()));
        }
    } else
    // if we don't have an url then let's try out a direct file
    {
        file = new File(path);
    }
    if (file != null && file.exists())
    // if we have a directory
    {
        if (file.isDirectory()) {
            list(file, new DirectoryLister(file, source.includes(), source.excludes()), policy);
            return;
        } else {
            LOGGER.trace(String.format("Path [%s] is not a valid directory. Continue discovery...", path));
        }
    } else {
        LOGGER.trace(String.format("Path [%s] is not a valid file. Continue discovery...", path));
    }
    // on this point we may have a zip
    try {
        ZipFile zip = null;
        URL baseUrl = null;
        if (file != null && file.exists())
        // try out a zip from the file we have
        {
            zip = new ZipFile(file);
            baseUrl = file.toURL();
        } else if (url != null) {
            zip = new ZipFile(url.toExternalForm());
            baseUrl = url;
        }
        if (zip != null && baseUrl != null) {
            list(new ZipLister(baseUrl, zip.entries(), source.includes(), source.excludes()), policy);
            return;
        }
    } catch (IOException ignore) {
        // ignore for the moment
        LOGGER.trace(String.format("Path [%s] is not a valid zip. Reason: %s. Continue discovery...", path,
                ignore.getMessage()));
    }
    // finally try with a zip protocol
    if (url != null && !url.toExternalForm().startsWith("jar")) {
        try {
            final URL jarUrl = new URL("jar:" + url.toURI().toASCIIString() + "!/");
            final JarURLConnection jar = (JarURLConnection) jarUrl.openConnection();
            list(new ZipLister(url, jar.getJarFile().entries(), source.includes(), source.excludes()), policy);
            return;
        } catch (IOException ignore) {
            LOGGER.trace(String.format("Path [%s] is not a valid jar. Reason: %s", path, ignore.getMessage()));
        } catch (URISyntaxException ignore) {
            LOGGER.trace(String.format("Path [%s] is not a valid jar. Reason: %s", path, ignore.getMessage()));
        }
    }
    // if we got to this point then we cannot go further
    LOGGER.trace(String.format("Source [%s] cannot be used. Stopping.", source));
    throw new HierarchicalIOException(String.format("Source [%s] cannot be used. Stopping.", source));
}

From source file:net.ontopia.utils.ResourcesDirectoryReader.java

private void findResourcesFromJar(URL jarPath) {
    try {//from  w ww  .j  a  va 2 s  .  c  o m
        JarURLConnection jarConnection = (JarURLConnection) jarPath.openConnection();
        JarFile jarFile = jarConnection.getJarFile();
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String resourcePath = entry.getName();
            if ((!entry.isDirectory()) && (resourcePath.startsWith(directoryPath))
                    && (searchSubdirectories || !resourcePath.substring(directoryPath.length()).contains("/"))
                    && (filtersApply(resourcePath))) {
                // cannot do new URL(jarPath, resourcePath), somehow leads to duplicated path
                // retest on java 8
                Enumeration<URL> urls = classLoader.getResources(resourcePath);
                while (urls.hasMoreElements()) {
                    URL url = urls.nextElement();
                    if (url.toExternalForm().startsWith(jarPath.toExternalForm())) {
                        resources.add(url);
                    }
                }
            }
        }
    } catch (IOException e) {
    }
}

From source file:org.wymiwyg.commons.util.dirbrowser.ZipPathNode.java

/**
 * @param connection/*from  w  w  w.  ja v a 2  s  .c  o m*/
 */
public ZipPathNode(JarURLConnection connection) throws IOException {
    entryName = connection.getJarEntry().getName();
    file = connection.getJarFile();
}

From source file:com.github.ithildir.liferay.mobile.go.GoSDKBuilder.java

protected void copyJarResource(JarURLConnection jarConnection, File destinationDir) throws IOException {

    String jarConnectionEntryName = jarConnection.getEntryName();
    JarFile jarFile = jarConnection.getJarFile();

    Enumeration<JarEntry> enu = jarFile.entries();

    while (enu.hasMoreElements()) {
        JarEntry jarEntry = enu.nextElement();
        String jarEntryName = jarEntry.getName();

        if (jarEntryName.startsWith(jarConnectionEntryName)) {
            String fileName = jarEntryName;

            if (fileName.startsWith(jarConnectionEntryName)) {
                fileName = fileName.substring(jarConnectionEntryName.length());
            }// w  ww  . j  av  a 2  s  .c o  m

            File file = new File(destinationDir, fileName);

            if (jarEntry.isDirectory()) {
                file.mkdirs();
            } else {
                InputStream is = null;

                try {
                    is = jarFile.getInputStream(jarEntry);

                    FileUtils.copyInputStreamToFile(is, file);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        }
    }
}

From source file:net.sf.freecol.FreeCol.java

/**
 * Get a stream for the default splash file.
 *
 * Note: Not bothering to check for nulls as this is called in try
 * block that ignores all exceptions.//from   w w  w .j av a2  s .  co  m
 *
 * @param juc The <code>JarURLConnection</code> to extract from.
 * @return A suitable <code>InputStream</code>, or null on error.
 */
private static InputStream getDefaultSplashStream(JarURLConnection juc) throws IOException {
    JarFile jf = juc.getJarFile();
    ZipEntry ze = jf.getEntry(SPLASH_DEFAULT);
    return jf.getInputStream(ze);
}

From source file:io.fabric8.insight.log.support.LogQuerySupport.java

protected String jarIndex(URL url) throws IOException {
    StringBuilder buffer = new StringBuilder();
    JarURLConnection uc = (JarURLConnection) url.openConnection();
    return jarIndex(uc.getJarFile());
}

From source file:org.mrgeo.utils.DependencyLoader.java

private static Set<Dependency> loadDependenciesFromJar(final String jarname) throws IOException {
    try {/* ww w .  j  av a  2s . com*/
        String jar = jarname;

        URL url;
        JarURLConnection conn;

        try {
            url = new URL(jar);
            conn = (JarURLConnection) url.openConnection();
        } catch (MalformedURLException e) {
            jar = (new File(jar)).toURI().toString();
            if (!jar.startsWith("jar:")) {
                jar = "jar:" + jar;
            }

            if (!jar.contains("!/")) {
                jar += "!/";
            }

            url = new URL(jar);
            conn = (JarURLConnection) url.openConnection();
        }

        log.debug("Looking in " + jar + " for dependency file");

        JarFile jf = conn.getJarFile();

        for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {
            JarEntry je = i.nextElement();
            String name = je.getName();
            if (name.endsWith("dependencies.properties")) {
                log.debug("Found dependency for " + jar + " -> " + name);
                URL resource = new URL(jar + name);
                InputStream is = resource.openStream();

                Set<Dependency> deps = readDependencies(is);

                is.close();
                return deps;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new IOException("Error Loading dependency properties file", e);
    }

    throw new IOException("No dependency properties file found in " + jarname);
}

From source file:com.stacksync.desktop.Environment.java

public void copyResourcesFromJar(JarURLConnection jarConnection, File destDir) {

    try {/*w  w  w  . ja  va 2  s.com*/
        JarFile jarFile = jarConnection.getJarFile();

        /**
         * Iterate all entries in the jar file.
         */
        for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {

            JarEntry jarEntry = e.nextElement();
            String jarEntryName = jarEntry.getName();
            String jarConnectionEntryName = jarConnection.getEntryName();

            /**
             * Extract files only if they match the path.
             */
            if (jarEntryName.startsWith(jarConnectionEntryName)) {

                String filename = jarEntryName.startsWith(jarConnectionEntryName)
                        ? jarEntryName.substring(jarConnectionEntryName.length())
                        : jarEntryName;
                File currentFile = new File(destDir, filename);

                if (jarEntry.isDirectory()) {
                    currentFile.mkdirs();
                } else {
                    InputStream is = jarFile.getInputStream(jarEntry);
                    OutputStream out = FileUtils.openOutputStream(currentFile);
                    IOUtils.copy(is, out);
                    is.close();
                    out.close();
                }
            }
        }
    } catch (IOException e) {
        // TODO add logger
        e.printStackTrace();
    }

}

From source file:com.technophobia.substeps.report.DefaultExecutionReportBuilder.java

public void copyJarResourcesRecursively(final File destination, final JarURLConnection jarConnection)
        throws IOException {
    final JarFile jarFile = jarConnection.getJarFile();
    for (final JarEntry entry : Collections.list(jarFile.entries())) {
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            final String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
            if (!entry.isDirectory()) {
                InputStream entryInputStream = null;
                try {
                    entryInputStream = jarFile.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName));
                } finally {
                    IOUtils.closeQuietly(entryInputStream);
                }/*from  w  w w.j ava 2  s  .c om*/
            } else {
                new File(destination, fileName).mkdirs();
            }
        }
    }
}

From source file:org.apache.myfaces.config.impl.FacesConfigEntityResolver.java

public InputSource resolveEntity(String publicId, String systemId) throws IOException {
    InputStream stream;/*from  w  w w.j  a  v  a 2  s  .c o  m*/
    if (systemId.equals(FACES_CONFIG_1_0_DTD_SYSTEM_ID)) {
        stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_0_DTD_RESOURCE);
    } else if (systemId.equals(FACES_CONFIG_1_1_DTD_SYSTEM_ID)) {
        stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_1_DTD_RESOURCE);
    }

    else if (systemId.startsWith("jar:")) {
        URL url = new URL(systemId);
        JarURLConnection conn = (JarURLConnection) url.openConnection();
        JarEntry jarEntry = conn.getJarEntry();
        if (jarEntry == null) {
            log.fatal("JAR entry '" + systemId + "' not found.");
        }
        //_jarFile.getInputStream(jarEntry);
        stream = conn.getJarFile().getInputStream(jarEntry);
    } else {
        if (_externalContext == null) {
            stream = ClassUtils.getResourceAsStream(systemId);
        } else {
            if (systemId.startsWith("file:")) {
                systemId = systemId.substring(7); // remove file://
            }
            stream = _externalContext.getResourceAsStream(systemId);
        }
    }

    if (stream == null) {
        return null;
    }
    InputSource is = new InputSource(stream);
    is.setPublicId(publicId);
    is.setSystemId(systemId);
    is.setEncoding("ISO-8859-1");
    return is;
}