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:net.lightbody.bmp.proxy.jetty.util.JarFileResource.java

public synchronized String[] list() {
    if (isDirectory() && _list == null) {
        ArrayList list = new ArrayList(32);

        checkConnection();//from  w w w  . j a  v  a2s  .c om

        JarFile jarFile = _jarFile;
        if (jarFile == null) {
            try {
                jarFile = ((JarURLConnection) ((new URL(_jarUrl)).openConnection())).getJarFile();
            } catch (Exception e) {
                LogSupport.ignore(log, e);
            }
        }

        Enumeration e = jarFile.entries();
        String dir = _urlString.substring(_urlString.indexOf("!/") + 2);
        while (e.hasMoreElements()) {
            JarEntry entry = (JarEntry) e.nextElement();
            String name = entry.getName().replace('\\', '/');
            if (!name.startsWith(dir) || name.length() == dir.length())
                continue;
            String listName = name.substring(dir.length());
            int dash = listName.indexOf('/');
            if (dash >= 0) {
                listName = listName.substring(0, dash + 1);
                if (list.contains(listName))
                    continue;
            }

            list.add(listName);
        }

        _list = new String[list.size()];
        list.toArray(_list);
    }
    return _list;
}

From source file:org.apache.sling.tooling.support.source.impl.SourceReferencesServlet.java

private void collectMavenSourceRerefences(JSONWriter w, URL entry) throws IOException, JSONException {

    InputStream wrappedIn = entry.openStream();
    try {/*from   w  w w  .  ja v  a2  s. co m*/
        JarInputStream jarIs = new JarInputStream(wrappedIn);
        JarEntry jarEntry;
        while ((jarEntry = jarIs.getNextJarEntry()) != null) {
            String entryName = jarEntry.getName();
            if (entryName.startsWith("META-INF/maven/") && entryName.endsWith("/pom.properties")) {
                writeMavenGav(w, jarIs);
            }
        }
    } finally {
        IOUtils.closeQuietly(wrappedIn);
    }
}

From source file:org.jahia.admin.components.AssemblerTask.java

private boolean needRewriting(File source) throws FileNotFoundException, IOException {
    final JarInputStream jarIn = new JarInputStream(new FileInputStream(source));
    String webXml = null;//from   w ww  .jav a 2s  .co  m
    JarEntry jarEntry;
    try {
        // Read the source archive entry by entry
        while ((jarEntry = jarIn.getNextJarEntry()) != null) {
            if (Assembler.SERVLET_XML.equals(jarEntry.getName())) {
                webXml = IOUtils.toString(jarIn);
            }
            jarIn.closeEntry();
        }
    } finally {
        jarIn.close();
    }

    return webXml == null || !webXml.contains(Assembler.DISPATCH_SERVLET_CLASS);
}

From source file:org.apache.pluto.util.assemble.io.AssemblyStreamTest.java

protected void verifyAssembly(File warFile) throws Exception {
    PlutoWebXmlRewriter webXmlRewriter = null;
    PortletAppDescriptorService portletSvc = new PortletAppDescriptorServiceImpl();
    int entryCount = 0;
    ByteArrayOutputStream portletXmlBytes = new ByteArrayOutputStream();
    ByteArrayOutputStream webXmlBytes = new ByteArrayOutputStream();
    PortletApplicationDefinition portletApp = null;

    JarInputStream assembledWarIn = new JarInputStream(new FileInputStream(warFile));
    JarEntry tempEntry;

    while ((tempEntry = assembledWarIn.getNextJarEntry()) != null) {
        entryCount++;//  w w w  .j a  v  a2 s  . co m

        if (Assembler.PORTLET_XML.equals(tempEntry.getName())) {
            IOUtils.copy(assembledWarIn, portletXmlBytes);
            portletApp = portletSvc.read("test", "/test",
                    new ByteArrayInputStream(portletXmlBytes.toByteArray()));
        }
        if (Assembler.SERVLET_XML.equals(tempEntry.getName())) {
            IOUtils.copy(assembledWarIn, webXmlBytes);
            webXmlRewriter = new PlutoWebXmlRewriter(new ByteArrayInputStream(webXmlBytes.toByteArray()));
        }
    }

    assertTrue("Assembled WAR file was empty.", entryCount > 0);
    assertNotNull("Web Application Descripter was null.", webXmlRewriter);
    assertNotNull("Portlet Application Descriptor was null.", portletApp);
    assertTrue("Portlet Application Descriptor doesn't define any portlets.",
            portletApp.getPortlets().size() > 0);
    assertTrue("Web Application Descriptor doesn't define any servlets.", webXmlRewriter.hasServlets());
    assertTrue("Web Application Descriptor doesn't define any servlet mappings.",
            webXmlRewriter.hasServletMappings());

    PortletDefinition portlet = (PortletDefinition) portletApp.getPortlets().iterator().next();
    assertTrue("Unable to retrieve test portlet named [" + testPortletName + "]",
            portlet.getPortletName().equals(testPortletName));

    String servletClassName = webXmlRewriter.getServletClass(testPortletName);
    assertNotNull("Unable to retrieve portlet dispatch for portlet named [" + testPortletName + "]",
            servletClassName);
    assertEquals("Dispatcher servlet incorrect for test portlet [" + testPortletName + "]",
            Assembler.DISPATCH_SERVLET_CLASS, servletClassName);
}

From source file:com.cloudbees.sdk.ArtifactInstallFactory.java

/**
 * Installs the given artifact and all its transitive dependencies
 *///from   ww w.ja  v  a 2s  .  co  m
public GAV install(GAV gav) throws Exception {
    Artifact a = toArtifact(gav);
    List<ArtifactResult> artifactResults = repo.get().resolveDependencies(gav).getArtifactResults();

    Plugin plugin = new Plugin();
    List<CommandProperties> command = plugin.getProperties();

    List<String> jars = plugin.getJars();
    List<URL> urls = new ArrayList<URL>();
    for (ArtifactResult artifactResult : artifactResults) {
        URL artifactURL = artifactResult.getArtifact().getFile().toURI().toURL();
        urls.add(artifactURL);
        jars.add(artifactResult.getArtifact().getFile().getAbsolutePath());
    }
    ClassLoader cl = createClassLoader(urls, getClass().getClassLoader());

    for (ArtifactResult artifactResult : artifactResults) {
        if (toString(artifactResult.getArtifact()).equals(toString(a))) {
            plugin.setArtifact(new GAV(artifactResult.getArtifact().toString()).toString());
            //               System.out.println("Analysing... " + plugin.getArtifact());

            JarFile jarFile = new JarFile(artifactResult.getArtifact().getFile());
            Enumeration<JarEntry> e = jarFile.entries();
            while (e.hasMoreElements()) {
                JarEntry entry = e.nextElement();
                if (entry.getName().endsWith(".class")) {
                    String className = entry.getName().replace('/', '.').substring(0,
                            entry.getName().length() - 6);
                    Class c = Class.forName(className, false, cl);
                    findCommand(true, command, c);
                }
            }

        }
    }

    XStream xStream = new XStream();
    xStream.processAnnotations(Plugin.class);
    xStream.processAnnotations(CommandProperties.class);
    //        System.out.println(xStream.toXML(plugin));

    File xmlFile = new File(directoryStructure.getPluginDir(), a.getArtifactId() + ".bees");
    OutputStreamWriter fos = null;
    try {
        xmlFile.getParentFile().mkdirs();
        FileOutputStream outputStream = new FileOutputStream(xmlFile);
        fos = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
        xStream.toXML(plugin, fos);
    } finally {
        IOUtils.closeQuietly(fos);
    }

    return new GAV(plugin.getArtifact());
}

From source file:se.crisp.codekvast.support.web.config.WebjarVersionFilter.java

private void analyzeJar(URL jarUrl, Pattern pattern) throws IOException {
    // Look for entries that match the pattern...
    JarInputStream inputStream = new JarInputStream(jarUrl.openStream());

    JarEntry jarEntry = inputStream.getNextJarEntry();
    while (jarEntry != null) {
        log.trace("Considering {}", jarEntry);

        Matcher matcher = pattern.matcher(jarEntry.getName());
        if (matcher.matches()) {
            String key = matcher.group(1);
            String value = matcher.group(2);
            versions.put(key, value);/*from w w  w . java  2 s.  c  o m*/
            log.debug("{} is a webjar, adding {}={} to map", basename(jarUrl.getPath()), key, value);
            return;
        }
        jarEntry = inputStream.getNextJarEntry();
    }
}

From source file:org.mule.util.scan.ClasspathScanner.java

protected <T> Set<Class<T>> processJarUrl(URL url, String basepath, Class<T> clazz, int flags)
        throws IOException {
    Set<Class<T>> set = new HashSet<Class<T>>();
    String path = url.getFile().substring(5, url.getFile().indexOf("!"));
    // We can't URLDecoder.decode(path) since some encoded chars are allowed on file uris
    path = path.replaceAll("%20", " ");
    JarFile jar = new JarFile(path);

    for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        JarEntry entry = entries.nextElement();
        if (entry.getName().startsWith(basepath) && entry.getName().endsWith(".class")) {
            try {
                String name = entry.getName();
                // Ignore anonymous and inner classes
                if (name.contains("$") && !hasFlag(flags, INCLUDE_INNER)) {
                    continue;
                }//from   w  ww  . j  a v  a  2s.  c  o m

                URL classURL = classLoader.getResource(name);
                InputStream classStream = classURL.openStream();
                ClassReader reader = new ClosableClassReader(classStream);

                ClassScanner visitor = getScanner(clazz);
                reader.accept(visitor, 0);
                if (visitor.isMatch()) {
                    @SuppressWarnings("unchecked")
                    Class<T> loadedClass = (Class<T>) loadClass(visitor.getClassName());
                    addClassToSet(loadedClass, set, flags);
                }
            } catch (Exception e) {
                if (logger.isDebugEnabled()) {
                    Throwable t = ExceptionHelper.getRootException(e);
                    logger.debug(String.format("%s: caused by: %s", e.toString(), t.toString()));
                }
            }
        }
    }
    jar.close();

    return set;
}

From source file:org.simplericity.jettyconsole.creator.DefaultCreator.java

private void copyIntoLib(URL url, File libDirectory, Set<String> tldJars, List<String> options)
        throws CreatorExecutionException {
    String name = url.getPath().substring(url.getPath().lastIndexOf("/"));

    try {/* w w w.j  a v a  2  s .c  o  m*/
        File file = new File(libDirectory, name);
        FileOutputStream out = new FileOutputStream(file);
        IOUtils.copy(url.openStream(), out);
        out.close();

        try (JarFile jarFile = new JarFile(file)) {

            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();

                if (entry.getName().startsWith("META-INF/") && entry.getName().endsWith(".tld")) {
                    tldJars.add(file.getName());
                }
                if (entry.getName().startsWith("META-INF/services")
                        && entry.getName().endsWith("JettyConsolePlugin/options.txt")) {
                    options.add(IOUtils.toString(jarFile.getInputStream(entry)));
                }
            }
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.rhq.enterprise.server.plugin.pc.perspective.PerspectiveServerPluginManager.java

private void undeployWars(ServerPluginEnvironment env) {
    String name = null;//  www  . j av a  2  s . c om
    try {
        JarFile plugin = new JarFile(env.getPluginUrl().getFile());
        try {
            for (JarEntry entry : Collections.list(plugin.entries())) {
                name = entry.getName();
                if (name.toLowerCase().endsWith(".war")) {
                    undeployWar(getDeployFile(env, entry.getName()));
                }
            }
        } finally {
            plugin.close();
        }
    } catch (Exception e) {
        this.log.error("Failed to deploy " + env.getPluginKey().getPluginName() + "#" + name, e);
    }
}

From source file:org.apache.sling.maven.bundlesupport.AbstractBundleDeployMojo.java

/**
 * Change the version in jar/*w  w  w  .j av a 2 s.co m*/
 * 
 * @param newVersion
 * @param file
 * @return
 * @throws MojoExecutionException
 */
protected File changeVersion(File file, String oldVersion, String newVersion) throws MojoExecutionException {
    String fileName = file.getName();
    int pos = fileName.indexOf(oldVersion);
    fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + oldVersion.length());

    JarInputStream jis = null;
    JarOutputStream jos;
    OutputStream out = null;
    JarFile sourceJar = null;
    try {
        // now create a temporary file and update the version
        sourceJar = new JarFile(file);
        final Manifest manifest = sourceJar.getManifest();
        manifest.getMainAttributes().putValue("Bundle-Version", newVersion);

        jis = new JarInputStream(new FileInputStream(file));
        final File destJar = new File(file.getParentFile(), fileName);
        out = new FileOutputStream(destJar);
        jos = new JarOutputStream(out, manifest);

        jos.setMethod(JarOutputStream.DEFLATED);
        jos.setLevel(Deflater.BEST_COMPRESSION);

        JarEntry entryIn = jis.getNextJarEntry();
        while (entryIn != null) {
            JarEntry entryOut = new JarEntry(entryIn.getName());
            entryOut.setTime(entryIn.getTime());
            entryOut.setComment(entryIn.getComment());
            jos.putNextEntry(entryOut);
            if (!entryIn.isDirectory()) {
                IOUtils.copy(jis, jos);
            }
            jos.closeEntry();
            jis.closeEntry();
            entryIn = jis.getNextJarEntry();
        }

        // close the JAR file now to force writing
        jos.close();
        return destJar;
    } catch (IOException ioe) {
        throw new MojoExecutionException("Unable to update version in jar file.", ioe);
    } finally {
        if (sourceJar != null) {
            try {
                sourceJar.close();
            } catch (IOException ex) {
                // close
            }
        }
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(out);
    }

}