Example usage for java.util.jar JarFile getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the path name of the ZIP file.

Usage

From source file:ro.agrade.jira.qanda.utils.ResourceUtils.java

/**
 * Gets an entry in the JAR. It's your responsability to close the input
 * stream, as usually/*from w  ww  .  j a v a 2  s.co m*/
 * @param jar the jar file
 * @param jarEntryName the jar entry name
 * @return the input stream, or null if no entry is found in the file
 * @throws IOException if the jar is invalid
 */
public static InputStream getJarInputStream(JarFile jar, String jarEntryName) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Searching in JAR >>%s<<, entry >>%s<<", jar.getName(), jarEntryName));
    }
    JarEntry je = jar.getJarEntry(jarEntryName);
    if (je == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("No such entry [%s] in JAR >>%s<<", jarEntryName, jar.getName()));
        }
        return null;
    }
    return jar.getInputStream(je);
}

From source file:com.app.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;
        if (cl instanceof WebClassLoader) {
            String url = "";
            CopyOnWriteArrayList webCLUrl = ((WebClassLoader) cl).geturlS();
            for (int index = 0; index < webCLUrl.size(); index++) {

                try {
                    url = ((URL) webCLUrl.get(index)).toURI().toString();
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }//from  ww  w .  j a  v  a  2s  . co m
                jars.add(url.replace("file:", "").replace("/", "\\").toUpperCase());
            }
        }
    } else if (cl instanceof VFSClassLoader) {
        classURLClassLoader = VFSClassLoader.class;
    }
    Field f = null;
    try {
        f = classURLClassLoader.getDeclaredField("ucp");
        //log.info(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");
                //log.info(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;
                            //log.info(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.thoughtworks.go.agent.common.util.JarUtil.java

private static String getManifestKey(JarFile jarFile, String key) {
    try {//  ww  w  .j  a  v  a 2 s  .c  om
        Manifest manifest = jarFile.getManifest();
        if (manifest != null) {
            Attributes attributes = manifest.getMainAttributes();
            return attributes.getValue(key);
        }
    } catch (IOException e) {
        LOG.error("Exception while trying to read key {} from manifest of {}", key, jarFile.getName(), e);
    }
    return null;
}

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}.
 * //  ww  w  .java 2s.  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.ebayopensource.turmeric.eclipse.utils.io.IOUtil.java

/**
 * In the normal jar URLs usage in Windows Java puts a lock on it. And the
 * SOA Tool Handler will create a non locking URL by setting the caching
 * off. There is obviously a performance compromise made here by disabling
 * the cache./*  w  w  w .ja  v a  2  s  .co m*/
 *
 * @param jarFileUrl the jar file url
 * @param jarEntryPath the jar entry path
 * @return the non locking url
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static URL getNonLockingURL(URL jarFileUrl, String jarEntryPath) throws IOException {
    File file = FileUtils.toFile(jarFileUrl);
    JarFile jarFile;
    jarFile = new JarFile(file);
    JarEntry jarEntry = jarFile.getJarEntry(jarEntryPath);
    if (jarEntry != null) {
        SOAToolFileUrlHandler handler = new SOAToolFileUrlHandler(jarFile, jarEntry);
        URL retUrl = new URL("jar", "", -1,
                new File(jarFile.getName()).toURI().toURL() + "!/" + jarEntry.getName(), handler);
        handler.setExpectedUrl(retUrl);
        return retUrl;

    }
    return null;
}

From source file:org.owasp.dependencycheck.xml.pom.PomUtils.java

/**
 * Retrieves the specified POM from a jar file and converts it to a Model.
 *
 * @param path the path to the pom.xml file within the jar file
 * @param jar the jar file to extract the pom from
 * @return returns an object representation of the POM
 * @throws AnalysisException is thrown if there is an exception extracting
 * or parsing the POM {@link Model} object
 *//*  w w  w  .ja v a2 s  . c  o m*/
public static Model readPom(String path, JarFile jar) throws AnalysisException {
    final ZipEntry entry = jar.getEntry(path);
    Model model = null;
    if (entry != null) { //should never be null
        //noinspection CaughtExceptionImmediatelyRethrown
        try {
            final PomParser parser = new PomParser();
            model = parser.parse(jar.getInputStream(entry));
            if (model == null) {
                throw new AnalysisException(String.format("Unable to parse pom '%s/%s'", jar.getName(), path));
            }
        } catch (AnalysisException ex) {
            throw ex;
        } catch (SecurityException ex) {
            LOGGER.warn("Unable to parse pom '{}' in jar '{}'; invalid signature", path, jar.getName());
            LOGGER.debug("", ex);
            throw new AnalysisException(ex);
        } catch (IOException ex) {
            LOGGER.warn("Unable to parse pom '{}' in jar '{}' (IO Exception)", path, jar.getName());
            LOGGER.debug("", ex);
            throw new AnalysisException(ex);
        } catch (Throwable ex) {
            LOGGER.warn("Unexpected error during parsing of the pom '{}' in jar '{}'", path, jar.getName());
            LOGGER.debug("", ex);
            throw new AnalysisException(ex);
        }
    }
    return model;
}

From source file:ClassFileUtilities.java

private static void computeClassDependencies(InputStream is, Set classpath, Set done, Set result, boolean rec)
        throws IOException {

    Iterator it = getClassDependencies(is).iterator();
    while (it.hasNext()) {
        String s = (String) it.next();
        if (!done.contains(s)) {
            done.add(s);//from   www  .  j  av  a 2 s . c o m

            Iterator cpit = classpath.iterator();
            while (cpit.hasNext()) {
                InputStream depis = null;
                String path = null;
                Object cpEntry = cpit.next();
                if (cpEntry instanceof JarFile) {
                    JarFile jarFile = (JarFile) cpEntry;
                    String classFileName = s + ".class";
                    ZipEntry ze = jarFile.getEntry(classFileName);
                    if (ze != null) {
                        path = jarFile.getName() + '!' + classFileName;
                        depis = jarFile.getInputStream(ze);
                    }
                } else {
                    path = ((String) cpEntry) + '/' + s + ".class";
                    File f = new File(path);
                    if (f.isFile()) {
                        depis = new FileInputStream(f);
                    }
                }

                if (depis != null) {
                    result.add(path);

                    if (rec) {
                        computeClassDependencies(depis, classpath, done, result, rec);
                    }
                }
            }
        }
    }
}

From source file:org.hyperic.hq.plugin.jboss.JBossDetector.java

public static String getVersion(File installPath, String jar) {
    File file = new File(installPath, "lib" + File.separator + jar);

    if (!file.exists()) {
        log.debug("[getVersion] file '" + file + "' not found");
        return null;
    }/*from www .  ja  va  2  s . c  o  m*/

    Attributes attributes;
    try {
        JarFile jarFile = new JarFile(file);
        log.debug("[getVersion] jarFile='" + jarFile.getName() + "'");
        attributes = jarFile.getManifest().getMainAttributes();
        jarFile.close();
    } catch (IOException e) {
        log.debug(e, e);
        return null;
    }

    //e.g. Implementation-Version:
    //3.0.6 Date:200301260037
    //3.2.1 (build: CVSTag=JBoss_3_2_1 date=200306201521)
    //3.2.2 (build: CVSTag=JBoss_3_2_2 date=200310182216)
    //3.2.3 (build: CVSTag=JBoss_3_2_3 date=200311301445)
    //4.0.0DR2 (build: CVSTag=JBoss_4_0_0_DR2 date=200307030107)
    //5.0.1.GA (build: SVNTag=JBoss_5_0_1_GA date=200902231221)
    String version = attributes.getValue("Implementation-Version");
    log.debug("[getVersion] version='" + version + "'");

    if (version == null) {
        return null;
    }
    if (version.length() < 3) {
        return null;
    }

    if (!(Character.isDigit(version.charAt(0)) && (version.charAt(1) == '.')
            && Character.isDigit(version.charAt(2)))) {
        return null;
    }

    return version;
}

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

public static List<URL> loadJar(String path, URL resource) throws IOException {
    JarURLConnection conn = (JarURLConnection) resource.openConnection();
    JarFile jarFile = conn.getJarFile();
    Enumeration<JarEntry> entries = jarFile.entries();
    List<URL> result = new LinkedList<URL>();

    String p = path;//  w  w  w . j  ava2s . c o m
    if (p.endsWith("/") == false) {
        p = p + "/";
    }

    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        if ((!entry.getName().equals(p))
                && (entry.getName().startsWith(p) || entry.getName().startsWith("WEB-INF/classes/" + p))) {
            URL url = new URL("jar:" + new URL("file", null, jarFile.getName() + "!/" + entry.getName()));
            result.add(url);
        }
    }

    return result;
}

From source file:com.jhash.oimadmin.Utils.java

public static String readFileInJar(JarFile jarFile, JarEntry file) {
    StringBuilder readFileData = new StringBuilder();
    try (BufferedReader jarFileInputStream = new BufferedReader(
            new InputStreamReader(jarFile.getInputStream(file)))) {
        String readLine;//www  .j  a va 2s  . c  om
        while ((readLine = jarFileInputStream.readLine()) != null) {
            readFileData.append(readLine);
            readFileData.append(System.lineSeparator());
        }
    } catch (Exception exception) {
        throw new OIMAdminException("Failed to read file " + file + " in jar file " + jarFile.getName(),
                exception);
    }
    return readFileData.toString();
}