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:net.rim.ejde.internal.packaging.PackagingManager.java

/**
 * Checks if a jar file is a MidletJar created by rapc.
 *
 * @param f/* w  w  w  .ja  v a2s  .com*/
 * @return
 */
static public int getJarFileType(File f) {
    int type = 0x0;
    if (!f.exists()) {
        return type;
    }
    java.util.jar.JarFile jar = null;
    try {
        jar = new java.util.jar.JarFile(f, false);
        java.util.jar.Manifest manifest = jar.getManifest();
        if (manifest != null) {
            java.util.jar.Attributes attributes = manifest.getMainAttributes();
            String profile = attributes.getValue("MicroEdition-Profile");
            if (profile != null) {
                if ("MIDP-1.0".equals(profile) || "MIDP-2.0".equals(profile)) {
                    type = type | MIDLET_JAR;
                }
            }
        }
        Enumeration<JarEntry> entries = jar.entries();
        JarEntry entry;
        String entryName;
        InputStream is = null;
        IClassFileReader classFileReader = null;
        // check the attribute of the class files in the jar file
        for (; entries.hasMoreElements();) {
            entry = entries.nextElement();
            entryName = entry.getName();
            if (entryName.endsWith(IConstants.CLASS_FILE_EXTENSION_WITH_DOT)) {
                is = jar.getInputStream(entry);
                classFileReader = ToolFactory.createDefaultClassFileReader(is, IClassFileReader.ALL);
                if (isEvisceratedClass(classFileReader)) {
                    type = type | EVISCERATED_JAR;
                    break;
                }
            }
        }
    } catch (IOException e) {
        _log.error(e.getMessage());
    } finally {
        try {
            if (jar != null) {
                jar.close();
            }
        } catch (IOException e) {
            _log.error(e.getMessage());
        }
    }
    return type;
}

From source file:co.cask.cdap.internal.app.services.http.AppFabricTestBase.java

/**
 * Deploys an application with (optionally) a defined app name and app version
 *//* w  w w. j av a 2s .  c o  m*/
protected HttpResponse deploy(Class<?> application, @Nullable String apiVersion, @Nullable String namespace,
        @Nullable String appVersion, @Nullable Config appConfig) throws Exception {
    namespace = namespace == null ? Id.Namespace.DEFAULT.getId() : namespace;
    apiVersion = apiVersion == null ? Constants.Gateway.API_VERSION_3_TOKEN : apiVersion;
    appVersion = appVersion == null ? String.format("1.0.%d", System.currentTimeMillis()) : appVersion;

    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.BUNDLE_VERSION, appVersion);

    File artifactJar = buildAppArtifact(application, application.getSimpleName(), manifest);
    File expandDir = tmpFolder.newFolder();
    BundleJarUtil.unJar(Locations.toLocation(artifactJar), expandDir);

    // Add webapp
    File webAppFile = new File(expandDir, "webapp/default/netlens/src/1.txt");
    webAppFile.getParentFile().mkdirs();
    Files.write("dummy data", webAppFile, Charsets.UTF_8);
    BundleJarUtil.createJar(expandDir, artifactJar);

    HttpEntityEnclosingRequestBase request;
    String versionedApiPath = getVersionedAPIPath("apps/", apiVersion, namespace);
    request = getPost(versionedApiPath);
    request.setHeader(Constants.Gateway.API_KEY, "api-key-example");
    request.setHeader("X-Archive-Name", String.format("%s-%s.jar", application.getSimpleName(), appVersion));
    if (appConfig != null) {
        request.setHeader("X-App-Config", GSON.toJson(appConfig));
    }
    request.setEntity(new FileEntity(artifactJar));
    return execute(request);
}

From source file:org.apache.brooklyn.rest.resources.CatalogResource.java

@Override
@Beta/*ww w  .  j av  a  2s .  c  o  m*/
public Response createFromArchive(byte[] zipInput) {
    if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.ROOT, null)) {
        throw WebResourceUtils.forbidden("User '%s' is not authorized to add catalog item",
                Entitlements.getEntitlementContext().user());
    }

    BundleMaker bm = new BundleMaker(mgmtInternal());
    File f = null, f2 = null;
    try {
        f = Os.newTempFile("brooklyn-posted-archive", "zip");
        try {
            Files.write(zipInput, f);
        } catch (IOException e) {
            Exceptions.propagate(e);
        }

        ZipFile zf;
        try {
            zf = new ZipFile(f);
        } catch (IOException e) {
            throw new IllegalArgumentException("Invalid ZIP/JAR archive: " + e);
        }
        ZipArchiveEntry bom = zf.getEntry("catalog.bom");
        if (bom == null) {
            bom = zf.getEntry("/catalog.bom");
        }
        if (bom == null) {
            throw new IllegalArgumentException("Archive must contain a catalog.bom file in the root");
        }
        String bomS;
        try {
            bomS = Streams.readFullyString(zf.getInputStream(bom));
        } catch (IOException e) {
            throw new IllegalArgumentException("Error reading catalog.bom from ZIP/JAR archive: " + e);
        }

        try {
            zf.close();
        } catch (IOException e) {
            log.debug("Swallowed exception closing zipfile. Full error logged at trace: {}", e.getMessage());
            log.trace("Exception closing zipfile", e);
        }

        VersionedName vn = BasicBrooklynCatalog.getVersionedName(BasicBrooklynCatalog.getCatalogMetadata(bomS));

        Manifest mf = bm.getManifest(f);
        if (mf == null) {
            mf = new Manifest();
        }
        String bundleNameInMF = mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
        if (Strings.isNonBlank(bundleNameInMF)) {
            if (!bundleNameInMF.equals(vn.getSymbolicName())) {
                throw new IllegalArgumentException("JAR MANIFEST symbolic-name '" + bundleNameInMF
                        + "' does not match '" + vn.getSymbolicName() + "' defined in BOM");
            }
        } else {
            bundleNameInMF = vn.getSymbolicName();
            mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, bundleNameInMF);
        }

        String bundleVersionInMF = mf.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
        if (Strings.isNonBlank(bundleVersionInMF)) {
            if (!bundleVersionInMF.equals(vn.getVersion().toString())) {
                throw new IllegalArgumentException("JAR MANIFEST version '" + bundleVersionInMF
                        + "' does not match '" + vn.getVersion() + "' defined in BOM");
            }
        } else {
            bundleVersionInMF = vn.getVersion().toString();
            mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, bundleVersionInMF);
        }
        if (mf.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION) == null) {
            mf.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
        }

        f2 = bm.copyAddingManifest(f, mf);

        BasicManagedBundle bundleMetadata = new BasicManagedBundle(bundleNameInMF, bundleVersionInMF, null);
        Bundle bundle;
        try (FileInputStream f2in = new FileInputStream(f2)) {
            bundle = ((ManagementContextInternal) mgmt()).getOsgiManager().get()
                    .installUploadedBundle(bundleMetadata, f2in, false);
        } catch (Exception e) {
            throw Exceptions.propagate(e);
        }

        Iterable<? extends CatalogItem<?, ?>> catalogItems = MutableList
                .copyOf(((ManagementContextInternal) mgmt()).getOsgiManager().get().loadCatalogBom(bundle));

        return buildCreateResponse(catalogItems);
    } catch (RuntimeException ex) {
        throw WebResourceUtils.badRequest(ex);
    } finally {
        if (f != null)
            f.delete();
        if (f2 != null)
            f2.delete();
    }
}

From source file:org.ops4j.pax.runner.platform.internal.PlatformImpl.java

/**
 * Determine name to be used for caching on local file system.
 *
 * @param file                      file to be validated
 * @param defaultBundleSymbolicName default bundle symbolic name to be used if manifest does not have a bundle
 *                                  symbolic name
 *
 * @return file name based on bundle symbolic name and version
 *///from w  w  w  .j  a  v  a 2 s.  c o  m
String determineCachingName(final File file, final String defaultBundleSymbolicName) {
    String bundleSymbolicName = null;
    String bundleVersion = null;
    JarFile jar = null;
    try {
        // verify that is a valid jar. Do not verify that is signed (the false param).
        jar = new JarFile(file, false);
        final Manifest manifest = jar.getManifest();
        if (manifest != null) {
            bundleSymbolicName = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
            bundleVersion = manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
        }
    } catch (IOException ignore) {
        // just ignore
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException ignore) {
                // just ignore as this is less probably to happen.
            }
        }
    }
    if (bundleSymbolicName == null) {
        bundleSymbolicName = defaultBundleSymbolicName;
    } else {
        // remove directives like "; singleton:=true"
        int semicolonPos = bundleSymbolicName.indexOf(";");
        if (semicolonPos > 0) {
            bundleSymbolicName = bundleSymbolicName.substring(0, semicolonPos);
        }
    }
    if (bundleVersion == null) {
        bundleVersion = "0.0.0";
    }
    return bundleSymbolicName + "_" + bundleVersion + ".jar";
}

From source file:co.cask.cdap.internal.app.services.http.handlers.ArtifactHttpHandlerTest.java

@Test
public void testGetPlugins() throws Exception {
    // add an app for plugins to extend
    Id.Artifact wordCount1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addAppArtifact(wordCount1Id, WordCountApp.class).getStatusLine().getStatusCode());
    Id.Artifact wordCount2Id = Id.Artifact.from(Id.Namespace.DEFAULT, "wordcount", "2.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addAppArtifact(wordCount2Id, WordCountApp.class).getStatusLine().getStatusCode());

    // add some plugins.
    // plugins-1.0.0 extends wordcount[1.0.0,2.0.0)
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
    Id.Artifact pluginsId1 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "1.0.0");
    Set<ArtifactRange> plugins1Parents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addPluginArtifact(pluginsId1, Plugin1.class, manifest, plugins1Parents).getStatusLine()
                    .getStatusCode());//  w w w. ja  v a 2s  .  c  o m

    // plugin-2.0.0 extends wordcount[1.0.0,3.0.0)
    Id.Artifact pluginsId2 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "2.0.0");
    Set<ArtifactRange> plugins2Parents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("3.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addPluginArtifact(pluginsId2, Plugin1.class, manifest, plugins2Parents).getStatusLine()
                    .getStatusCode());

    ArtifactSummary plugins1Artifact = new ArtifactSummary("plugins", "1.0.0");
    ArtifactSummary plugins2Artifact = new ArtifactSummary("plugins", "2.0.0");

    // get plugin types, should be the same for both
    Set<String> expectedTypes = Sets.newHashSet("dummy", "callable");
    Set<String> actualTypes = getPluginTypes(wordCount1Id);
    Assert.assertEquals(expectedTypes, actualTypes);
    actualTypes = getPluginTypes(wordCount2Id);
    Assert.assertEquals(expectedTypes, actualTypes);

    // get plugin summaries. wordcount1 should see plugins from both plugin artifacts
    Set<PluginSummary> expectedSummaries = Sets.newHashSet(
            new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins1Artifact),
            new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(),
                    plugins2Artifact));
    Set<PluginSummary> actualSummaries = getPluginSummaries(wordCount1Id, "dummy");
    Assert.assertEquals(expectedSummaries, actualSummaries);

    expectedSummaries = Sets.newHashSet(
            new PluginSummary("Plugin2", "callable", "Just returns the configured integer",
                    Plugin2.class.getName(), plugins1Artifact),
            new PluginSummary("Plugin2", "callable", "Just returns the configured integer",
                    Plugin2.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount1Id, "callable");
    Assert.assertEquals(expectedSummaries, actualSummaries);

    // wordcount2 should only see plugins from plugins2 artifact
    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin1", "dummy", "This is plugin1",
            Plugin1.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount2Id, "dummy");
    Assert.assertEquals(expectedSummaries, actualSummaries);

    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin2", "callable",
            "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount2Id, "callable");
    Assert.assertEquals(expectedSummaries, actualSummaries);

    // get plugin info. Again, wordcount1 should see plugins from both artifacts
    Map<String, PluginPropertyField> p1Properties = ImmutableMap.of("x",
            new PluginPropertyField("x", "", "int", true), "stuff",
            new PluginPropertyField("stuff", "", "string", true));
    Map<String, PluginPropertyField> p2Properties = ImmutableMap.of("v",
            new PluginPropertyField("v", "value to return when called", "int", true));

    Set<PluginInfo> expectedInfos = Sets.newHashSet(
            new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins1Artifact,
                    p1Properties, new HashSet<String>()),
            new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact,
                    p1Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "dummy", "Plugin1"));

    expectedInfos = Sets.newHashSet(
            new PluginInfo("Plugin2", "callable", "Just returns the configured integer",
                    Plugin2.class.getName(), plugins1Artifact, p2Properties, new HashSet<String>()),
            new PluginInfo("Plugin2", "callable", "Just returns the configured integer",
                    Plugin2.class.getName(), plugins2Artifact, p2Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "callable", "Plugin2"));

    // while wordcount2 should only see plugins from plugins2 artifact
    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin1", "dummy", "This is plugin1",
            Plugin1.class.getName(), plugins2Artifact, p1Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "dummy", "Plugin1"));

    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin2", "callable", "Just returns the configured integer",
            Plugin2.class.getName(), plugins2Artifact, p2Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "callable", "Plugin2"));
}

From source file:org.eclipse.ebr.maven.BundleMojo.java

private Properties readL10nProps(final Manifest manifest) throws IOException {
    String bundleL10nBase = manifest.getMainAttributes().getValue(BUNDLE_LOCALIZATION);
    if (bundleL10nBase == null) {
        bundleL10nBase = BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
    }/*from   ww  w .j ava2s.c o  m*/
    final File l10nPropsFile = new File(outputDirectory, bundleL10nBase + ".properties");
    if (!l10nPropsFile.isFile()) {
        getLog().warn("Bundle localization file " + l10nPropsFile + " not found.");
        return null;
    }
    final Properties l10nProps = new Properties();
    FileInputStream in = null;
    try {
        in = new FileInputStream(l10nPropsFile);
        l10nProps.load(in);
    } finally {
        IOUtil.close(in);
    }
    return l10nProps;
}

From source file:co.cask.cdap.internal.app.services.http.handlers.ArtifactHttpHandlerTest.java

@Test
public void testPluginNamespaceIsolation() throws Exception {
    // add a system artifact. currently can't do this through the rest api (by design)
    // so bypass it and use the repository directly
    Id.Artifact systemId = Id.Artifact.from(Id.Namespace.SYSTEM, "wordcount", "1.0.0");
    File systemArtifact = buildAppArtifact(WordCountApp.class, "wordcount-1.0.0.jar");
    artifactRepository.addArtifact(systemId, systemArtifact, Sets.<ArtifactRange>newHashSet());

    Set<ArtifactRange> parents = Sets.newHashSet(new ArtifactRange(systemId.getNamespace(), systemId.getName(),
            systemId.getVersion(), true, systemId.getVersion(), true));

    Id.Namespace namespace1 = Id.Namespace.from("ns1");
    Id.Namespace namespace2 = Id.Namespace.from("ns2");
    createNamespace(namespace1.getId());
    createNamespace(namespace2.getId());

    try {//ww w .j a v a  2  s  . c om
        // add some plugins in namespace1. Will contain Plugin1 and Plugin2
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
        Id.Artifact pluginsId1 = Id.Artifact.from(namespace1, "plugins1", "1.0.0");
        Assert.assertEquals(HttpResponseStatus.OK.getCode(),
                addPluginArtifact(pluginsId1, Plugin1.class, manifest, parents).getStatusLine()
                        .getStatusCode());

        // add some plugins in namespace2. Will contain Plugin1 and Plugin2
        manifest = new Manifest();
        manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
        Id.Artifact pluginsId2 = Id.Artifact.from(namespace2, "plugins2", "1.0.0");
        Assert.assertEquals(HttpResponseStatus.OK.getCode(),
                addPluginArtifact(pluginsId2, Plugin1.class, manifest, parents).getStatusLine()
                        .getStatusCode());

        ArtifactSummary artifact1 = new ArtifactSummary(pluginsId1.getName(),
                pluginsId1.getVersion().getVersion(), ArtifactScope.USER);
        ArtifactSummary artifact2 = new ArtifactSummary(pluginsId2.getName(),
                pluginsId2.getVersion().getVersion(), ArtifactScope.USER);

        PluginSummary summary1Namespace1 = new PluginSummary("Plugin1", "dummy", "This is plugin1",
                Plugin1.class.getName(), artifact1);
        PluginSummary summary2Namespace1 = new PluginSummary("Plugin2", "callable",
                "Just returns the configured integer", Plugin2.class.getName(), artifact1);
        PluginSummary summary1Namespace2 = new PluginSummary("Plugin1", "dummy", "This is plugin1",
                Plugin1.class.getName(), artifact2);
        PluginSummary summary2Namespace2 = new PluginSummary("Plugin2", "callable",
                "Just returns the configured integer", Plugin2.class.getName(), artifact2);

        PluginInfo info1Namespace1 = new PluginInfo("Plugin1", "dummy", "This is plugin1",
                Plugin1.class.getName(), artifact1,
                ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true), "stuff",
                        new PluginPropertyField("stuff", "", "string", true)),
                new HashSet<String>());
        PluginInfo info2Namespace1 = new PluginInfo("Plugin2", "callable",
                "Just returns the configured integer", Plugin2.class.getName(), artifact1,
                ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true)),
                new HashSet<String>());
        PluginInfo info1Namespace2 = new PluginInfo("Plugin1", "dummy", "This is plugin1",
                Plugin1.class.getName(), artifact2,
                ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true), "stuff",
                        new PluginPropertyField("stuff", "", "string", true)),
                new HashSet<String>());
        PluginInfo info2Namespace2 = new PluginInfo("Plugin2", "callable",
                "Just returns the configured integer", Plugin2.class.getName(), artifact2,
                ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true)),
                new HashSet<String>());

        Id.Artifact namespace1Artifact = Id.Artifact.from(namespace1, systemId.getName(),
                systemId.getVersion());
        Id.Artifact namespace2Artifact = Id.Artifact.from(namespace2, systemId.getName(),
                systemId.getVersion());

        // should see same types in both namespaces
        Assert.assertEquals(ImmutableSet.of("dummy", "callable"),
                getPluginTypes(namespace1Artifact, ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of("dummy", "callable"),
                getPluginTypes(namespace2Artifact, ArtifactScope.SYSTEM));

        // should see that plugins in namespace1 come only from the namespace1 artifact
        Assert.assertEquals(ImmutableSet.of(summary1Namespace1),
                getPluginSummaries(namespace1Artifact, "dummy", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(summary2Namespace1),
                getPluginSummaries(namespace1Artifact, "callable", ArtifactScope.SYSTEM));

        Assert.assertEquals(ImmutableSet.of(info1Namespace1),
                getPluginInfos(namespace1Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info2Namespace1),
                getPluginInfos(namespace1Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));

        // should see that plugins in namespace2 come only from the namespace2 artifact
        Assert.assertEquals(ImmutableSet.of(summary1Namespace2),
                getPluginSummaries(namespace2Artifact, "dummy", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(summary2Namespace2),
                getPluginSummaries(namespace2Artifact, "callable", ArtifactScope.SYSTEM));

        Assert.assertEquals(ImmutableSet.of(info1Namespace2),
                getPluginInfos(namespace2Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info2Namespace2),
                getPluginInfos(namespace2Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));
    } finally {
        deleteNamespace("iso1");
        deleteNamespace("iso2");
    }
}

From source file:co.cask.cdap.internal.app.services.http.handlers.ArtifactHttpHandlerTest.java

@Test
public void testPluginWithEndpoints() throws Exception {
    // add an app for plugins to extend
    Id.Artifact wordCount1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addAppArtifact(wordCount1Id, WordCountApp.class).getStatusLine().getStatusCode());

    // add some plugins.
    // plugins-3.0.0 extends wordcount[1.0.0,2.0.0)
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE,
            CallablePlugin.class.getPackage().getName());
    Id.Artifact plugins3Id = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins3", "1.0.0");
    Set<ArtifactRange> plugins3Parents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addPluginArtifact(plugins3Id, CallablePlugin.class, manifest, plugins3Parents).getStatusLine()
                    .getStatusCode());/*from  ww w. j  a  va 2 s  .  c o  m*/

    Set<PluginInfo> expectedInfos = Sets
            .newHashSet(new PluginInfo("CallablePlugin", "interactive", "This is plugin with endpoint",
                    CallablePlugin.class.getName(), new ArtifactSummary("plugins3", "1.0.0"),
                    ImmutableMap.<String, PluginPropertyField>of(), ImmutableSet.<String>of("ping")));

    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "interactive", "CallablePlugin"));
    // test plugin with endpoint
    Assert.assertEquals("hello", GSON.fromJson(callPluginMethod(plugins3Id, "interactive", "CallablePlugin",
            "ping", "user", ArtifactScope.USER, 200).getResponseBodyAsString(), String.class));

    manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, CallingPlugin.class.getPackage().getName());
    Id.Artifact plugins4Id = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins4", "1.0.0");
    Set<ArtifactRange> plugins4Parents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addPluginArtifact(plugins4Id, CallingPlugin.class, manifest, plugins4Parents).getStatusLine()
                    .getStatusCode());

    // test plugin with endpoint having endpoint-context parameter
    Assert.assertEquals("hi user", GSON.fromJson(callPluginMethod(plugins4Id, "interactive", "CallingPlugin",
            "ping", "user", ArtifactScope.USER, 200).getResponseBodyAsString(), String.class));

    // test plugin that accepts list of data and aggregates and returns result map
    manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE,
            PluginWithPojo.class.getPackage().getName());
    Id.Artifact plugins5Id = Id.Artifact.from(Id.Namespace.DEFAULT, "aggregator", "1.0.0");
    Set<ArtifactRange> plugins5Parents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addPluginArtifact(plugins5Id, PluginWithPojo.class, manifest, plugins5Parents).getStatusLine()
                    .getStatusCode());

    // test plugin with endpoint having endpoint-context parameter
    List<TestData> data = ImmutableList.of(new TestData(1, 10), new TestData(1, 20), new TestData(3, 15),
            new TestData(4, 5), new TestData(3, 15));
    Map<Long, Long> expectedResult = new HashMap<>();
    expectedResult.put(1L, 30L);
    expectedResult.put(3L, 30L);
    expectedResult.put(4L, 5L);
    String response = callPluginMethod(plugins5Id, "interactive", "aggregator", "aggregate", GSON.toJson(data),
            ArtifactScope.USER, 200).getResponseBodyAsString();
    Assert.assertEquals(expectedResult, GSON.fromJson(response, new TypeToken<Map<Long, Long>>() {
    }.getType()));

    // test calling a non-existent plugin method "bing"
    callPluginMethod(plugins4Id, "interactive", "CallingPlugin", "bing", "user", ArtifactScope.USER, 404);

    manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, InvalidPlugin.class.getPackage().getName());
    Id.Artifact invalidPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "invalid", "1.0.0");
    Set<ArtifactRange> invalidPluginParents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT,
            "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(),
            addPluginArtifact(invalidPluginId, InvalidPlugin.class, manifest, invalidPluginParents)
                    .getStatusLine().getStatusCode());

    // test adding plugin artifact which has endpoint method containing 3 params (invalid)
    manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE,
            InvalidPluginMethodParams.class.getPackage().getName());
    invalidPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "invalidParams", "1.0.0");
    invalidPluginParents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(),
            addPluginArtifact(invalidPluginId, InvalidPluginMethodParams.class, manifest, invalidPluginParents)
                    .getStatusLine().getStatusCode());

    // test adding plugin artifact which has endpoint method containing 2 params
    // but 2nd param is not EndpointPluginContext (invalid)
    manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE,
            InvalidPluginMethodParamType.class.getPackage().getName());
    invalidPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "invalidParamType", "1.0.0");
    invalidPluginParents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(),
            addPluginArtifact(invalidPluginId, InvalidPluginMethodParamType.class, manifest,
                    invalidPluginParents).getStatusLine().getStatusCode());

    // test adding plugin artifact which has endpoint methods containing 2 params
    // but 2nd param is implementation and extensions of EndpointPluginContext, should succeed
    manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE,
            PluginEndpointContextTestPlugin.class.getPackage().getName());
    Id.Artifact validPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "extender", "1.0.0");
    Set<ArtifactRange> validPluginParents = Sets.newHashSet(new ArtifactRange(Id.Namespace.DEFAULT, "wordcount",
            new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(),
            addPluginArtifact(validPluginId, PluginEndpointContextTestPlugin.class, manifest,
                    validPluginParents).getStatusLine().getStatusCode());
}

From source file:org.eclipse.ebr.maven.BundleMojo.java

private File generateSourceBundleManifest() throws MojoExecutionException {
    try {/*from   ww w  .  java 2  s . c o m*/
        generateSourceBundleL10nFile();

        final Manifest mf = new Manifest();
        final Attributes attributes = mf.getMainAttributes();

        if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
            attributes.put(Name.MANIFEST_VERSION, "1.0");
        }

        final String expandedVersion = getExpandedVersion();
        attributes.putValue(BUNDLE_VERSION, expandedVersion);
        attributes.putValue(BUNDLE_MANIFESTVERSION, "2");
        attributes.putValue(BUNDLE_SYMBOLICNAME, getSourceBundleSymbolicName());
        attributes.putValue(BUNDLE_NAME, I18N_KEY_PREFIX + I18N_KEY_BUNDLE_NAME);
        attributes.putValue(BUNDLE_VENDOR, I18N_KEY_PREFIX + I18N_KEY_BUNDLE_VENDOR);
        //attributes.putValue(BUNDLE_LOCALIZATION, BUNDLE_LOCALIZATION_DEFAULT_BASENAME);
        attributes.putValue("Eclipse-SourceBundle",
                project.getArtifactId() + ";version=\"" + expandedVersion + "\";roots:=\".\"");
        attributes.putValue(CREATED_BY, "Eclipse Bundle Recipe Maven Plug-in");

        final File mfile = getSourceBundleManifestFile();
        mfile.getParentFile().mkdirs();
        final BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(mfile));
        try {
            mf.write(os);
        } finally {
            os.close();
        }

        return mfile;
    } catch (final Exception e) {
        throw new MojoExecutionException("Error generating source bundle manifest: " + e.getMessage(), e);
    }
}

From source file:org.echocat.nodoodle.classloading.FileClassLoader.java

private boolean isSealed(String name, Manifest man) {
    final String path = name.replace('.', '/').concat("/");
    Attributes attr = man.getAttributes(path);
    String sealed = null;//from  w w w.  jav  a2  s  .  co m
    if (attr != null) {
        sealed = attr.getValue(Name.SEALED);
    }
    if (sealed == null) {
        if ((attr = man.getMainAttributes()) != null) {
            sealed = attr.getValue(Name.SEALED);
        }
    }
    return "true".equalsIgnoreCase(sealed);
}