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:org.colombbus.tangara.FileUtils.java

public static int extractJar(File jar, File directory) {
    JarEntry entry = null;//from w  w  w . j  a  v a 2s  .  co m
    File currentFile = null;
    BufferedInputStream input = null;
    JarFile jarFile = null;
    int filesCount = 0;
    try {
        jarFile = new JarFile(jar);
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            currentFile = new File(directory, entry.getName());
            if (entry.isDirectory()) {
                currentFile.mkdir();
            } else {
                currentFile.createNewFile();
                input = new BufferedInputStream(jarFile.getInputStream(entry));
                copyFile(input, currentFile);
                input.close();
                filesCount++;
            }
        }
    } catch (IOException e) {
        LOG.error("Error extracting JAR file " + jar.getAbsolutePath(), e);
    } finally {
        try {
            if (input != null) {
                input.close();
            }
            if (jarFile != null) {
                jarFile.close();
            }
        } catch (IOException e) {
        }
    }
    return filesCount;
}

From source file:org.echocat.nodoodle.classloading.FileClassLoader.java

@Override
public synchronized void close() throws IOException {
    if (!_closed) {
        _closed = true;//from  w ww . j a v a2  s .  com
        for (JarFile jarFile : _jarFiles) {
            jarFile.close();
        }
    }
}

From source file:org.jahia.modules.serversettings.portlets.BasePortletHelper.java

/**
 * Returns a file descriptor for the modified (prepared) portlet WAR file.
 * /* w  w  w. j a  v a 2 s .co  m*/
 * @param sourcePortletWar
 *            the source portlet WAR file
 * @return a file descriptor for the modified (prepared) portlet WAR file
 * @throws IOException
 *             in case of processing error
 */
public File process(File sourcePortletWar) throws IOException {
    JarFile jar = new JarFile(sourcePortletWar);
    File dest = new File(FilenameUtils.getFullPathNoEndSeparator(sourcePortletWar.getPath()),
            FilenameUtils.getBaseName(sourcePortletWar.getName()) + ".war");
    try {
        boolean needsServerSpecificProcessing = needsProcessing(jar);
        if (portletTldsPresent(jar) && !needsServerSpecificProcessing) {
            return sourcePortletWar;
        }
        jar.close();
        final JarInputStream jarIn = new JarInputStream(new FileInputStream(sourcePortletWar));
        final Manifest manifest = jarIn.getManifest();
        final JarOutputStream jarOut;
        if (manifest != null) {
            jarOut = new JarOutputStream(new FileOutputStream(dest), manifest);
        } else {
            jarOut = new JarOutputStream(new FileOutputStream(dest));
        }

        try {
            copyEntries(jarIn, jarOut);

            process(jarIn, jarOut);

            if (!hasPortletTld) {
                addToJar("META-INF/portlet-resources/portlet.tld", "WEB-INF/portlet.tld", jarOut);
            }
            if (!hasPortlet2Tld) {
                addToJar("META-INF/portlet-resources/portlet_2_0.tld", "WEB-INF/portlet_2_0.tld", jarOut);
            }
        } finally {
            jarIn.close();
            jarOut.close();
            FileUtils.deleteQuietly(sourcePortletWar);
        }
        return dest;
    } finally {
        jar.close();
    }
}

From source file:org.rhq.maven.plugins.ValidateMojo.java

private void validate(File agentPluginArchive, Set<File> parentPlugins) throws MojoExecutionException {
    JarFile jarFile = null;
    try {//from   www. j  a  v  a  2  s  . c om
        jarFile = new JarFile(agentPluginArchive);

        if (jarFile.getEntry("META-INF/rhq-plugin.xml") == null) {
            handleFailure("Descriptor missing");
        }
    } catch (Exception e) {
        handleException(e);
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception e) {
                handleException(e);
            }
        }

    }

    // Run the plugin validator as a forked process
    Process process = null;
    try {
        String pluginValidatorClasspath = buildPluginValidatorClasspath(agentPluginArchive, parentPlugins);
        String javaCommand = buildJavaCommand();
        ProcessBuilder processBuilder = buildProcessBuilder(javaCommand, pluginValidatorClasspath);
        process = processBuilder.start();
        redirectOuput(process);
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            handleFailure("Invalid plugin");
        }
    } catch (Exception e) {
        handleException(e);
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}

From source file:net.erdfelt.android.sdkfido.local.JarListing.java

private final void loadJar(File jarfile) throws IOException {
    JarFile jar = null;

    try {/*from  www . j  av a2  s.  co  m*/
        String name;
        jar = new JarFile(jarfile);
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            name = entry.getName();
            add(name);

            if (!entry.isDirectory() && name.endsWith(".class")) {
                classlist.add(name);
            }
        }
    } finally {
        if (jar != null) {
            jar.close();
        }
    }
}

From source file:org.hyperic.hq.plugin.jboss7.JBossDetectorBase.java

final String getVersion(Map<String, String> args) {
    String version = "not found";

    File jbossAsServerJar = null;
    String mp = args.get("mp");
    List<String> modulesPAths = Arrays.asList(mp.split(File.pathSeparator));
    log.debug("[getVersion] modulesPAths=" + modulesPAths);
    for (String path : modulesPAths) {
        Collection<File> files = listFileTree(new File(path));
        for (File file : files) {
            String name = file.getName();
            if (name.startsWith("jboss-as-server") && name.endsWith(".jar")) {
                jbossAsServerJar = file;
            }/* www. java  2 s.c o  m*/
        }
    }

    if (jbossAsServerJar != null) {
        try {
            JarFile jarFile = new JarFile(jbossAsServerJar);
            log.debug("[getVersion] jboss-as-server.jar = '" + jarFile.getName() + "'");
            Attributes attributes = jarFile.getManifest().getMainAttributes();
            jarFile.close();
            version = attributes.getValue("JBossAS-Release-Version");
        } catch (IOException e) {
            log.debug("[getVersion] Error getting JBoss version (" + e + ")", e);
        }
    } else {
        log.debug("[getVersion] 'jboss-as-server.*.jar' not found.");
    }

    return version;
}

From source file:org.rhq.enterprise.server.plugin.pc.perspective.PerspectiveServerPluginManager.java

private void undeployWars(ServerPluginEnvironment env) {
    String name = null;/*from   w w  w. j  av  a2s  .  c o  m*/
    try {
        JarFile plugin = new JarFile(env.getPluginUrl().getFile());
        try {
            for (JarEntry entry : Collections.list(plugin.entries())) {
                name = entry.getName();
                if (name.toLowerCase().endsWith(".war")) {
                    undeployWar(getDeployFile(env, entry.getName()));
                }
            }
        } finally {
            plugin.close();
        }
    } catch (Exception e) {
        this.log.error("Failed to deploy " + env.getPluginKey().getPluginName() + "#" + name, e);
    }
}

From source file:org.apache.storm.utils.ServerUtils.java

/**
 * Unpack matching files from a jar. Entries inside the jar that do
 * not match the given pattern will be skipped.
 *
 * @param jarFile the .jar file to unpack
 * @param toDir the destination directory into which to unpack the jar
 *///from w w w.j  a va  2  s . c  om
public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    ensureDirectory(file.getParentFile());
                    OutputStream out = new FileOutputStream(file);
                    try {
                        copyBytes(in, out, 8192);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:org.xwiki.webjars.internal.FilesystemResourceReferenceCopier.java

void processCSS(String resourcePrefix, String resourceName, String targetPrefix,
        FilesystemExportContext exportContext) throws Exception {
    String resourcePath = String.format(CONCAT_PATH_FORMAT, resourcePrefix, resourceName);
    JarFile jar = new JarFile(getJARFile(resourcePath));
    try {//from  w w w .  j a v a2s  . c o  m
        for (Enumeration<JarEntry> enumeration = jar.entries(); enumeration.hasMoreElements();) {
            JarEntry entry = enumeration.nextElement();
            if (entry.getName().equals(resourcePath)) {
                // Read the CSS and look for url() entries...
                processCSSfile(resourcePrefix, targetPrefix, entry, jar, exportContext);
                break;
            }
        }
    } finally {
        jar.close();
    }
}

From source file:com.wavemaker.commons.classloader.ThrowawayFileClassLoader.java

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {

    if (this.classPath == null) {
        throw new ClassNotFoundException("invalid search root: " + this.classPath);
    } else if (name == null) {
        throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage());
    }/*  w  w  w.j a va 2 s. c o  m*/

    String classNamePath = name.replace('.', '/') + ".class";

    byte[] fileBytes = null;
    try {
        InputStream is = null;
        JarFile jarFile = null;

        for (Resource entry : this.classPath) {
            if (entry.getFilename().toLowerCase().endsWith(".jar")) {
                jarFile = new JarFile(entry.getFile());
                ZipEntry ze = jarFile.getEntry(classNamePath);

                if (ze != null) {
                    is = jarFile.getInputStream(ze);
                    break;
                } else {
                    jarFile.close();
                }
            } else {
                Resource classFile = entry.createRelative(classNamePath);
                if (classFile.exists()) {
                    is = classFile.getInputStream();
                    break;
                }
            }
        }

        if (is != null) {
            try {
                fileBytes = IOUtils.toByteArray(is);
                is.close();
            } finally {
                if (jarFile != null) {
                    jarFile.close();
                }
            }
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(e.getMessage(), e);
    }

    if (name.contains(".")) {
        String packageName = name.substring(0, name.lastIndexOf('.'));
        if (getPackage(packageName) == null) {
            definePackage(packageName, "", "", "", "", "", "", null);
        }
    }

    Class<?> ret;
    if (fileBytes == null) {
        ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader);
    } else {
        ret = defineClass(name, fileBytes, 0, fileBytes.length);
    }

    if (ret == null) {
        throw new ClassNotFoundException(
                "Couldn't find class " + name + " in expected classpath: " + this.classPath);
    }

    return ret;
}