Example usage for java.util.jar JarFile getEntry

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

Introduction

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

Prototype

public ZipEntry getEntry(String name) 

Source Link

Document

Returns the ZipEntry for the given base entry name or null if not found.

Usage

From source file:org.jahia.modules.serversettings.portlets.WebSpherePortletHelper.java

@Override
boolean needsProcessing(JarFile jar) {
    ZipEntry webXml = jar.getEntry("WEB-INF/web.xml");
    if (webXml == null) {
        return false;
    }//  w ww.  j av a2 s  . co m
    boolean doProcess = false;
    InputStream is = null;
    try {
        is = jar.getInputStream(webXml);
        String webXmlContent = IOUtils.toString(is, "UTF-8");
        doProcess = webXmlContent != null
                && !webXmlContent.contains("com.ibm.websphere.portletcontainer.PortletDeploymentEnabled");
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return doProcess;
}

From source file:org.springframework.extensions.config.source.JarConfigSource.java

@Override
protected InputStream getInputStream(String sourceString) {
    InputStream in = null;/*from   w w  w .  j a va  2s  . c o  m*/

    try {
        // make sure the source string is valid
        if (sourceString.startsWith(JAR_PROTOCOL) == false) {
            throw new IllegalArgumentException("sourceString must start with \"" + JAR_PROTOCOL + ":\"");
        }

        int indexSep = sourceString.indexOf(JAR_PATH_SEPARATOR);
        if (indexSep == -1) {
            throw new IllegalArgumentException(
                    "sourceString must contain an entry within the JAR file i.e. jar:file:/[file]!/[entry]");
        }

        // extract the entry part from source string
        String entryStr = sourceString.substring(indexSep + JAR_PATH_SEPARATOR.length());

        // open the JAR file
        URL url = new URL(sourceString);
        URLConnection conn = url.openConnection();
        if (conn instanceof JarURLConnection) {
            // open the jar file, if it does not contain the entry requested a FileNotFound exception
            // is thrown and reported below
            JarFile jar = ((JarURLConnection) conn).getJarFile();
            ZipEntry entry = jar.getEntry(entryStr);
            if (entry != null) {
                in = jar.getInputStream(entry);
            }
        }
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.debug("Failed to obtain input stream to URL: " + sourceString, e);
    }

    return in;
}

From source file:org.jahia.modules.serversettings.portlets.BasePortletHelper.java

boolean portletTldsPresent(JarFile jar) {
    hasPortletTld = jar.getEntry("WEB-INF/portlet.tld") != null;
    hasPortlet2Tld = jar.getEntry("WEB-INF/portlet_2_0.tld") != null;
    return hasPortletTld && hasPortlet2Tld;
}

From source file:org.openecard.addon.PluginDirectoryAlterationListener.java

private InputStream getPluginEntryClass(JarFile jarFile) throws IOException {
    ZipEntry manifest = jarFile.getEntry(MANIFEST_XML);
    if (manifest == null) {
        return null;
    } else {/*w w w  . j  a  v  a2  s.com*/
        return jarFile.getInputStream(manifest);
    }
}

From source file:org.jenkins_ci.update_center.model.MavenArtifact.java

public Manifest getManifest() throws IOException {
    if (manifest == null) {
        try {/* w ww.j a  va 2s  . c om*/
            JarFile jar = new JarFile(file);
            ZipEntry e = jar.getEntry("META-INF/MANIFEST.MF");
            timestamp = e.getTime();
            manifest = jar.getManifest();
            jar.close();
        } catch (IOException x) {
            throw (IOException) new IOException("Failed to open " + file).initCause(x);
        }
    }
    return manifest;
}

From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java

@Test
public void testDefaultPackaging() throws MojoExecutionException, IOException {
    WebJarPackager packager = new WebJarPackager();
    packager.project = mock(MavenProject.class);
    packager.projectHelper = mock(MavenProjectHelper.class);
    when(packager.project.getArtifactId()).thenReturn("test");
    when(packager.project.getVersion()).thenReturn("1.1");
    when(packager.project.getBasedir()).thenReturn(fake);
    packager.buildDirectory = new File("target/junk");
    copy();//from www.j  a  v a 2  s. co m
    packager.packageWebJar = true;
    packager.deployWebJarToWisdom = true;
    packager.execute();
    final File wj = new File(packager.buildDirectory, "test-1.1-webjar.jar");
    assertThat(wj).isFile();
    JarFile jar = new JarFile(wj);
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/less/style.less")).isNotNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/missing")).isNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/coffee/script.coffee")).isNotNull();
    Attributes attributes = jar.getManifest().getMainAttributes();
    assertThat(attributes.getValue("Webjar-Name")).isEqualTo("test");
    assertThat(attributes.getValue("Webjar-Version")).isEqualTo("1.1");
}

From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java

@Test
public void testIncludesCustomization() throws MojoExecutionException, IOException {
    WebJarPackager packager = new WebJarPackager();
    packager.project = mock(MavenProject.class);
    packager.projectHelper = mock(MavenProjectHelper.class);
    when(packager.project.getArtifactId()).thenReturn("test");
    when(packager.project.getVersion()).thenReturn("1.0");
    when(packager.project.getBasedir()).thenReturn(fake);
    packager.buildDirectory = new File("target/junk");
    copy();/*from   w  ww.j a  v  a2  s.  c  o  m*/
    packager.webjar = new WebJar();
    FileSet set = new FileSet();
    set.setDirectory(new File(classes, "assets").getAbsolutePath());
    set.setIncludes(ImmutableList.of("**/coffee/*"));
    packager.webjar.setFileset(set);

    packager.execute();
    final File wj = new File(packager.buildDirectory, "test-1.0-webjar.jar");
    assertThat(wj).isFile();
    JarFile jar = new JarFile(wj);
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.0/missing")).isNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.0/coffee/script.coffee")).isNotNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.0/less/style.less")).isNull();
}

From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java

@Test
public void testExcludesCustomization() throws MojoExecutionException, IOException {
    WebJarPackager packager = new WebJarPackager();
    packager.project = mock(MavenProject.class);
    packager.projectHelper = mock(MavenProjectHelper.class);
    when(packager.project.getArtifactId()).thenReturn("test");
    when(packager.project.getVersion()).thenReturn("1.0");
    when(packager.project.getBasedir()).thenReturn(fake);
    packager.buildDirectory = new File("target/junk");
    copy();/*from  w  w w .  j av  a2 s  . c o  m*/
    packager.webjar = new WebJar();
    FileSet set = new FileSet();
    set.setDirectory(new File(classes, "assets").getAbsolutePath());
    set.setExcludes(ImmutableList.of("**/less/*"));
    packager.webjar.setFileset(set);

    packager.execute();
    final File wj = new File(packager.buildDirectory, "test-1.0-webjar.jar");
    assertThat(wj).isFile();
    JarFile jar = new JarFile(wj);
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.0/missing")).isNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.0/coffee/script.coffee")).isNotNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.0/less/style.less")).isNull();
}

From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java

@Test
public void testNameVersionAndClassifierCustomization() throws MojoExecutionException, IOException {
    WebJarPackager packager = new WebJarPackager();
    packager.project = mock(MavenProject.class);
    packager.projectHelper = mock(MavenProjectHelper.class);
    when(packager.project.getArtifactId()).thenReturn("test");
    when(packager.project.getVersion()).thenReturn("1.0");
    when(packager.project.getBasedir()).thenReturn(fake);
    packager.buildDirectory = new File("target/junk");
    copy();/*  ww  w  .ja  va 2 s  .c  o  m*/
    packager.webjar = new WebJar();
    packager.webjar.setName("library");
    packager.webjar.setVersion("2.0");
    packager.webjar.setClassifier("wb");
    packager.execute();
    final File wj = new File(packager.buildDirectory, "library-2.0-wb.jar");
    assertThat(wj).isFile();
    JarFile jar = new JarFile(wj);
    assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/missing")).isNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/coffee/script.coffee")).isNotNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/less/style.less")).isNotNull();
    Attributes attributes = jar.getManifest().getMainAttributes();
    assertThat(attributes.getValue("Webjar-Name")).isEqualTo("library");
    assertThat(attributes.getValue("Webjar-Version")).isEqualTo("2.0");
}

From source file:com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptorBuilder.java

public GoPluginDescriptor build(File pluginJarFile, boolean isBundledPlugin) {
    if (!pluginJarFile.exists()) {
        throw new RuntimeException(
                String.format("Plugin jar does not exist: %s", pluginJarFile.getAbsoluteFile()));
    }//  w  ww . j  av  a 2s.c o  m
    InputStream pluginXMLStream = null;
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(pluginJarFile);
        ZipEntry entry = jarFile.getEntry(PLUGIN_XML);

        if (entry == null) {
            return GoPluginDescriptor.usingId(pluginJarFile.getName(), pluginJarFile.getAbsolutePath(),
                    getBundleLocation(bundlePathLocation, pluginJarFile.getName()), isBundledPlugin);
        }

        pluginXMLStream = jarFile.getInputStream(entry);
        return GoPluginDescriptorParser.parseXML(pluginXMLStream, pluginJarFile.getAbsolutePath(),
                getBundleLocation(bundlePathLocation, pluginJarFile.getName()), isBundledPlugin);
    } catch (Exception e) {
        LOGGER.warn("Could not load plugin with jar filename:" + pluginJarFile.getName(), e);
        String cause = e.getCause() != null
                ? String.format("%s. Cause: %s", e.getMessage(), e.getCause().getMessage())
                : e.getMessage();
        return GoPluginDescriptor
                .usingId(pluginJarFile.getName(), pluginJarFile.getAbsolutePath(),
                        getBundleLocation(bundlePathLocation, pluginJarFile.getName()), isBundledPlugin)
                .markAsInvalid(Arrays.asList(
                        String.format("Plugin with ID (%s) is not valid: %s", pluginJarFile.getName(), cause)),
                        e);
    } finally {
        IOUtils.closeQuietly(pluginXMLStream);
        closeQuietly(jarFile);
    }
}