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:ffx.FFXClassLoader.java

/**
 * {@inheritDoc}//from w  w  w . j a v a2s  .  com
 *
 * Returns the URL of the given resource searching first if it exists among
 * the extension JARs given in constructor.
 */
@Override
public URL findResource(String name) {
    if (!extensionsLoaded) {
        loadExtensions();
    }

    if (name.equals("List all scripts")) {
        listScripts();
    }

    if (extensionJars != null) {
        for (JarFile extensionJar : extensionJars) {
            JarEntry jarEntry = extensionJar.getJarEntry(name);
            if (jarEntry != null) {
                String path = "jar:file:" + extensionJar.getName() + "!/" + jarEntry.getName();
                try {
                    return new URL(path);
                } catch (MalformedURLException ex) {
                    System.out.println(path + "\n" + ex.toString());
                }
            }
        }
    }

    return super.findResource(name);
}

From source file:org.apache.struts2.osgi.host.BaseOsgiHost.java

/**
 * Gets the version used to export the packages. it tries to get it from MANIFEST.MF, or the file name
 *//*from   w w w. j a  va2 s .c o m*/
protected String getVersion(URL url) {
    if ("jar".equals(url.getProtocol())) {
        try {
            FileManager fileManager = ServletActionContext.getContext().getInstance(FileManagerFactory.class)
                    .getFileManager();
            JarFile jarFile = new JarFile(new File(fileManager.normalizeToFileProtocol(url).toURI()));
            Manifest manifest = jarFile.getManifest();
            if (manifest != null) {
                String version = manifest.getMainAttributes().getValue("Bundle-Version");
                if (StringUtils.isNotBlank(version)) {
                    return getVersionFromString(version);
                }
            } else {
                //try to get the version from the file name
                return getVersionFromString(jarFile.getName());
            }
        } catch (Exception e) {
            if (LOG.isErrorEnabled()) {
                LOG.error("Unable to extract version from [#0], defaulting to '1.0.0'", url.toExternalForm());
            }
        }
    }
    return "1.0.0";
}

From source file:com.photon.phresco.util.PhrescoDynamicLoader.java

public InputStream getResourceAsStream(String fileName) throws PhrescoException {
    InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
    if (resourceAsStream != null) {
        return resourceAsStream;
    }//from  ww  w.  j a v  a  2 s  . co m
    List<Artifact> artifacts = new ArrayList<Artifact>();
    Artifact foundArtifact = null;
    String destFile = "";
    JarFile jarfile = null;
    for (ArtifactGroup plugin : plugins) {
        List<ArtifactInfo> versions = plugin.getVersions();
        for (ArtifactInfo artifactInfo : versions) {

            foundArtifact = createArtifact(plugin.getGroupId(), plugin.getArtifactId(), "jar",
                    artifactInfo.getVersion());
            artifacts.add(foundArtifact);
        }
    }
    try {
        URL artifactURLs = MavenArtifactResolver.resolveSingleArtifact(repoInfo.getGroupRepoURL(),
                repoInfo.getRepoUserName(), repoInfo.getRepoPassword(), artifacts);
        File jarFile = new File(artifactURLs.toURI());
        if (jarFile.getName().equals(foundArtifact.getArtifactId() + "-" + foundArtifact.getVersion() + "."
                + foundArtifact.getType())) {
            jarfile = new JarFile(jarFile);
            for (Enumeration<JarEntry> em = jarfile.entries(); em.hasMoreElements();) {
                JarEntry jarEntry = em.nextElement();
                if (jarEntry.getName().endsWith(fileName)) {
                    destFile = jarEntry.getName();
                }
            }
        }
        if (StringUtils.isNotEmpty(destFile)) {
            ZipEntry entry = jarfile.getEntry(destFile);
            return jarfile.getInputStream(entry);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new PhrescoException(e);
    }
    return null;
}

From source file:org.apache.struts2.osgi.BaseOsgiHost.java

/**
 * Gets the version used to export the packages. it tries to get it from MANIFEST.MF, or the file name
 *///ww  w . j  a  v  a2s .  co m
protected String getVersion(URL url) {
    if ("jar".equals(url.getProtocol())) {
        try {
            JarFile jarFile = new JarFile(new File(URLUtil.normalizeToFileProtocol(url).toURI()));
            Manifest manifest = jarFile.getManifest();
            if (manifest != null) {
                String version = manifest.getMainAttributes().getValue("Bundle-Version");
                if (StringUtils.isNotBlank(version)) {
                    return getVersionFromString(version);
                }
            } else {
                //try to get the version from the file name
                return getVersionFromString(jarFile.getName());
            }
        } catch (Exception e) {
            if (LOG.isErrorEnabled())
                LOG.error("Unable to extract version from [#0], defaulting to '1.0.0'", url.toExternalForm());

        }
    }

    return "1.0.0";
}

From source file:com.twosigma.beaker.autocomplete.ClasspathScanner.java

private boolean findClasses(File root, File file, boolean includeJars) {
    if (file != null && file.isDirectory()) {
        File[] lf = file.listFiles();
        if (lf != null)
            for (File child : lf) {
                if (!findClasses(root, child, includeJars)) {
                    return false;
                }// w  ww .  j  a v  a2s  .c  o m
            }
    } else {
        if (file.getName().toLowerCase().endsWith(".jar") && includeJars) {
            JarFile jar = null;
            try {
                jar = new JarFile(file);
            } catch (Exception ex) {
            }
            if (jar != null) {
                try {
                    Manifest mf = jar.getManifest();
                    if (mf != null) {
                        String cp = mf.getMainAttributes().getValue("Class-Path");
                        if (StringUtils.isNotEmpty(cp)) {
                            for (String fn : cp.split(" ")) {
                                File child = new File(
                                        file.getParent() + System.getProperty("file.separator") + fn);
                                if (child.getAbsolutePath().equals(jar.getName())) {
                                    continue; //skip bad jars, that contain references to themselves in MANIFEST.MF
                                }
                                if (child.exists()) {
                                    if (!findClasses(root, child, includeJars)) {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                }

                Enumeration<JarEntry> entries = jar.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    String name = entry.getName();
                    int extIndex = name.lastIndexOf(".class");
                    if (extIndex > 0 && !name.contains("$")) {
                        String cname = name.substring(0, extIndex).replace("/", ".");
                        int pIndex = cname.lastIndexOf('.');
                        if (pIndex > 0) {
                            String pname = cname.substring(0, pIndex);
                            cname = cname.substring(pIndex + 1);
                            if (!packages.containsKey(pname))
                                packages.put(pname, new ArrayList<String>());
                            packages.get(pname).add(cname);
                        }
                    }
                }
            }
        } else if (file.getName().toLowerCase().endsWith(".class")) {
            String cname = createClassName(root, file);
            if (!cname.contains("$")) {
                int pIndex = cname.lastIndexOf('.');
                if (pIndex > 0) {
                    String pname = cname.substring(0, pIndex + 1);
                    cname = cname.substring(pIndex);
                    if (!packages.containsKey(pname))
                        packages.put(pname, new ArrayList<String>());
                    packages.get(pname).add(cname);
                }
            }
        } else {
            examineFile(root, file);
        }
    }

    return true;
}

From source file:com.twosigma.beakerx.autocomplete.ClasspathScanner.java

private boolean findClasses(File root, File file, boolean includeJars) {
    if (file != null && file.isDirectory()) {
        File[] lf = file.listFiles();
        if (lf != null)
            for (File child : lf) {
                if (!findClasses(root, child, includeJars)) {
                    return false;
                }//from w ww.j  ava2 s .c  o m
            }
    } else {
        if (file.getName().toLowerCase().endsWith(".jar") && includeJars) {
            JarFile jar = null;
            try {
                jar = new JarFile(file);
            } catch (Exception ex) {
            }
            if (jar != null) {
                try {
                    Manifest mf = jar.getManifest();
                    if (mf != null) {
                        String cp = mf.getMainAttributes().getValue("Class-Path");
                        if (StringUtils.isNotEmpty(cp)) {
                            for (String fn : cp.split(" ")) {
                                if (!fn.equals(".")) {
                                    File child = new File(
                                            file.getParent() + System.getProperty("file.separator") + fn);
                                    if (child.getAbsolutePath().equals(jar.getName())) {
                                        continue; //skip bad jars, that contain references to themselves in MANIFEST.MF
                                    }
                                    if (child.exists()) {
                                        if (!findClasses(root, child, includeJars)) {
                                            return false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                }

                Enumeration<JarEntry> entries = jar.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    String name = entry.getName();
                    int extIndex = name.lastIndexOf(".class");
                    if (extIndex > 0 && !name.contains("$")) {
                        String cname = name.substring(0, extIndex).replace("/", ".");
                        int pIndex = cname.lastIndexOf('.');
                        if (pIndex > 0) {
                            String pname = cname.substring(0, pIndex);
                            cname = cname.substring(pIndex + 1);
                            if (!packages.containsKey(pname))
                                packages.put(pname, new ArrayList<String>());
                            packages.get(pname).add(cname);
                        }
                    }
                }
            }
        } else if (file.getName().toLowerCase().endsWith(".class")) {
            String cname = createClassName(root, file);
            if (!cname.contains("$")) {
                int pIndex = cname.lastIndexOf('.');
                if (pIndex > 0) {
                    String pname = cname.substring(0, pIndex + 1);
                    cname = cname.substring(pIndex);
                    if (!packages.containsKey(pname))
                        packages.put(pname, new ArrayList<String>());
                    packages.get(pname).add(cname);
                }
            }
        } else {
            examineFile(root, file);
        }
    }

    return true;
}

From source file:UnpackedJarFile.java

public static URL createJarURL(JarFile jarFile, String path) throws MalformedURLException {
    if (jarFile instanceof NestedJarFile) {
        NestedJarFile nestedJar = (NestedJarFile) jarFile;
        if (nestedJar.isUnpacked()) {
            JarFile baseJar = nestedJar.getBaseJar();
            String basePath = nestedJar.getBasePath();
            if (baseJar instanceof UnpackedJarFile) {
                File baseDir = ((UnpackedJarFile) baseJar).getBaseDir();
                baseDir = new File(baseDir, basePath);
                return new File(baseDir, path).toURL();
            }/*from www. j a v a 2s  .c  o m*/
        }
    }

    if (jarFile instanceof UnpackedJarFile) {
        File baseDir = ((UnpackedJarFile) jarFile).getBaseDir();
        return new File(baseDir, path).toURL();
    } else {
        String urlString = "jar:" + new File(jarFile.getName()).toURL() + "!/" + path;
        if (jarUrlRewrite) {
            // To prevent the lockout of archive, instead of returning a jar url, write the content to a
            // temp file and return the url of that file.
            File tempFile = null;
            try {
                tempFile = toTempFile(new URL(urlString));
            } catch (IOException e) {
                // The JarEntry does not exist!
                // Return url of a file that does not exist.
                try {
                    tempFile = createTempFile();
                    tempFile.delete();
                } catch (IOException ignored) {
                }
            }
            return tempFile.toURL();
        } else {
            return new URL(urlString);
        }
    }
}

From source file:plaid.compilerjava.CompilerCore.java

private void handleFileInJar(JarFile jarFile, JarEntry entry, PackageRep plaidpath) throws Exception {
    if (!entry.getName().endsWith(".class")
            || (entry.getName().contains("$") && !entry.getName().contains("plaid")))
        return;/*from  w w  w  .  ja va  2s .c  o  m*/

    String entryName = entry.getName();
    // Enumerating entries in a Jar seems to always return paths containing slashes, even on Windows.
    // So do *not* use file.seperator here. TODO: Check that this is actually correct.
    String className = entryName.substring(0, entryName.length() - 6).replace("/", ".");

    URL[] loaderDir = { new URL("jar:file://" + jarFile.getName() + "!/") };
    ClassLoader loader = new URLClassLoader(loaderDir, Thread.currentThread().getContextClassLoader());

    try {
        Class<?> classRep = loader.loadClass(className);
        handleClassInPlaidPath(classRep, className, plaidpath);
    } catch (NoClassDefFoundError e) {
        System.err.println("Warning: Loading class \"" + className + "\" failed.");
    }
}

From source file:org.apache.archiva.rest.services.DefaultBrowseService.java

private void closeQuietly(JarFile jarFile) {
    if (jarFile != null) {
        try {//from w  w  w  . j  a  v  a2  s.  c o  m
            jarFile.close();
        } catch (IOException e) {
            log.warn("ignore error closing jarFile {}", jarFile.getName());
        }
    }
}

From source file:com.datatorrent.stram.webapp.TypeGraph.java

public TypeGraphVertex addNode(JarEntry jarEntry, JarFile jar) throws IOException {
    return addNode(jar.getInputStream(jarEntry), jar.getName());
}