Example usage for java.util.jar JarEntry getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

From source file:io.sightly.tck.TCK.java

/**
 * Reads the embedded test definition files and provides a list of {@link JSONObject}.
 *
 * @return the list of test definitions as JSON objects
 *///from  w  w  w .ja v  a2  s . c  o  m
public List<JSONObject> getTestDefinitions() {
    if (testDefinitions.size() == 0) {
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String entryName = entry.getName();
            if (entryName.startsWith(TEST_DEFINITIONS_PATH)
                    && entryName.length() > TEST_DEFINITIONS_PATH.length()) {
                StringBuilder sb = new StringBuilder();
                try {
                    InputStream testDefinitionStream = jarFile.getInputStream(entry);
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(testDefinitionStream, "UTF-8"));
                    String line;

                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    testDefinitions.add(new JSONObject(sb.toString()));
                    reader.close();
                } catch (IOException e) {
                    LOG.error("Skipping entry " + entryName, e);
                }
            }
        }
    }
    return testDefinitions;
}

From source file:org.silverpeas.applicationbuilder.ReadOnlyArchive.java

/**
 * Gets the available entries in the archive. It also opens the archive for reading.
 *
 * @return the entries of this archive//from  www . j a  va 2 s . co m
 * @since 1.0
 * @roseuid 3AAFB0770391
 */
public ApplicationBuilderItem[] getEntries() {
    if (getJar() == null) {
        return null;
    }
    List<ApplicationBuilderItem> entries = new ArrayList<ApplicationBuilderItem>(getJar().size());
    for (Enumeration<JarEntry> e = getJar().entries(); e.hasMoreElements();) {
        JarEntry jarEntry = e.nextElement();
        if (!jarEntry.isDirectory()) {
            File oneFile = new File(jarEntry.getName());
            ApplicationBuilderItem item = new ApplicationBuilderItem(oneFile.getParent(), oneFile.getName());
            item.setSize(jarEntry.getSize());
            entries.add(item);
        }
    }
    return entries.toArray(new ApplicationBuilderItem[entries.size()]);
}

From source file:com.eviware.soapui.plugins.JarClassLoader.java

private void addLibrariesIn(JarFile jarFile) throws IOException {
    if (containsLibraries(jarFile)) {
        File libDirectory = Tools.createTemporaryDirectory();
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            if (isLibrary(jarEntry)) {
                String fileName = jarEntry.getName().substring(LIB_PREFIX.length());
                File outputFile = new File(libDirectory, fileName);
                FileUtils.copyInputStreamToFile(jarFile.getInputStream(jarEntry), outputFile);
                this.addURL(outputFile.toURI().toURL());
            }/*from   w  w w .  j a  va2  s.  co  m*/
        }
    }
}

From source file:com.thoughtworks.go.util.NestedJarClassLoader.java

private URL[] enumerateJar(URL urlOfJar) {
    LOGGER.debug("Enumerating jar: {}", urlOfJar);
    List<URL> urls = new ArrayList<>();
    urls.add(urlOfJar);/*  ww w  .  j  a  va 2s  .  c o  m*/
    try {
        JarInputStream jarStream = new JarInputStream(urlOfJar.openStream());
        JarEntry entry;
        while ((entry = jarStream.getNextJarEntry()) != null) {
            if (!entry.isDirectory() && entry.getName().endsWith(".jar")) {
                urls.add(expandJarAndReturnURL(jarStream, entry));
            }
        }
    } catch (IOException e) {
        LOGGER.error("Failed to enumerate jar {}", urlOfJar, e);
    }
    return urls.toArray(new URL[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;
                }/*from  www .  j  a  v a  2s. 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:at.tuwien.minimee.migration.engines.MonitorEngineTOPDefault.java

/**
 * Copies resource file 'from' from destination 'to' and set execution permission.
 * /*from w w w  .ja  v  a2  s . c om*/
 * @param from
 * @param to
 * @throws Exception
 */
protected void copyFile(String from, String to, String workingDirectory) throws Exception {

    //
    // copy the shell script to the working directory
    //
    URL monitorCallShellScriptUrl = Thread.currentThread().getContextClassLoader().getResource(from);
    File f = new File(monitorCallShellScriptUrl.getFile());
    String directoryPath = f.getAbsolutePath();
    // if the application was created as exploded archive, the absolute path is a real filename
    String fileName = from;
    InputStream in = null;
    if (directoryPath.indexOf(".jar!") > -1) {
        // this class is not in an exploded archive, extract the filename  
        URL urlJar = new URL(
                directoryPath.substring(directoryPath.indexOf("file:"), directoryPath.indexOf('!')));

        JarFile jf = new JarFile(urlJar.getFile());
        JarEntry je = jf.getJarEntry(from);
        fileName = je.getName();
        in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    } else {
        in = new FileInputStream(f);
    }

    File outScriptFile = new File(to);

    FileOutputStream fos = new FileOutputStream(outScriptFile);
    int nextChar;
    while ((nextChar = in.read()) != -1)
        fos.write(nextChar);

    fos.flush();
    fos.close();

    //
    // This seems kind of hard core, but we have to set execution rights for the shell script, 
    // otherwise we wouldn't be allowed to execute it.
    // The Java-way with FilePermission didn't work for some reason.
    //
    try {
        LinuxCommandExecutor cmdExecutor = new LinuxCommandExecutor();
        cmdExecutor.setWorkingDirectory(workingDirectory);

        cmdExecutor.runCommand("chmod 777 " + to);
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.izforge.izpack.compiler.packager.impl.AbstractPackagerTest.java

/**
 * Helper to return a stream to the content of a jar entry.
 *
 * @param name the name of the entry/*from w w  w  .  ja v  a 2s.  c om*/
 * @param jar  the jar
 * @return a stream to the content
 * @throws IOException for any I/O error
 */
private InputStream getJarEntry(String name, File jar) throws IOException {
    JarInputStream input = new JarInputStream(new FileInputStream(jar));
    JarEntry entry;
    while ((entry = input.getNextJarEntry()) != null) {
        if (entry.getName().equals(name)) {
            return input;
        }
    }
    fail("Failed to find jar entry: " + name);
    return null;
}

From source file:com.qualogy.qafe.web.css.util.CssProvider.java

private Map<String, InputStream> findResourceInFile(File resourceFile, String fileNamePattern)
        throws IOException {
    Map<String, InputStream> result = new TreeMap<String, InputStream>();
    if (resourceFile.canRead() && resourceFile.getAbsolutePath().endsWith(".jar")) {
        JarFile jarFile = new JarFile(resourceFile);
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry singleEntry = entries.nextElement();
            if (singleEntry.getName().matches(fileNamePattern)) {
                result.put(jarFile.getName() + "/" + singleEntry.getName(),
                        jarFile.getInputStream(singleEntry));
            }//  w ww . j av a2 s  . c o m
        }
    }
    return result;
}

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

private String getVersionFromPluginJarManifest(URL pluginJarUrl) throws IOException {
    JarInputStream jarInputStream = new JarInputStream(pluginJarUrl.openStream());
    Manifest manifest = jarInputStream.getManifest();
    if (manifest == null) {
        // BZ 682116 (ips, 03/25/11): The manifest file is not in the standard place as the 2nd entry of the JAR,
        // but we want to be flexible and support JARs that have a manifest file somewhere else, so scan the entire
        // JAR for one.
        JarEntry jarEntry;
        while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
            if (JarFile.MANIFEST_NAME.equalsIgnoreCase(jarEntry.getName())) {
                manifest = new Manifest(jarInputStream);
                break;
            }//  ww  w  . j  av  a 2s. c  om
        }
    }
    try {
        jarInputStream.close();
    } catch (IOException e) {
        LOG.error("Failed to close plugin jar input stream for plugin jar [" + pluginJarUrl + "].", e);
    }
    if (manifest != null) {
        Attributes attributes = manifest.getMainAttributes();
        return attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
    } else {
        return null;
    }
}

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.  ja  v a2 s  . com
            }
    } 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;
}