Example usage for java.util.jar Manifest getMainAttributes

List of usage examples for java.util.jar Manifest getMainAttributes

Introduction

In this page you can find the example usage for java.util.jar Manifest getMainAttributes.

Prototype

public Attributes getMainAttributes() 

Source Link

Document

Returns the main Attributes for the Manifest.

Usage

From source file:test.BuilderTest.java

public static void testMultipleExport2() throws Exception {
    File cp[] = { IO.getFile("jar/asm.jar") };
    Builder bmaker = new Builder();
    try {/*from w  w w . j  ava 2s .c om*/
        Properties p = new Properties();
        p.setProperty("Export-Package",
                "org.objectweb.asm;version=1.1, org.objectweb.asm;version=1.2, org.objectweb.asm;version=2.3");
        bmaker.setProperties(p);
        bmaker.setClasspath(cp);
        Jar jar = bmaker.build();
        assertTrue(bmaker.check());
        jar.getManifest().write(System.err);
        Manifest m = jar.getManifest();
        m.write(System.err);
        String ip = m.getMainAttributes().getValue("Export-Package");
        assertTrue(ip.indexOf("org.objectweb.asm;version=\"1.1\"") >= 0);
        assertTrue(ip.indexOf("org.objectweb.asm;version=\"1.2\"") >= 0);
        assertTrue(ip.indexOf("org.objectweb.asm;version=\"2.3\"") >= 0);
    } finally {
        bmaker.close();
    }
}

From source file:test.BuilderTest.java

public static void testRemoveHeaders() throws Exception {
    Builder b = new Builder();
    try {/*from w  ww .  j a va  2 s. c  o m*/
        b.setProperty("Private-Package", "org.osgi.framework");
        b.setProperty("T1", "1");
        b.setProperty("T2", "1");
        b.setProperty("T1_2", "1");
        b.setProperty("-removeheaders", "!T1_2,T1*");
        b.addClasspath(IO.getFile("jar/osgi.jar"));
        Jar jar = b.build();
        assertTrue(b.check());

        Manifest m = jar.getManifest();
        assertNotNull(m);
        assertEquals("1", m.getMainAttributes().getValue("T2"));
        assertEquals("1", m.getMainAttributes().getValue("T1_2"));
        assertEquals(null, m.getMainAttributes().getValue("T1"));
    } finally {
        b.close();
    }
}

From source file:test.BuilderTest.java

public static void testImportExportBadVersion() throws Exception {
    Builder b = new Builder();
    try {//from w w w .  j a  v  a 2  s .  c  om
        b.addClasspath(IO.getFile("jar/ds.jar"));
        b.set(Analyzer.BUNDLE_VERSION, "0.9.5-@#SNAPSHOT");
        b.set(Analyzer.EXPORT_PACKAGE, "*;version=0.9.5-@#SNAPSHOT");
        b.set(Analyzer.IMPORT_PACKAGE, "*;version=0.9.5-@#SNAPSHOT");

        Jar jar = b.build();
        assertTrue(b.check());
        Manifest m = jar.getManifest();
        m.write(System.err);
        assertEquals(m.getMainAttributes().getValue("Bundle-Version"), "0.9.5.SNAPSHOT");

        assertNotNull(b.getExports().getByFQN("org.eclipse.equinox.ds.parser"));
        assertEquals("0.9.5.SNAPSHOT", b.getExports().getByFQN("org.eclipse.equinox.ds.parser").getVersion());

        assertNotNull(b.getImports().getByFQN("org.osgi.framework"));
        assertEquals("0.9.5.SNAPSHOT", b.getImports().getByFQN("org.osgi.framework").getVersion());
    } finally {
        b.close();
    }
}

From source file:org.marketcetera.strategy.StrategyTestBase.java

/**
 * Constructs a classpath to use for Java compilation.
 * /*w  w w .  j a  v  a2s  .c o  m*/
 * <p>This method will make a best-effort to create the classpath,
 * ignoring errors that occur during the collection.  This method
 * is not expected to throw exceptions, muddling on instead.
 *
 * @return a <code>Set&lt;String&gt;</code> value
 */
private Set<String> getClassPath() {
    // get the classloader that was used to load this class
    ClassLoader classLoader = getClass().getClassLoader();
    // this collection will hold all the paths we find, duplicates discarded, in the order they appear
    Set<String> paths = new LinkedHashSet<String>();
    //        //Collect all URLs from the URL Class Loaders.
    //        do {
    //            if(classLoader instanceof URLClassLoader) {
    //                URLClassLoader urlClassLoader = (URLClassLoader)classLoader;
    //                for(URL url : urlClassLoader.getURLs()) {
    //                    try {
    //                        paths.add(url.toURI().getPath());
    //                    } catch (URISyntaxException ignore) {
    //                    }
    //                }
    //            }
    //            // traverse the classloader tree upwards until no more remain
    //        } while((classLoader = classLoader.getParent()) != null);
    //        // reset the classloader to the current
    //        classLoader = getClass().getClassLoader();
    //iterate through the manifests of all the jars to find the
    // values of their Class-Path attribute value and add them to the
    // set.
    try {
        Enumeration<URL> resourceEnumeration = classLoader.getResources("META-INF/MANIFEST.MF");
        while (resourceEnumeration.hasMoreElements()) {
            URL resourceURL = resourceEnumeration.nextElement();
            InputStream is = null;
            try {
                // open the resource
                is = resourceURL.openStream();
                Manifest manifest = new Manifest(is);
                String theClasspath = manifest.getMainAttributes().getValue("Class-Path");
                if (theClasspath != null && !theClasspath.trim().isEmpty()) {
                    //manifest classpath is space separated URLs
                    for (String path : theClasspath.split(" ")) {
                        try {
                            URL pathURL = new URL(path);
                            paths.add(pathURL.toURI().getPath());
                        } catch (MalformedURLException ignore) {
                        } catch (URISyntaxException ignore) {
                        }
                    }
                }
            } catch (IOException ignore) {
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        }
    } catch (IOException ignore) {
    }
    return paths;
}

From source file:test.BuilderTest.java

/**
 * Test private imports. We first build a jar with a import:=private packge.
 * Then place it/* w w w . j av a  2s.  c  o  m*/
 * 
 * @throws Exception
 */

public static void testClassnames() throws Exception {
    Builder b = new Builder();
    try {
        b.addClasspath(IO.getFile("jar/osgi.jar"));
        b.addClasspath(IO.getFile("jar/ds.jar"));
        b.addClasspath(IO.getFile("jar/ifc112.jar"));
        b.setProperty("Export-Package", "*");
        b.setProperty("C1", "${classes;implementing;org.osgi.service.component.*}");
        b.setProperty("C2", "${classes;extending;org.xml.sax.helpers.*}");
        b.setProperty("C3", "${classes;importing;org.xml.sax}");
        b.setProperty("C4", "${classes;named;*Parser*}");
        b.setProperty("C5", "${classes;named;*Parser*;version;45.*}");
        Jar jar = b.build();
        assertTrue(b.check());

        Manifest m = jar.getManifest();
        m.write(System.err);
        Attributes main = m.getMainAttributes();
        assertList(asl(
                "org.eclipse.equinox.ds.service.ComponentContextImpl,org.eclipse.equinox.ds.service.ComponentFactoryImpl,org.eclipse.equinox.ds.service.ComponentInstanceImpl"),
                asl(main.getValue("C1")));
        assertList(asl("org.eclipse.equinox.ds.parser.ElementHandler, "
                + "org.eclipse.equinox.ds.parser.IgnoredElement,"
                + "org.eclipse.equinox.ds.parser.ImplementationElement,"
                + "org.eclipse.equinox.ds.parser.ParserHandler, "
                + "org.eclipse.equinox.ds.parser.PropertiesElement,"
                + "org.eclipse.equinox.ds.parser.PropertyElement, "
                + "org.eclipse.equinox.ds.parser.ProvideElement, "
                + "org.eclipse.equinox.ds.parser.ReferenceElement, "
                + "org.eclipse.equinox.ds.parser.ServiceElement,"
                + "org.eclipse.equinox.ds.parser.ComponentElement"), asl(main.getValue("C2")));
        assertList(asl(
                "org.eclipse.equinox.ds.parser.ComponentElement,org.eclipse.equinox.ds.parser.ElementHandler,org.eclipse.equinox.ds.parser.IgnoredElement,org.eclipse.equinox.ds.parser.ImplementationElement,org.eclipse.equinox.ds.parser.Parser,org.eclipse.equinox.ds.parser.ParserHandler,org.eclipse.equinox.ds.parser.PropertiesElement,org.eclipse.equinox.ds.parser.PropertyElement,org.eclipse.equinox.ds.parser.ProvideElement,org.eclipse.equinox.ds.parser.ReferenceElement,org.eclipse.equinox.ds.parser.ServiceElement"),
                asl(main.getValue("C3")));
        assertList(asl(
                "org.eclipse.equinox.ds.parser.XMLParserNotAvailableException,org.eclipse.equinox.ds.parser.Parser,org.eclipse.equinox.ds.parser.ParserHandler,netscape.application.HTMLParser,org.eclipse.equinox.ds.parser.ParserConstants,org.osgi.util.xml.XMLParserActivator"),
                asl(main.getValue("C4")));
        assertEquals("netscape.application.HTMLParser", main.getValue("C5"));
    } finally {
        b.close();
    }
}

From source file:org.eclipse.jubula.client.ui.rcp.widgets.autconfig.JavaAutConfigComponent.java

/** 
 * The action of the JAR name field./*ww  w  .  j a  v  a2 s. c om*/
 * @return <code>null</code> if the new value is valid. Otherwise, returns
 *         a status parameter indicating the cause of the problem.
 */
DialogStatusParameter modifyJarFieldAction() {

    DialogStatusParameter error = null;
    boolean isEmpty = m_jarTextField.getText().length() == 0;
    if (isValid(m_jarTextField, true) && !isEmpty) {
        if (checkLocalhostServer()) {
            String filename = m_jarTextField.getText();
            File file = new File(filename);
            String workingDir = StringUtils.defaultString(getConfigValue(AutConfigConstants.WORKING_DIR));
            if (!file.isAbsolute() && workingDir.length() != 0) {

                filename = workingDir + "/" + filename; //$NON-NLS-1$
                file = new File(filename);
            }
            try {
                if (!file.exists()) {
                    error = createWarningStatus(
                            NLS.bind(Messages.AUTConfigComponentFileNotFound, file.getCanonicalPath()));
                } else {
                    JarFile jarFile = new JarFile(file);
                    Manifest jarManifest = jarFile.getManifest();
                    if (jarManifest == null) {
                        // no manifest for JAR
                        error = createErrorStatus(Messages.AUTConfigComponentNoManifest);
                    } else if (jarManifest.getMainAttributes().getValue(MAIN_CLASS) == null) {

                        // no main class defined in JAR manifest
                        error = createErrorStatus(Messages.AUTConfigComponentNoMainClass);
                    }
                }
            } catch (ZipException ze) {
                // given file is not a jar file
                error = createErrorStatus(NLS.bind(Messages.AUTConfigComponentFileNotJar, filename));
            } catch (IOException e) {
                // could not find jar file
                error = createWarningStatus(NLS.bind(Messages.AUTConfigComponentFileNotFound, filename));
            }
        }
    } else if (!isEmpty) {
        error = createErrorStatus(Messages.AUTConfigComponentWrongJAR);
    }

    putConfigValue(AutConfigConstants.JAR_FILE, m_jarTextField.getText());

    return error;
}

From source file:test.BuilderTest.java

public static void testWab() throws Exception {
    Builder b = new Builder();
    try {// w w w  .java  2s .  c  o m
        b.setProperty("-wablib", "jar/asm.jar, jar/easymock.jar");
        b.setProperty("-wab", "jar/osgi.jar");
        b.setProperty("-includeresource", "OSGI-INF/xml/x.xml;literal=\"text\"");
        b.setProperty("Private-Package", "org.osgi.framework");
        b.addClasspath(IO.getFile("jar/osgi.jar"));
        Jar jar = b.build();
        assertTrue(b.check());

        Manifest m = jar.getManifest();
        assertNotNull(m);
        assertEquals("WEB-INF/classes,WEB-INF/lib/asm.jar,WEB-INF/lib/easymock.jar",
                m.getMainAttributes().getValue("Bundle-ClassPath"));
        assertNotNull(jar.getResource("WEB-INF/lib/asm.jar"));
        assertNotNull(jar.getResource("WEB-INF/classes/org/osgi/framework/BundleContext.class"));
        assertNotNull(jar.getResource("osgi.jar"));
        assertNotNull(jar.getResource("OSGI-INF/xml/x.xml"));
    } finally {
        b.close();
    }
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlOsgiManifestRequiresCeylonLanguageBundle() throws IOException {
    compile("modules/osgi/a/module.ceylon", "modules/osgi/a/package.ceylon", "modules/osgi/a/A.ceylon");

    final Manifest manifest = getManifest("com.redhat.ceylon.compiler.java.test.cmr.modules.osgi.a", "1.1.0");

    assertEquals(// w w w . j a  v  a 2 s .  co  m
            "ceylon.language;bundle-version=" + Versions.CEYLON_VERSION_NUMBER + ";visibility:=reexport"
                    + ",com.redhat.ceylon.dist;bundle-version=" + Versions.CEYLON_VERSION_NUMBER
                    + ";visibility:=reexport",
            manifest.getMainAttributes().get(OsgiUtil.OsgiManifest.Require_Bundle));
}

From source file:org.apache.openejb.config.DeploymentLoader.java

protected ClientModule createClientModule(final URL clientUrl, final String absolutePath,
        final ClassLoader appClassLoader, final String moduleName, final boolean log) throws OpenEJBException {
    final ResourceFinder clientFinder = new ResourceFinder(clientUrl);

    URL manifestUrl = null;/* ww  w . j  av  a 2 s. co  m*/
    try {
        manifestUrl = clientFinder.find("META-INF/MANIFEST.MF");
    } catch (final IOException e) {
        //
    }

    String mainClass = null;
    if (manifestUrl != null) {
        try {
            final InputStream is = IO.read(manifestUrl);
            final Manifest manifest = new Manifest(is);
            mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
        } catch (final IOException e) {
            throw new OpenEJBException("Unable to determine Main-Class defined in META-INF/MANIFEST.MF file",
                    e);
        }
    }

    //        if (mainClass == null) throw new IllegalStateException("No Main-Class defined in META-INF/MANIFEST.MF file");

    final Map<String, URL> descriptors = getDescriptors(clientFinder, log);

    ApplicationClient applicationClient = null;
    final URL clientXmlUrl = descriptors.get("application-client.xml");
    if (clientXmlUrl != null) {
        applicationClient = ReadDescriptors.readApplicationClient(clientXmlUrl);
    }

    final ClientModule clientModule = new ClientModule(applicationClient, appClassLoader, absolutePath,
            mainClass, moduleName);

    clientModule.getAltDDs().putAll(descriptors);
    if (absolutePath != null) {
        clientModule.getWatchedResources().add(absolutePath);
    }
    if (clientXmlUrl != null && "file".equals(clientXmlUrl.getProtocol())) {
        clientModule.getWatchedResources().add(URLs.toFilePath(clientXmlUrl));
    }
    return clientModule;
}