Example usage for java.util.jar JarFile close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP file.

Usage

From source file:com.qmetry.qaf.automation.core.ConfigurationManager.java

private static Map<String, String> getBuildInfo() {
    Manifest manifest = null;//www .j  av  a  2 s .  co m
    Map<String, String> buildInfo = new HashMap<String, String>();
    JarFile jar = null;
    try {
        URL url = ConfigurationManager.class.getProtectionDomain().getCodeSource().getLocation();
        File file = new File(url.toURI());
        jar = new JarFile(file);
        manifest = jar.getManifest();
    } catch (NullPointerException ignored) {
    } catch (URISyntaxException ignored) {
    } catch (IOException ignored) {
    } catch (IllegalArgumentException ignored) {
    } finally {
        if (null != jar)
            try {
                jar.close();
            } catch (IOException e) {
                log.warn(e.getMessage());
            }
    }

    if (manifest == null) {
        return buildInfo;
    }

    try {
        Attributes attributes = manifest.getAttributes("Build-Info");
        Set<Entry<Object, Object>> entries = attributes.entrySet();
        for (Entry<Object, Object> e : entries) {
            buildInfo.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
        }
    } catch (NullPointerException e) {
        // Fall through
    }

    return buildInfo;
}

From source file:org.commonjava.web.test.fixture.JarKnockouts.java

public static File rewriteJar(final File source, final File targetDir, final Set<JarKnockouts> jarKnockouts)
        throws IOException {
    final JarKnockouts allKnockouts = new JarKnockouts();
    for (final JarKnockouts jk : jarKnockouts) {
        allKnockouts.knockoutPaths(jk.getKnockedOutPaths());
    }/*  w w  w  . ja  v a  2  s.  c  o  m*/

    targetDir.mkdirs();
    final File target = new File(targetDir, source.getName());

    JarFile in = null;
    JarOutputStream out = null;
    try {
        in = new JarFile(source);

        final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target));
        out = new JarOutputStream(fos, in.getManifest());

        final Enumeration<JarEntry> entries = in.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!allKnockouts.knockout(entry.getName())) {
                final InputStream stream = in.getInputStream(entry);
                out.putNextEntry(entry);
                copy(stream, out);
                out.closeEntry();
            }
        }
    } finally {
        closeQuietly(out);
        if (in != null) {
            try {
                in.close();
            } catch (final IOException e) {
            }
        }
    }

    return target;
}

From source file:org.apache.wiki.util.ClassUtil.java

/**
 * Searchs for all the files in classpath under a given package, for a given {@link JarURLConnection}.
 * /*  www. jav a2  s . c  o  m*/
 * @param results collection in which the found entries are stored
 * @param jurlcon given {@link JarURLConnection} to search in.
 * @param rootPackage base package.
 */
static void jarEntriesUnder(List<String> results, JarURLConnection jurlcon, String rootPackage) {
    JarFile jar = null;
    try {
        jar = jurlcon.getJarFile();
        log.debug("scanning [" + jar.getName() + "]");
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.getName().startsWith(rootPackage) && !entry.isDirectory()) {
                results.add(entry.getName());
            }
        }
    } catch (IOException ioe) {
        log.error(ioe.getMessage(), ioe);
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException ioe) {
                log.error(ioe.getMessage(), ioe);
            }
        }
    }
}

From source file:org.nuxeo.osgi.util.jar.URLJarFileCloser.java

@Override
public void close(JarFile file) throws IOException {
    file.close();
    URL location = new File(file.getName()).toURI().toURL();
    boolean closed = false;
    try {//ww  w  .  ja v a2s . c  o  m
        final SharedResourceLoader loader = Framework.getResourceLoader();
        if (loader != null) {
            closed = introspector.newURLClassLoaderCloser(loader).close(location);
        }
    } catch (URLJarFileIntrospectionError cause) {
        LogFactory.getLog(URLJarFileCloser.class).error("Cannot introspect shared resource loader", cause);

    }
    if (closed == false) {
        if (applicationCloser != null) {
            closed = applicationCloser.close(location);
        }
    }
    introspector.close(location);
}

From source file:com.cisco.dbds.utils.configfilehandler.ConfigFileHandler.java

/**
 * Load jar cong file./*from   w w w. ja  v  a 2 s.  c o  m*/
 *
 * @param Utilclass the utilclass
 */
public static void loadJarCongFile(Class Utilclass) {
    try {
        String path = Utilclass.getResource("").getPath();
        path = path.substring(6, path.length() - 1);
        path = path.split("!")[0];
        System.out.println(path);
        JarFile jarFile = new JarFile(path);

        final Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (entry.getName().contains(".properties")) {
                System.out.println("Jar File Property File: " + entry.getName());
                JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
                InputStream input = jarFile.getInputStream(fileEntry);
                setSystemvariable(input);
                InputStreamReader isr = new InputStreamReader(input);
                BufferedReader reader = new BufferedReader(isr);
                String line;

                while ((line = reader.readLine()) != null) {
                    System.out.println("Jar file" + line);
                }
                reader.close();
            }
        }
        jarFile.close();
    } catch (Exception e) {
        System.out.println("Jar file reading Error");
    }
}

From source file:org.wso2.carbon.wsdl2code.POMGenerator.java

public static String getVersion(String artifact) throws Exception {

    java.io.File file = new java.io.File(artifact);
    java.util.jar.JarFile jar = new java.util.jar.JarFile(file);
    java.util.jar.Manifest manifest = jar.getManifest();

    String versionNumber = "";
    java.util.jar.Attributes attributes = manifest.getMainAttributes();
    if (attributes != null) {
        java.util.Iterator it = attributes.keySet().iterator();
        while (it.hasNext()) {
            java.util.jar.Attributes.Name key = (java.util.jar.Attributes.Name) it.next();
            String keyword = key.toString();
            if (keyword.equals("Implementation-Version") || keyword.equals("Bundle-Version")) {
                versionNumber = (String) attributes.get(key);
                break;
            }/*from   w  w w .j  av a 2  s .  co m*/
        }
    }
    jar.close();

    if (versionNumber != null && !versionNumber.equals("")) {
        return versionNumber;
    }

    //if manifest does not contain version number it had to be extracted from the file name
    String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
    if (fileName.contains(".")) {
        String majorVersion = fileName.substring(0, fileName.indexOf("."));
        String minorVersion = fileName.substring(fileName.indexOf("."));
        int delimiter = majorVersion.lastIndexOf("-");
        if (majorVersion.indexOf("_") > delimiter)
            delimiter = majorVersion.indexOf("_");
        majorVersion = majorVersion.substring(delimiter + 1, fileName.indexOf("."));
        versionNumber = majorVersion + minorVersion;
    }

    return versionNumber;

}

From source file:com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptorBuilder.java

private void closeQuietly(JarFile jarFile) {
    if (jarFile != null) {
        try {/*from www . j  a v a2s .  co  m*/
            jarFile.close();
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:com.navercorp.pinpoint.bootstrap.agentdir.AgentDirBaseClassPathResolverTest.java

private void closeJarFile(List<JarFile> jarFiles) {
    for (JarFile jarFile : jarFiles) {
        try {/*from   w ww  . ja  va  2  s  . c  om*/
            jarFile.close();
        } catch (IOException e) {
            logger.debug(jarFile + " delete fail", e);
        }
    }
}

From source file:org.shept.util.JarUtils.java

/**
 * Copy resources from a classPath, typically within a jar file 
 * to a specified destination, typically a resource directory in
 * the projects webApp directory (images, sounds, e.t.c. )
 * /*  w  ww .  j  a va  2s  .c om*/
 * Copies resources only if the destination file does not exist and
 * the specified resource is available.
 * 
 * The ClassPathResource will be scanned for all resources in the path specified by the resource.
 * For example  a path like:
 *    new ClassPathResource("resource/images/pager/", SheptBaseController.class);
 * takes all the resources in the path 'resource/images/pager' (but not in sub-path)
 * from the specified clazz 'SheptBaseController'
 * 
 * @param cpr ClassPathResource specifying the source location on the classPath (maybe within a jar)
 * @param webAppDestPath Full path String to the fileSystem destination directory   
 * @throws IOException when copying on copy error
 * @throws URISyntaxException 
 */
public static void copyResources(ClassPathResource cpr, String webAppDestPath)
        throws IOException, URISyntaxException {
    String dstPath = webAppDestPath; //  + "/" + jarPathInternal(cpr.getURL());
    File dir = new File(dstPath);
    dir.mkdirs();

    URL url = cpr.getURL();
    // jarUrl is the URL of the containing lib, e.g. shept.org in this case
    URL jarUrl = ResourceUtils.extractJarFileURL(url);
    String urlFile = url.getFile();
    String resPath = "";
    int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
    if (separatorIndex != -1) {
        // just copy the the location path inside the jar without leading separators !/
        resPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
    } else {
        return; // no resource within jar to copy
    }

    File f = new File(ResourceUtils.toURI(jarUrl));
    JarFile jf = new JarFile(f);

    Enumeration<JarEntry> entries = jf.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String path = entry.getName();
        if (path.startsWith(resPath) && entry.getSize() > 0) {
            String fileName = path.substring(path.lastIndexOf("/"));
            File dstFile = new File(dstPath, fileName); //  (StringUtils.applyRelativePath(dstPath, fileName));
            Resource fileRes = cpr.createRelative(fileName);
            if (!dstFile.exists() && fileRes.exists()) {
                FileOutputStream fos = new FileOutputStream(dstFile);
                FileCopyUtils.copy(fileRes.getInputStream(), fos);
                logger.info("Successfully copied file " + fileName + " from " + cpr.getPath() + " to "
                        + dstFile.getPath());
            }
        }
    }

    if (jf != null) {
        jf.close();
    }

}

From source file:com.navercorp.pinpoint.bootstrap.AgentDirBaseClassPathResolverTest.java

private void closeJarFile(BootstrapJarFile bootstrapJarFile) {
    final List<JarFile> jarFileList = bootstrapJarFile.getJarFileList();
    for (JarFile jarFile : jarFileList) {
        try {//from   w ww  .  j  ava  2s  .  com
            jarFile.close();
        } catch (IOException e) {
            logger.debug(jarFile + " delete fail", e);
        }
    }
}