Search in sources :

Example 21 with Resource

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

the class FelixResourceAdapterTest method testGetCapabilitiesWithNullNamespace.

@Test
public void testGetCapabilitiesWithNullNamespace() {
    Resource resource = EasyMock.createNiceMock(Resource.class);
    Capability capability = EasyMock.createNiceMock(Capability.class);
    EasyMock.expect(capability.getName()).andReturn(Capability.PACKAGE);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(Capability.PACKAGE, "org.foo.bar");
    EasyMock.expect(capability.getPropertiesAsMap()).andReturn(properties);
    Capability[] capabilities = new Capability[] { capability };
    EasyMock.expect(resource.getCapabilities()).andReturn(capabilities);
    EasyMock.replay(resource);
    FelixResourceAdapter adapter = new FelixResourceAdapter(resource);
    List<org.osgi.resource.Capability> caps = adapter.getCapabilities(null);
    // osgi.identity, osgi.content. osgi.wiring.host, and osgi.wiring.package
    assertEquals("Null namespace should return all capabilities", 4, caps.size());
}
Also used : Capability(org.apache.felix.bundlerepository.Capability) HashMap(java.util.HashMap) Resource(org.apache.felix.bundlerepository.Resource) FelixResourceAdapter(org.apache.aries.subsystem.util.felix.FelixResourceAdapter) Test(org.junit.Test)

Example 22 with Resource

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

the class ResolveCommand method doExecute.

protected void doExecute(RepositoryAdmin admin) throws Exception {
    List<Repository> repositories = new ArrayList<>();
    repositories.add(admin.getSystemRepository());
    if (!noLocal) {
        repositories.add(admin.getLocalRepository());
    }
    if (!noRemote) {
        repositories.addAll(Arrays.asList(admin.listRepositories()));
    }
    Resolver resolver = admin.resolver(repositories.toArray(new Repository[repositories.size()]));
    for (Requirement requirement : parseRequirements(admin, requirements)) {
        resolver.add(requirement);
    }
    if (resolver.resolve(optional ? 0 : Resolver.NO_OPTIONAL_RESOURCES)) {
        Resource[] resources;
        resources = resolver.getRequiredResources();
        if ((resources != null) && (resources.length > 0)) {
            System.out.println("Required resource(s):");
            printUnderline(System.out, 21);
            for (Resource resource : resources) {
                System.out.println("   " + resource.getPresentationName() + " (" + resource.getVersion() + ")");
                if (why) {
                    Reason[] req = resolver.getReason(resource);
                    for (int reqIdx = 0; req != null && reqIdx < req.length; reqIdx++) {
                        if (!req[reqIdx].getRequirement().isOptional()) {
                            Resource r = req[reqIdx].getResource();
                            if (r != null) {
                                System.out.println("      - " + r.getPresentationName() + " / " + req[reqIdx].getRequirement().getName() + ":" + req[reqIdx].getRequirement().getFilter());
                            } else {
                                System.out.println("      - " + req[reqIdx].getRequirement().getName() + ":" + req[reqIdx].getRequirement().getFilter());
                            }
                        }
                    }
                }
            }
        }
        resources = resolver.getOptionalResources();
        if ((resources != null) && (resources.length > 0)) {
            System.out.println();
            System.out.println("Optional resource(s):");
            printUnderline(System.out, 21);
            for (Resource resource : resources) {
                System.out.println("   " + resource.getPresentationName() + " (" + resource.getVersion() + ")");
                if (why) {
                    Reason[] req = resolver.getReason(resource);
                    for (int reqIdx = 0; req != null && reqIdx < req.length; reqIdx++) {
                        if (!req[reqIdx].getRequirement().isOptional()) {
                            Resource r = req[reqIdx].getResource();
                            if (r != null) {
                                System.out.println("      - " + r.getPresentationName() + " / " + req[reqIdx].getRequirement().getName() + ":" + req[reqIdx].getRequirement().getFilter());
                            } else {
                                System.out.println("      - " + req[reqIdx].getRequirement().getName() + ":" + req[reqIdx].getRequirement().getFilter());
                            }
                        }
                    }
                }
            }
        }
        if (deploy || start) {
            try {
                System.out.print("\nDeploying...");
                resolver.deploy(start ? Resolver.START : 0);
                System.out.println("done.");
            } catch (IllegalStateException ex) {
                System.err.println(ex);
            }
        }
    } else {
        Reason[] reqs = resolver.getUnsatisfiedRequirements();
        if ((reqs != null) && (reqs.length > 0)) {
            System.out.println("Unsatisfied requirement(s):");
            printUnderline(System.out, 27);
            for (Reason req : reqs) {
                System.out.println("   " + req.getRequirement().getName() + ":" + req.getRequirement().getFilter());
                System.out.println("      " + req.getResource().getPresentationName());
            }
        } else {
            System.out.println("Could not resolve targets.");
        }
    }
}
Also used : Requirement(org.apache.felix.bundlerepository.Requirement) Repository(org.apache.felix.bundlerepository.Repository) Resolver(org.apache.felix.bundlerepository.Resolver) ArrayList(java.util.ArrayList) Resource(org.apache.felix.bundlerepository.Resource) Reason(org.apache.felix.bundlerepository.Reason)

Example 23 with Resource

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

the class SourceCommand method doExecute.

protected void doExecute(RepositoryAdmin admin) throws Exception {
    for (String bundle : bundles) {
        String[] target = getTarget(bundle);
        Resource resource = selectNewestVersion(searchRepository(admin, target[0], target[1]));
        if (resource == null) {
            System.err.println("Unknown bundle and/or version: " + target[0]);
        } else {
            URI srcURL = (URI) resource.getProperties().get(Resource.SOURCE_URI);
            if (srcURL != null) {
                FileUtil.downloadSource(System.out, System.err, srcURL.toURL(), localDir, extract);
            } else {
                System.err.println("Missing source URL: " + target[0]);
            }
        }
    }
}
Also used : Resource(org.apache.felix.bundlerepository.Resource) URI(java.net.URI)

Example 24 with Resource

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

the class ObrMBeanImpl method getBundles.

public TabularData getBundles() throws MBeanException {
    try {
        CompositeType bundleType = new CompositeType("OBR Resource", "Bundle available in the OBR", new String[] { "presentationname", "symbolicname", "version" }, new String[] { "Presentation Name", "Symbolic Name", "Version" }, new OpenType[] { SimpleType.STRING, SimpleType.STRING, SimpleType.STRING });
        TabularType tableType = new TabularType("OBR Resources", "Table of all resources/bundles available in the OBR", bundleType, new String[] { "symbolicname", "version" });
        TabularData table = new TabularDataSupport(tableType);
        Resource[] resources = repositoryAdmin.discoverResources("(|(presentationname=*)(symbolicname=*))");
        for (Resource resource : resources) {
            try {
                CompositeData data = new CompositeDataSupport(bundleType, new String[] { "presentationname", "symbolicname", "version" }, new Object[] { resource.getPresentationName(), resource.getSymbolicName(), resource.getVersion().toString() });
                table.put(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return table;
    } catch (Exception e) {
        throw new MBeanException(null, e.toString());
    }
}
Also used : TabularDataSupport(javax.management.openmbean.TabularDataSupport) TabularType(javax.management.openmbean.TabularType) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) Resource(org.apache.felix.bundlerepository.Resource) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MBeanException(javax.management.MBeanException) CompositeType(javax.management.openmbean.CompositeType) TabularData(javax.management.openmbean.TabularData)

Example 25 with Resource

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

the class FelixBundleRepositoryRenderHelper method toJSON.

private final void toJSON(JSONWriter writer, Resource resource, Bundle[] bundles, boolean details) throws IOException {
    final String symbolicName = resource.getSymbolicName();
    final Version version = resource.getVersion();
    String installed = "";
    for (int i = 0; symbolicName != null && installed.length() == 0 && bundles != null && i < bundles.length; i++) {
        final Version ver = bundles[i].getVersion();
        if (symbolicName.equals(bundles[i].getSymbolicName())) {
            installed = ver.toString();
        }
    }
    writer.object();
    // $NON-NLS-1$
    writer.key("id");
    writer.value(resource.getId());
    // $NON-NLS-1$
    writer.key("presentationname");
    writer.value(resource.getPresentationName());
    // $NON-NLS-1$
    writer.key("symbolicname");
    writer.value(symbolicName);
    // $NON-NLS-1$
    writer.key("url");
    writer.value(resource.getURI());
    // $NON-NLS-1$
    writer.key("version");
    writer.value(version);
    // $NON-NLS-1$
    writer.key("categories");
    writer.value(resource.getCategories());
    // $NON-NLS-1$
    writer.key("installed");
    writer.value(installed);
    if (details) {
        Capability[] caps = resource.getCapabilities();
        if (caps != null) {
            // $NON-NLS-1$
            writer.key("capabilities");
            writer.array();
            for (int i = 0; i < caps.length; i++) {
                writer.object();
                // $NON-NLS-1$
                writer.key("name");
                writer.value(caps[i].getName());
                // $NON-NLS-1$
                writer.key("properties");
                toJSON(writer, caps[i].getProperties());
                writer.endObject();
            }
            writer.endArray();
        }
        Requirement[] reqs = resource.getRequirements();
        if (reqs != null) {
            // $NON-NLS-1$
            writer.key("requirements");
            writer.array();
            for (int i = 0; i < reqs.length; i++) {
                writer.object();
                // $NON-NLS-1$
                writer.key("name");
                writer.value(reqs[i].getName());
                // $NON-NLS-1$
                writer.key("filter");
                writer.value(reqs[i].getFilter());
                // $NON-NLS-1$
                writer.key("optional");
                writer.value(reqs[i].isOptional());
                writer.endObject();
            }
            writer.endArray();
        }
        final RepositoryAdmin admin = (RepositoryAdmin) getRepositoryAdmin();
        Resolver resolver = admin.resolver();
        resolver.add(resource);
        resolver.resolve(Resolver.NO_OPTIONAL_RESOURCES);
        Resource[] required = resolver.getRequiredResources();
        if (required != null) {
            // $NON-NLS-1$
            writer.key("required");
            writer.array();
            for (int i = 0; required != null && i < required.length; i++) {
                toJSON(writer, required[i], bundles, false);
            }
            writer.endArray();
        }
        Resource[] optional = resolver.getOptionalResources();
        if (optional != null) {
            // $NON-NLS-1$
            writer.key("optional");
            writer.array();
            for (int i = 0; optional != null && i < optional.length; i++) {
                toJSON(writer, optional[i], bundles, false);
            }
            writer.endArray();
        }
        Reason[] unsatisfied = resolver.getUnsatisfiedRequirements();
        if (unsatisfied != null) {
            // $NON-NLS-1$
            writer.key("unsatisfied");
            writer.array();
            for (int i = 0; unsatisfied != null && i < unsatisfied.length; i++) {
                writer.object();
                // $NON-NLS-1$
                writer.key("name");
                writer.value(unsatisfied[i].getRequirement().getName());
                // $NON-NLS-1$
                writer.key("filter");
                writer.value(unsatisfied[i].getRequirement().getFilter());
                // $NON-NLS-1$
                writer.key("optional");
                writer.value(unsatisfied[i].getRequirement().isOptional());
                writer.endObject();
            }
            writer.endArray();
        }
    }
    writer.endObject();
}
Also used : RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) Capability(org.apache.felix.bundlerepository.Capability) Resolver(org.apache.felix.bundlerepository.Resolver) Resource(org.apache.felix.bundlerepository.Resource) Reason(org.apache.felix.bundlerepository.Reason) Requirement(org.apache.felix.bundlerepository.Requirement) Version(org.osgi.framework.Version)

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