Search in sources :

Example 26 with Resource

use of org.apache.felix.bundlerepository.Resource in project felix by apache.

the class FelixBundleRepositoryRenderHelper method doDeploy.

@Override
final void doDeploy(String[] bundles, boolean start, boolean optional) {
    try {
        // check whether we have to do something
        if (bundles == null || bundles.length == 0) {
            logger.log("No resources to deploy");
            return;
        }
        RepositoryAdmin repoAdmin = (RepositoryAdmin) getRepositoryAdmin();
        Resolver resolver = repoAdmin.resolver();
        // prepare the deployment
        for (int i = 0; i < bundles.length; i++) {
            String bundle = bundles[i];
            if (// $NON-NLS-1$
            bundle == null || bundle.equals("-")) {
                continue;
            }
            String filter = "(id=" + bundle + ")";
            Resource[] resources = repoAdmin.discoverResources(filter);
            if (resources != null && resources.length > 0) {
                resolver.add(resources[0]);
            }
        }
        FelixDeployer.deploy(resolver, logger, start, optional);
    } catch (InvalidSyntaxException e) {
        throw new IllegalStateException(e);
    }
}
Also used : RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) Resolver(org.apache.felix.bundlerepository.Resolver) Resource(org.apache.felix.bundlerepository.Resource) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 27 with Resource

use of org.apache.felix.bundlerepository.Resource in project felix by apache.

the class FelixBundleRepositoryRenderHelper method getData.

@Override
String getData(final String filter, final boolean details, Bundle[] bundles) {
    RepositoryAdmin admin = (RepositoryAdmin) getRepositoryAdmin();
    if (admin != null) {
        final StringWriter sw = new StringWriter();
        JSONWriter json = new JSONWriter(sw);
        try {
            json.object();
            // $NON-NLS-1$
            json.key("status");
            json.value(true);
            // $NON-NLS-1$
            json.key("details");
            json.value(details);
            final Repository[] repositories = admin.listRepositories();
            if (repositories != null) {
                // $NON-NLS-1$
                json.key("repositories");
                json.array();
                for (int i = 0; repositories != null && i < repositories.length; i++) {
                    json.object();
                    // $NON-NLS-1$
                    json.key("lastModified");
                    json.value(repositories[i].getLastModified());
                    // $NON-NLS-1$
                    json.key("name");
                    json.value(repositories[i].getName());
                    // $NON-NLS-1$
                    json.key("url");
                    json.value(repositories[i].getURI());
                    json.endObject();
                }
                json.endArray();
            }
            Resource[] resources = admin.discoverResources(filter);
            if (resources != null) {
                // $NON-NLS-1$
                json.key("resources");
                json.array();
                for (int i = 0; i < resources.length; i++) {
                    toJSON(json, resources[i], bundles, details);
                }
                json.endArray();
            }
            json.endObject();
            json.flush();
        } catch (IOException e) {
            logger.log("Failed to serialize repository to JSON object.", e);
        } catch (Exception e) {
            logger.log("Failed to parse filter '" + filter + "'", e);
            try {
                String reason = "filter=" + filter;
                if (e.getMessage() != null) {
                    reason = e.getMessage() + "(" + reason + ")";
                }
                // $NON-NLS-1$
                json.key("error");
                json.value(reason);
                json.endObject();
                json.flush();
            } catch (IOException je) {
            // ignore
            }
        }
        return sw.toString();
    }
    // $NON-NLS-1$
    return "{}";
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) Repository(org.apache.felix.bundlerepository.Repository) RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) StringWriter(java.io.StringWriter) Resource(org.apache.felix.bundlerepository.Resource) IOException(java.io.IOException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 28 with Resource

use of org.apache.felix.bundlerepository.Resource in project felix by apache.

the class DataModelHelperTest method testResource.

public void testResource() throws Exception {
    Attributes attr = new Attributes();
    attr.putValue("Manifest-Version", "1.0");
    attr.putValue("Bundle-Name", "Apache Felix Utils");
    attr.putValue("Bundle-Version", "0.1.0.SNAPSHOT");
    attr.putValue("Bundle-ManifestVersion", "2");
    attr.putValue("Bundle-License", "http://www.apache.org/licenses/LICENSE-2.0.txt");
    attr.putValue("Bundle-Description", "Utility classes for OSGi.");
    attr.putValue("Import-Package", "org.osgi.framework;version=\"[1.4,2)\"");
    attr.putValue("Bundle-SymbolicName", "org.apache.felix.utils");
    Resource resource = dmh.createResource(attr);
    String xml = dmh.writeResource(resource);
    System.out.println(xml);
    Resource resource2 = dmh.readResource(xml);
    String xml2 = dmh.writeResource(resource2);
    System.out.println(xml2);
    assertEquals(xml, xml2);
}
Also used : Attributes(java.util.jar.Attributes) Resource(org.apache.felix.bundlerepository.Resource)

Example 29 with Resource

use of org.apache.felix.bundlerepository.Resource in project felix by apache.

the class DataModelHelperTest method testCapabilities.

public void testCapabilities() throws Exception {
    Attributes attr = new Attributes();
    attr.putValue("Manifest-Version", "1.0");
    attr.putValue("Bundle-Name", "Apache Felix Utils");
    attr.putValue("Bundle-Version", "0.1.0.SNAPSHOT");
    attr.putValue("Bundle-ManifestVersion", "2");
    attr.putValue("Bundle-License", "http://www.apache.org/licenses/LICENSE-2.0.txt");
    attr.putValue("Bundle-Description", "Utility classes for OSGi.");
    attr.putValue("Import-Package", "org.osgi.framework;version=\"[1.4,2)\"");
    attr.putValue("Bundle-SymbolicName", "org.apache.felix.utils");
    attr.putValue("Provide-Capability", "osgi.extender;osgi.extender=\"osgi.component\";uses:=\"\n" + " org.osgi.service.component\";version:Version=\"1.3\",osgi.service;objectCl\n" + " ass:List<String>=\"org.osgi.service.component.runtime.ServiceComponentRu\n" + " ntime\";uses:=\"org.osgi.service.component.runtime\"");
    attr.putValue("Export-Package", "test.package;version=\"1.0.0\"");
    Resource resource = dmh.createResource(attr);
    assertEquals(4, resource.getCapabilities().length);
    Capability bundleCap = null;
    Capability osgiExtenderCap = null;
    Capability osgiServiceCap = null;
    Capability osgiPackageCap = null;
    for (Capability capability : resource.getCapabilities()) {
        if (capability.getName().equals("bundle")) {
            bundleCap = capability;
        } else if (capability.getName().equals("osgi.extender")) {
            osgiExtenderCap = capability;
        } else if (capability.getName().equals("service")) {
            osgiServiceCap = capability;
        } else if (capability.getName().equals("package")) {
            osgiPackageCap = capability;
        } else {
            osgiServiceCap = capability;
        }
    }
    assertNotNull(bundleCap);
    assertNotNull(osgiExtenderCap);
    assertNotNull(osgiServiceCap);
    assertNotNull(osgiPackageCap);
    assertEquals("osgi.extender", osgiExtenderCap.getName());
    assertEquals("osgi.component", osgiExtenderCap.getPropertiesAsMap().get("osgi.extender"));
    assertEquals("1.3.0", osgiExtenderCap.getPropertiesAsMap().get(Constants.VERSION_ATTRIBUTE).toString());
    assertEquals("service", osgiServiceCap.getName());
    assertEquals("package", osgiPackageCap.getName());
}
Also used : Capability(org.apache.felix.bundlerepository.Capability) Attributes(java.util.jar.Attributes) Resource(org.apache.felix.bundlerepository.Resource)

Example 30 with Resource

use of org.apache.felix.bundlerepository.Resource in project felix by apache.

the class RepositoryImplTest method testReferral2.

public void testReferral2() throws Exception {
    URL url = getClass().getResource("/referral1_repository.xml");
    RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
    RepositoryImpl repo = repoAdmin.addRepository(url, 1);
    Referral[] refs = repo.getReferrals();
    assertNotNull("Expect referrals", refs);
    assertTrue("Expect one referral", refs.length == 1);
    // <referral depth="1" url="referred.xml" />
    assertEquals(1, refs[0].getDepth());
    assertEquals("referred.xml", refs[0].getUrl());
    // expect one resource (referral is not followed
    Resource[] res = repoAdmin.discoverResources((String) null);
    assertNotNull("Expect Resource", res);
    assertEquals("Expect one resource", 1, res.length);
    // first resource is from the referral1_repository.xml
    assertEquals("6", res[0].getId());
// assertEquals("referral1_repository", res[0].getRepository().getName());
}
Also used : Resource(org.apache.felix.bundlerepository.Resource) URL(java.net.URL)

Aggregations

Resource (org.apache.felix.bundlerepository.Resource)45 Repository (org.apache.felix.bundlerepository.Repository)19 Capability (org.apache.felix.bundlerepository.Capability)9 Resolver (org.apache.felix.bundlerepository.Resolver)9 ArrayList (java.util.ArrayList)8 ModelledResource (org.apache.aries.application.modelling.ModelledResource)8 ModelledBundleResource (org.apache.aries.application.resolver.obr.ext.ModelledBundleResource)8 Reason (org.apache.felix.bundlerepository.Reason)7 RepositoryAdmin (org.apache.felix.bundlerepository.RepositoryAdmin)7 HashMap (java.util.HashMap)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 URL (java.net.URL)5 File (java.io.File)4 DataModelHelper (org.apache.felix.bundlerepository.DataModelHelper)4 FileWriter (java.io.FileWriter)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Requirement (org.apache.felix.bundlerepository.Requirement)3 BundleContext (org.osgi.framework.BundleContext)3