Example usage for java.util.jar JarFile entries

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

Introduction

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

Prototype

public Enumeration<JarEntry> entries() 

Source Link

Document

Returns an enumeration of the jar file entries.

Usage

From source file:com.photon.maven.plugins.android.common.JarHelper.java

/** Unjars the specified jar file into the the specified directory
 *
 * @param jarFile//w w  w.j  a v  a 2 s. com
 * @param outputDirectory
 * @param unjarListener
 * @throws IOException
 */
public static void unjar(JarFile jarFile, File outputDirectory, UnjarListener unjarListener)
        throws IOException {
    for (Enumeration en = jarFile.entries(); en.hasMoreElements();) {
        JarEntry entry = (JarEntry) en.nextElement();
        File entryFile = new File(outputDirectory, entry.getName());
        if (unjarListener.include(entry)) {
            if (!entryFile.getParentFile().exists() && !entryFile.getParentFile().mkdirs()) {
                throw new IOException("Error creating output directory: " + entryFile.getParentFile());
            }

            // If the entry is an actual file, unzip that too
            if (!entry.isDirectory()) {
                final InputStream in = jarFile.getInputStream(entry);
                try {
                    final OutputStream out = new FileOutputStream(entryFile);
                    try {
                        IOUtil.copy(in, out);
                    } finally {
                        IOUtils.closeQuietly(out);
                    }
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
    }
}

From source file:com.apifest.oauth20.LifecycleEventHandlers.java

@SuppressWarnings("unchecked")
public static void loadLifecycleHandlers(URLClassLoader classLoader, String customJar) {
    try {//from www. j a v a2 s  .  c om
        if (classLoader != null) {
            JarFile jarFile = new JarFile(customJar);
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
                    continue;
                }
                // remove .class
                String className = entry.getName().substring(0, entry.getName().length() - 6);
                className = className.replace('/', '.');
                try {
                    // REVISIT: check for better solution
                    if (className.startsWith("org.jboss.netty") || className.startsWith("org.apache.log4j")
                            || className.startsWith("org.apache.commons")) {
                        continue;
                    }
                    Class<?> clazz = classLoader.loadClass(className);
                    if (clazz.isAnnotationPresent(OnRequest.class)
                            && LifecycleHandler.class.isAssignableFrom(clazz)) {
                        requestEventHandlers.add((Class<LifecycleHandler>) clazz);
                        log.debug("preIssueTokenHandler added {}", className);
                    }
                    if (clazz.isAnnotationPresent(OnResponse.class)
                            && LifecycleHandler.class.isAssignableFrom(clazz)) {
                        responseEventHandlers.add((Class<LifecycleHandler>) clazz);
                        log.debug("postIssueTokenHandler added {}", className);
                    }
                    if (clazz.isAnnotationPresent(OnException.class)
                            && ExceptionEventHandler.class.isAssignableFrom(clazz)) {
                        exceptionHandlers.add((Class<ExceptionEventHandler>) clazz);
                        log.debug("exceptionHandlers added {}", className);
                    }
                } catch (ClassNotFoundException e1) {
                    // continue
                }
            }
        }
    } catch (MalformedURLException e) {
        log.error("cannot load lifecycle handlers", e);
    } catch (IOException e) {
        log.error("cannot load lifecycle handlers", e);
    } catch (IllegalArgumentException e) {
        log.error(e.getMessage());
    }
}

From source file:org.raspinloop.fmi.VMRunnerUtils.java

private static boolean containsClass(JarFile jarFile, String agentRunnerClassName) {

    Enumeration<JarEntry> e = jarFile.entries();
    while (e.hasMoreElements()) {
        JarEntry je = e.nextElement();
        if (je.isDirectory() || !je.getName().endsWith(".class")) {
            continue;
        }//from   w  w  w . ja  v  a2s  . c  o m
        // -6 because of .class
        String className = je.getName().substring(0, je.getName().length() - 6);
        className = className.replace('/', '.');
        if (className.equals(agentRunnerClassName))
            return true;
    }
    return false;
}

From source file:azkaban.jobtype.connectors.teradata.TeraDataWalletInitializer.java

/**
 * As of TDCH 1.4.1, integration with Teradata wallet only works with hadoop jar command line command.
 * This is mainly because TDCH depends on the behavior of hadoop jar command line which extracts jar file
 * into hadoop tmp folder./*from w w  w. j a va2 s.  c o m*/
 *
 * This method will extract tdchJarfile and place it into temporary folder, and also add jvm shutdown hook
 * to delete the directory when JVM shuts down.
 * @param tdchJarFile TDCH jar file.
 */
public static void initialize(File tmpDir, File tdchJarFile) {
    synchronized (TeraDataWalletInitializer.class) {
        if (tdchJarExtractedDir != null) {
            return;
        }

        if (tdchJarFile == null) {
            throw new IllegalArgumentException("TDCH jar file cannot be null.");
        }
        if (!tdchJarFile.exists()) {
            throw new IllegalArgumentException(
                    "TDCH jar file does not exist. " + tdchJarFile.getAbsolutePath());
        }
        try {
            //Extract TDCH jar.
            File unJarDir = createUnjarDir(
                    new File(tmpDir.getAbsolutePath() + File.separator + UNJAR_DIR_NAME));
            JarFile jar = new JarFile(tdchJarFile);
            Enumeration<JarEntry> enumEntries = jar.entries();

            while (enumEntries.hasMoreElements()) {
                JarEntry srcFile = enumEntries.nextElement();
                File destFile = new File(unJarDir + File.separator + srcFile.getName());
                if (srcFile.isDirectory()) { // if its a directory, create it
                    destFile.mkdir();
                    continue;
                }

                InputStream is = jar.getInputStream(srcFile);
                BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(destFile));
                IOUtils.copy(is, os);
                close(os);
                close(is);
            }
            jar.close();
            tdchJarExtractedDir = unJarDir;
        } catch (IOException e) {
            throw new RuntimeException("Failed while extracting TDCH jar file.", e);
        }
    }
    logger.info("TDCH jar has been extracted into directory: " + tdchJarExtractedDir.getAbsolutePath());
}

From source file:org.jvnet.hudson.test.JellyTestSuiteBuilder.java

static Map<URL, String> scan(File resources, String extension) throws IOException {
    Map<URL, String> result = new HashMap<>();
    if (resources.isDirectory()) {
        for (File f : FileUtils.listFiles(resources, new String[] { extension }, true)) {
            result.put(f.toURI().toURL(),
                    f.getAbsolutePath().substring((resources.getAbsolutePath() + File.separator).length()));
        }/*from   w ww  . j av a  2  s .c  o m*/
    } else if (resources.getName().endsWith(".jar")) {
        String jarUrl = resources.toURI().toURL().toExternalForm();
        JarFile jf = new JarFile(resources);
        Enumeration<JarEntry> e = jf.entries();
        while (e.hasMoreElements()) {
            JarEntry ent = e.nextElement();
            if (ent.getName().endsWith("." + extension)) {
                result.put(new URL("jar:" + jarUrl + "!/" + ent.getName()), ent.getName());
            }
        }
        jf.close();
    }
    return result;
}

From source file:org.javaweb.utils.ClassUtils.java

/**
 * ?jarclass//from  w w  w.  j  a  va2s.c o m
 *
 * @param url
 * @param classList
 * @throws IOException
 */
public static void getAllJarClass(URL url, Set<String> classList) throws IOException {
    if (url != null) {
        JarURLConnection juc = (JarURLConnection) url.openConnection();
        JarFile jf = juc.getJarFile();
        Enumeration<JarEntry> je = jf.entries();

        while (je.hasMoreElements()) {
            JarEntry jar = je.nextElement();

            if (jar.getName().endsWith(".class")) {
                String classPath = jar.getName().replaceAll("\\\\", "/").replaceAll("/+", ".");
                classList.add(classPath.substring(0, classPath.length() - ".class".length()));
            }
        }
    }
}

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

/**
 * Load jar cong file./*from   w w w . j  av a 2s  . c  om*/
 *
 * @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.reprap.configuration.store.ConfigurationInitializer.java

private static void copyJarTree(final URL source, final File target) throws IOException {
    final JarURLConnection jarConnection = (JarURLConnection) source.openConnection();
    final String prefix = jarConnection.getEntryName();
    final JarFile jarFile = jarConnection.getJarFile();
    for (final JarEntry jarEntry : Collections.list(jarFile.entries())) {
        final String entryName = jarEntry.getName();
        if (entryName.startsWith(prefix)) {
            if (!jarEntry.isDirectory()) {
                final String fileName = StringUtils.removeStart(entryName, prefix);
                final InputStream fileStream = jarFile.getInputStream(jarEntry);
                try {
                    FileUtils.copyInputStreamToFile(fileStream, new File(target, fileName));
                } finally {
                    fileStream.close();/*from w w w.  ja  v  a  2s  .  c  o m*/
                }
            }
        }
    }
}

From source file:com.dragome.compiler.utils.FileManager.java

private static List<String> findClassesInJar(JarFile jarFile) {
    ArrayList<String> result = new ArrayList<String>();

    final Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        final JarEntry entry = entries.nextElement();
        final String entryName = entry.getName();
        if (entryName.endsWith(".class"))
            result.add(entryName.replace('/', File.separatorChar).replace(".class", ""));
    }//from   w  w w  . ja  va 2  s.  co m

    return result;
}

From source file:javadepchecker.Main.java

private static boolean depNeeded(String pkg, Collection<String> deps) throws IOException {
    for (String jarName : getPackageJars(pkg)) {
        JarFile jar = new JarFile(jarName);
        for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
            String name = e.nextElement().getName();
            if (deps.contains(name)) {
                return true;
            }// ww w. j a v a  2  s .co  m
        }
    }
    return false;
}