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.nuxeo.osgi.application.loader.FrameworkLoader.java

protected static boolean isBundle(File f) {
    Manifest mf;/*  w  ww.  jav a 2 s. com*/
    try {
        if (f.isFile()) { // jar file
            JarFile jf = new JarFile(f);
            try {
                mf = jf.getManifest();
            } finally {
                jf.close();
            }
            if (mf == null) {
                return false;
            }
        } else if (f.isDirectory()) { // directory
            f = new File(f, "META-INF/MANIFEST.MF");
            if (!f.isFile()) {
                return false;
            }
            mf = new Manifest();
            FileInputStream input = new FileInputStream(f);
            try {
                mf.read(input);
            } finally {
                input.close();
            }
        } else {
            return false;
        }
    } catch (IOException e) {
        return false;
    }
    return mf.getMainAttributes().containsKey(SYMBOLIC_NAME);
}

From source file:org.rhq.core.clientapi.descriptor.AgentPluginDescriptorUtil.java

/**
 * Obtains the manifest of the plugin file represented by the given deployment info.
 * Use this method rather than calling deploymentInfo.getManifest()
 * (workaround for https://jira.jboss.org/jira/browse/JBAS-6266).
 *
 * @param pluginFile the plugin file/*from  w w w  . ja v a 2  s  .co m*/
 * @return the deployed plugin's manifest
 */
private static Manifest getManifest(File pluginFile) {
    try {
        JarFile jarFile = new JarFile(pluginFile);
        try {
            Manifest manifest = jarFile.getManifest();
            return manifest;
        } finally {
            jarFile.close();
        }
    } catch (Exception ignored) {
        return null; // this is OK, it just means we do not have a manifest
    }
}

From source file:org.jboss.tools.central.internal.ImageUtil.java

/**
 * Creates an image from an {@link URL}.
 * If the iconUrl points at a jar file, the created image doesn't not leak file handle. 
 *///from  w w w  . j  a  v a  2  s.c om
public static Image createImageFromUrl(Device device, URL iconUrl) {

    if (!iconUrl.getProtocol().equals("jar")) {
        ImageDescriptor descriptor = ImageDescriptor.createFromURL(iconUrl);
        return descriptor.createImage();
    }

    //Load from jar:
    Image image = null;
    try {
        String fileName = iconUrl.getFile();
        if (fileName.contains("!")) {
            String[] location = fileName.split("!");
            fileName = location[0];
            String imageName = URLDecoder.decode(location[1].substring(1), "utf-8");
            File file = new File(new URI(fileName));
            JarFile jarFile = null;
            InputStream inputStream = null;
            try {
                jarFile = new JarFile(file);
                ZipEntry imageEntry = jarFile.getEntry(imageName);
                if (imageEntry != null) {
                    inputStream = jarFile.getInputStream(imageEntry);
                    image = new Image(device, inputStream);
                }
            } finally {
                IOUtils.closeQuietly(inputStream);
                try {
                    if (jarFile != null) {
                        jarFile.close();
                    }
                } catch (Exception e) {
                    //ignore
                }
            }
        }
    } catch (Exception e) {
        JBossCentralActivator.log(e);
    }

    return image;
}

From source file:org.colombbus.tangara.Main.java

public static void copyFilesInTempDirectory() {
    try {/*w ww .j a v a  2s.  c  o  m*/
        // Creation of a temp directory
        tempDirectory = FileUtils.createTempDirectory();
        Configuration conf = Configuration.instance();
        StringTokenizer resources = new StringTokenizer(conf.getProperty("program.resources"), ",");
        String resource = null;

        JarFile jarFile = new JarFile(conf.getTangaraPath());

        while (resources.hasMoreTokens()) {
            resource = resources.nextToken();
            ZipEntry entry = jarFile.getEntry(RESOURCES_DIRECTORY + resource);
            if (entry == null) {
                jarFile.close();
                throw new Exception("Resource '" + resource + "' not found");
            }
            BufferedInputStream input = new BufferedInputStream(jarFile.getInputStream(entry));
            File destinationFile = new File(tempDirectory, resource);
            destinationFile.createNewFile();
            FileUtils.copyFile(input, destinationFile);
        }
    } catch (Exception e) {
        LOG.error("error while copying program files: ", e);
    }
}

From source file:org.jahia.utils.maven.plugin.jarscan.JarsToSkipListMojo.java

private static boolean hasTLDFiles(File file) throws IOException {
    String name = file.getName();
    if (!file.isFile() || !name.endsWith(".jar")) {
        return false;
    }//from  w w  w  .jav  a2s  . co m

    boolean tldPresent = false;
    JarFile jar = null;
    try {
        jar = new JarFile(file);
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            String entry = entries.nextElement().getName();
            if (entry != null && entry.startsWith("META-INF/") && entry.endsWith(".tld")) {
                tldPresent = true;
                break;
            }
        }
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    return tldPresent;
}

From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java

public static String getPortalVaadinJarVersion(String jarPath) throws IOException {
    JarFile jarFile = new JarFile(jarPath);
    try {/*  www  .  j  a v  a  2s .  co  m*/
        String version = getManifestVaadinVersion(jarFile);
        if (version == null) {
            version = getPomVaadinVersion(jarFile);
        }

        return version;
    } catch (Exception ex) {
        return null;
    } finally {
        try {
            jarFile.close();
        } catch (IOException e) {
            log.warn(e);
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfigurationTest.java

/**
 * Return the classes inside the specified package and its sub-packages.
 * @param klass a class inside that package
 * @return a list of class names// w  ww  .  j av a2  s. com
 */
public static List<String> getClassesForPackage(final Class<?> klass) {
    final List<String> list = new ArrayList<>();

    File directory = null;
    final String relPath = klass.getName().replace('.', '/') + ".class";

    final URL resource = JavaScriptConfiguration.class.getClassLoader().getResource(relPath);

    if (resource == null) {
        throw new RuntimeException("No resource for " + relPath);
    }
    final String fullPath = resource.getFile();

    try {
        directory = new File(resource.toURI()).getParentFile();
    } catch (final URISyntaxException e) {
        throw new RuntimeException(klass.getName() + " (" + resource + ") does not appear to be a valid URL",
                e);
    } catch (final IllegalArgumentException e) {
        directory = null;
    }

    if (directory != null && directory.exists()) {
        addClasses(directory, klass.getPackage().getName(), list);
    } else {
        try {
            String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
            if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
                jarPath = jarPath.replace("%20", " ");
            }
            final JarFile jarFile = new JarFile(jarPath);
            for (final Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
                final String entryName = entries.nextElement().getName();
                if (entryName.endsWith(".class")) {
                    list.add(entryName.replace('/', '.').replace('\\', '.').replace(".class", ""));
                }
            }
            jarFile.close();
        } catch (final IOException e) {
            throw new RuntimeException(klass.getPackage().getName() + " does not appear to be a valid package",
                    e);
        }
    }
    return list;
}

From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java

public static String getPortalVaadin6Version() throws IOException {

    JarFile jarFile = new JarFile(get6VersionVaadinJarLocation());

    try {/*from w  w w .  ja v  a  2  s.c  o  m*/
        // Check Vaadin 6 version from manifest
        String manifestVaadinVersion = getManifestVaadin6Version(jarFile);
        if (manifestVaadinVersion != null) {
            return manifestVaadinVersion;
        }
        return null;
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {
                log.warn(e);
            }
        }
    }
}

From source file:com.web.server.util.ClassLoaderUtil.java

public static CopyOnWriteArrayList closeClassLoader(ClassLoader cl) {
    CopyOnWriteArrayList jars = new CopyOnWriteArrayList();
    boolean res = false;
    Class classURLClassLoader = null;
    if (cl instanceof URLClassLoader)
        classURLClassLoader = URLClassLoader.class;
    else if (cl instanceof VFSClassLoader) {
        classURLClassLoader = VFSClassLoader.class;
    }/*from ww  w . jav a2s .co  m*/
    Field f = null;
    try {
        f = classURLClassLoader.getDeclaredField("ucp");
        System.out.println(f);
    } catch (NoSuchFieldException e1) {
        // e1.printStackTrace();
        // log.info(e1.getMessage(), e1);

    }
    if (f != null) {
        f.setAccessible(true);
        Object obj = null;
        try {
            obj = f.get(cl);
        } catch (IllegalAccessException e1) {
            // e1.printStackTrace();
            // log.info(e1.getMessage(), e1);
        }
        if (obj != null) {
            final Object ucp = obj;
            f = null;
            try {
                f = ucp.getClass().getDeclaredField("loaders");
                System.out.println(f);
            } catch (NoSuchFieldException e1) {
                // e1.printStackTrace();
                // log.info(e1.getMessage(), e1);
            }
            if (f != null) {
                f.setAccessible(true);
                ArrayList loaders = null;
                try {
                    loaders = (ArrayList) f.get(ucp);
                    res = true;
                } catch (IllegalAccessException e1) {
                    // e1.printStackTrace();
                }
                for (int i = 0; loaders != null && i < loaders.size(); i++) {
                    obj = loaders.get(i);
                    f = null;
                    try {
                        f = obj.getClass().getDeclaredField("jar");
                        // log.info(f);
                    } catch (NoSuchFieldException e) {
                        // e.printStackTrace();
                        // log.info(e.getMessage(), e);
                    }
                    if (f != null) {
                        f.setAccessible(true);
                        try {
                            obj = f.get(obj);
                        } catch (IllegalAccessException e1) {
                            // e1.printStackTrace();
                            // log.info(e1.getMessage(), e1);
                        }
                        if (obj instanceof JarFile) {
                            final JarFile jarFile = (JarFile) obj;
                            System.out.println(jarFile.getName());
                            jars.add(jarFile.getName().replace("/", "\\").trim().toUpperCase());
                            // try {
                            // jarFile.getManifest().clear();
                            // } catch (IOException e) {
                            // // ignore
                            // }
                            try {
                                jarFile.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                                // ignore
                                // log.info(e.getMessage(), e);
                            }
                        }
                    }
                }
            }
        }
    }
    return jars;
}

From source file:com.norconex.commons.lang.ClassFinder.java

private static List<String> findSubTypesFromJar(File jarFile, Class<?> superClass) {

    List<String> classes = new ArrayList<String>();
    ClassLoader loader = getClassLoader(jarFile);
    if (loader == null) {
        return classes;
    }/*  w ww  . jav  a2s  .c o m*/
    JarFile jar = null;
    try {
        jar = new JarFile(jarFile);
    } catch (IOException e) {
        LOG.error("Invalid JAR: " + jarFile, e);
        return classes;
    }
    Enumeration<JarEntry> entries = jar.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String className = entry.getName();
        className = resolveName(loader, className, superClass);
        if (className != null) {
            classes.add(className);
        }
    }
    try {
        jar.close();
    } catch (IOException e) {
        LOG.error("Could not close JAR.", e);
    }
    return classes;
}