Example usage for java.util.jar JarInputStream getManifest

List of usage examples for java.util.jar JarInputStream getManifest

Introduction

In this page you can find the example usage for java.util.jar JarInputStream getManifest.

Prototype

public Manifest getManifest() 

Source Link

Document

Returns the Manifest for this JAR file, or null if none.

Usage

From source file:org.apache.hadoop.hbase.mapreduce.hadoopbackport.TestJarFinder.java

@Test
public void testExistingManifest() throws Exception {
    File dir = new File(System.getProperty("test.build.dir", "target/test-dir"),
            TestJarFinder.class.getName() + "-testExistingManifest");
    delete(dir);/* ww  w .  j  av  a 2  s  .  c  o m*/
    dir.mkdirs();

    File metaInfDir = new File(dir, "META-INF");
    metaInfDir.mkdirs();
    File manifestFile = new File(metaInfDir, "MANIFEST.MF");
    Manifest manifest = new Manifest();
    OutputStream os = new FileOutputStream(manifestFile);
    manifest.write(os);
    os.close();

    File propsFile = new File(dir, "props.properties");
    Writer writer = new FileWriter(propsFile);
    new Properties().store(writer, "");
    writer.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream zos = new JarOutputStream(baos);
    JarFinder.jarDir(dir, "", zos);
    JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Assert.assertNotNull(jis.getManifest());
    jis.close();
}

From source file:ru.codeinside.adm.ui.UploadDeployer.java

private boolean hasApiServer(String supportedInterface, JarInputStream jarStream)
        throws IOException, SAXException, ParserConfigurationException {
    for (String xmlPath : jarStream.getManifest().getMainAttributes().getValue("Service-Component")
            .split(",")) {
        ZipEntry nextZipEntry = jarStream.getNextEntry();
        while (nextZipEntry != null && !xmlPath.equals(nextZipEntry.getName())) {
            nextZipEntry = jarStream.getNextEntry();
        }// w  ww. j  a  va  2 s . com
        Document xmlDoc = readXml(jarStream);

        Node node = getChildForNlByNames(xmlDoc.getChildNodes(), "component", "service", "provide");
        if (node != null) {
            String interfaceValue = node.getAttributes().getNamedItem("interface").getNodeValue();

            if (StringUtils.isNotEmpty(interfaceValue) && supportedInterface.equals(interfaceValue.trim())) {
                return true;
            }
        }
    }
    return false;
}

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;/*from   w  w w. j av a 2s  .  com*/
        while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
            if (JarFile.MANIFEST_NAME.equalsIgnoreCase(jarEntry.getName())) {
                manifest = new Manifest(jarInputStream);
                break;
            }
        }
    }
    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:org.jahia.modules.serversettings.portlets.BasePortletHelper.java

/**
 * Returns a file descriptor for the modified (prepared) portlet WAR file.
 * /* w  w w  . j  a  v  a 2s  .c  o m*/
 * @param sourcePortletWar
 *            the source portlet WAR file
 * @return a file descriptor for the modified (prepared) portlet WAR file
 * @throws IOException
 *             in case of processing error
 */
public File process(File sourcePortletWar) throws IOException {
    JarFile jar = new JarFile(sourcePortletWar);
    File dest = new File(FilenameUtils.getFullPathNoEndSeparator(sourcePortletWar.getPath()),
            FilenameUtils.getBaseName(sourcePortletWar.getName()) + ".war");
    try {
        boolean needsServerSpecificProcessing = needsProcessing(jar);
        if (portletTldsPresent(jar) && !needsServerSpecificProcessing) {
            return sourcePortletWar;
        }
        jar.close();
        final JarInputStream jarIn = new JarInputStream(new FileInputStream(sourcePortletWar));
        final Manifest manifest = jarIn.getManifest();
        final JarOutputStream jarOut;
        if (manifest != null) {
            jarOut = new JarOutputStream(new FileOutputStream(dest), manifest);
        } else {
            jarOut = new JarOutputStream(new FileOutputStream(dest));
        }

        try {
            copyEntries(jarIn, jarOut);

            process(jarIn, jarOut);

            if (!hasPortletTld) {
                addToJar("META-INF/portlet-resources/portlet.tld", "WEB-INF/portlet.tld", jarOut);
            }
            if (!hasPortlet2Tld) {
                addToJar("META-INF/portlet-resources/portlet_2_0.tld", "WEB-INF/portlet_2_0.tld", jarOut);
            }
        } finally {
            jarIn.close();
            jarOut.close();
            FileUtils.deleteQuietly(sourcePortletWar);
        }
        return dest;
    } finally {
        jar.close();
    }
}

From source file:org.pentaho.osgi.platform.webjars.WebjarsURLConnectionTest.java

@Test
public void testClosingStream() throws IOException {
    WebjarsURLConnection connection = new WebjarsURLConnection(
            new URL("mvn:org.webjars/angular-dateparser/1.0.9"));
    connection.connect();//from w w w.j a  v a2s . co m

    InputStream inputStream = connection.getInputStream();
    JarInputStream jar = new JarInputStream(inputStream);
    jar.getManifest();
    jar.close();
    try {
        connection.transform_thread.get();
    } catch (Exception exception) {
        fail("Thread failed to execute transform() method: " + exception.getMessage());
    }
}

From source file:com.sketchy.server.action.GetCurrentVersion.java

@Override
public JSONServletResult execute(HttpServletRequest request) throws Exception {
    JSONServletResult jsonServletResult = new JSONServletResult(Status.SUCCESS);
    String version = "";
    try {//from  ww  w.j  ava  2 s  . co  m
        File sketchyFile = new File("Sketchy.jar");
        if (!sketchyFile.exists()) {
            throw new Exception("Can't find Sketchy.jar file!");
        }
        JarInputStream jarInputStream = null;
        try {
            // check to make sure it's a Sketchy File with a Manifest File
            jarInputStream = new JarInputStream(new FileInputStream(sketchyFile), true);
            Manifest manifest = jarInputStream.getManifest();
            if (manifest == null) {
                throw new Exception("Manifest file not found.");
            }
            Attributes titleAttributes = manifest.getMainAttributes();
            version = titleAttributes.getValue("Implementation-Version");
        } catch (Exception e) {
            throw new Exception("Invalid Upgrade File!");
        } finally {
            IOUtils.closeQuietly(jarInputStream);
        }

        jsonServletResult.put("currentVersion", version);
    } catch (Throwable t) {
        jsonServletResult = new JSONServletResult(Status.ERROR,
                "Error getting Current Version! " + t.getMessage());
    }
    return jsonServletResult;
}

From source file:org.apache.brooklyn.core.mgmt.osgi.OsgiStandaloneTest.java

@Test
public void testReadKnownManifest() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    InputStream in = this.getClass().getResourceAsStream(BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    JarInputStream jarIn = new JarInputStream(in);
    ManifestHelper helper = Osgis.ManifestHelper.forManifest(jarIn.getManifest());
    jarIn.close();/*from w  w w . j ava  2  s  . c o m*/
    Assert.assertEquals(helper.getVersion().toString(), "0.1.0");
    Assert.assertTrue(helper.getExportedPackages().contains("org.apache.brooklyn.test.osgi.entities"));
}

From source file:brooklyn.management.osgi.OsgiStandaloneTest.java

@Test
public void testReadKnownManifest() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    InputStream in = this.getClass().getResourceAsStream(BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    JarInputStream jarIn = new JarInputStream(in);
    ManifestHelper helper = Osgis.ManifestHelper.forManifest(jarIn.getManifest());
    jarIn.close();/*from w w w  . j  ava2  s  .  c  om*/
    Assert.assertEquals(helper.getVersion().toString(), "0.1.0");
    Assert.assertTrue(helper.getExportedPackages().contains("brooklyn.osgi.tests"));
}

From source file:com.ikon.util.cl.BinaryClassLoader.java

/**
 * Create internal classes and resources cache
 *//*ww w .j  a va  2  s  . co  m*/
private void createCache(byte[] buf) throws IOException {
    ByteArrayInputStream bais = null;
    JarInputStream jis = null;
    byte[] buffer = new byte[1024 * 4];

    try {
        bais = new ByteArrayInputStream(buf);
        jis = new JarInputStream(bais);
        Attributes attr = jis.getManifest().getMainAttributes();
        mainClassName = attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;

        for (JarEntry entry = null; (entry = jis.getNextJarEntry()) != null;) {
            String name = entry.getName();

            if (!entry.isDirectory()) {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

                for (int n = 0; -1 != (n = jis.read(buffer));) {
                    byteStream.write(buffer, 0, n);
                }

                if (name.endsWith(".class")) {
                    String className = name.substring(0, name.indexOf('.')).replace('/', '.');
                    resources.put(className, byteStream.toByteArray());
                } else {
                    resources.put(name, byteStream.toByteArray());
                }

                byteStream.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(bais);
    }
}

From source file:org.echocat.nodoodle.transport.HandlerUnpacker.java

protected SplitResult splitMultipleJarIntoJars(InputStream inputStream, File targetDirectory)
        throws IOException {
    if (inputStream == null) {
        throw new NullPointerException();
    }/*  ww w . ja  va 2 s.c om*/
    if (targetDirectory == null) {
        throw new NullPointerException();
    }
    final Collection<File> jarFiles = new ArrayList<File>();
    final File mainJarFile = getMainJarFile(targetDirectory);
    jarFiles.add(mainJarFile);
    final JarInputStream jarInput = new JarInputStream(inputStream);
    final Manifest manifest = jarInput.getManifest();
    final FileOutputStream mainJarFileStream = new FileOutputStream(mainJarFile);
    try {
        final JarOutputStream jarOutput = new JarOutputStream(mainJarFileStream, manifest);
        try {
            JarEntry entry = jarInput.getNextJarEntry();
            while (entry != null) {
                final String entryName = entry.getName();
                if (!entry.isDirectory() && entryName.startsWith("lib/")) {
                    final File targetFile = new File(targetDirectory, entryName);
                    if (!targetFile.getParentFile().mkdirs()) {
                        throw new IOException("Could not create parent directory of " + targetFile + ".");
                    }
                    final OutputStream outputStream = new FileOutputStream(targetFile);
                    try {
                        IOUtils.copy(jarInput, outputStream);
                    } finally {
                        IOUtils.closeQuietly(outputStream);
                    }
                    jarFiles.add(targetFile);
                } else {
                    if (entryName.startsWith(TransportConstants.CLASSES_PREFIX)
                            && entryName.length() > TransportConstants.CLASSES_PREFIX.length()) {
                        try {
                            ZIP_ENTRY_NAME_FIELD.set(entry, entryName.substring(CLASSES_PREFIX.length()));
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException("Could not set " + ZIP_ENTRY_NAME_FIELD + ".", e);
                        }
                    }
                    jarOutput.putNextEntry(entry);
                    IOUtils.copy(jarInput, jarOutput);
                    jarOutput.closeEntry();
                }
                entry = jarInput.getNextJarEntry();
            }
        } finally {
            IOUtils.closeQuietly(jarOutput);
        }
    } finally {
        IOUtils.closeQuietly(mainJarFileStream);
    }
    return new SplitResult(Collections.unmodifiableCollection(jarFiles), manifest);
}