Search in sources :

Example 1 with Resource

use of org.osgi.service.obr.Resource in project felix by apache.

the class ObrProbe method deploy.

// //////////////////////////////////////////////////////
// ObrProbeMBean                              //
// //////////////////////////////////////////////////////
public void deploy(String location, String version) {
    ServiceReference sref = this.bc.getServiceReference(RepositoryAdmin.class.getName());
    System.out.println("Starting " + location + " " + version);
    if (sref != null) {
        RepositoryAdmin brs = (RepositoryAdmin) this.bc.getService(sref);
        Resolver resolver = brs.resolver();
        Resource ressource = selectNewestVersion(searchRepository(brs, location, version));
        if (ressource != null) {
            resolver.add(ressource);
        }
        if ((resolver.getAddedResources() != null) && (resolver.getAddedResources().length > 0)) {
            if (resolver.resolve()) {
                try {
                    // Bundles are started
                    resolver.deploy(true);
                } catch (IllegalStateException ex) {
                    System.out.println(ex);
                }
            }
        }
    } else {
        this.log(LogService.LOG_ERROR, "No BundleRepository Service", null);
    }
}
Also used : RepositoryAdmin(org.osgi.service.obr.RepositoryAdmin) Resolver(org.osgi.service.obr.Resolver) Resource(org.osgi.service.obr.Resource) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with Resource

use of org.osgi.service.obr.Resource in project felix by apache.

the class OsgiBundleRepositoryRenderHelper 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; 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].getURL());
                    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.osgi.service.obr.Repository) RepositoryAdmin(org.osgi.service.obr.RepositoryAdmin) StringWriter(java.io.StringWriter) Resource(org.osgi.service.obr.Resource) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 3 with Resource

use of org.osgi.service.obr.Resource in project felix by apache.

the class UpdateHelper method updateFromOsgiOBR.

private boolean updateFromOsgiOBR() {
    RepositoryAdmin ra = (RepositoryAdmin) getService("org.osgi.service.obr.RepositoryAdmin");
    if (ra != null) {
        getLog().log(LogService.LOG_DEBUG, "Trying to update from OSGi Bundle Repository (OSGi API)");
        final Resolver resolver = ra.resolver();
        String version = (String) bundle.getHeaders().get(Constants.BUNDLE_VERSION);
        if (version == null) {
            version = "0.0.0";
        }
        final String filter = "(&(symbolicname=" + bundle.getSymbolicName() + ")(!(version=" + version + "))(version>=" + version + "))";
        final Resource[] resources = ra.discoverResources(filter);
        final Resource resource = selectHighestVersion(resources);
        if (resource != null) {
            resolver.add(resource);
            if (!resolver.resolve()) {
                logRequirements("Cannot updated bundle from OBR due to unsatisfied requirements", resolver.getUnsatisfiedRequirements());
            } else {
                logResource("Installing Requested Resources", resolver.getAddedResources());
                logResource("Installing Required Resources", resolver.getRequiredResources());
                logResource("Installing Optional Resources", resolver.getOptionalResources());
                // deploy the resolved bundles and ensure they are started
                resolver.deploy(true);
                getLog().log(LogService.LOG_INFO, "Bundle updated from OSGi Bundle Repository");
                return true;
            }
        } else {
            getLog().log(LogService.LOG_INFO, "Nothing to update, OSGi Bundle Repository does not provide more recent version");
        }
    } else {
        getLog().log(LogService.LOG_DEBUG, "Cannot updated from OSGi Bundle Repository: Service not available");
    }
    // fallback to false, nothing done
    return false;
}
Also used : RepositoryAdmin(org.osgi.service.obr.RepositoryAdmin) Resolver(org.osgi.service.obr.Resolver) Resource(org.osgi.service.obr.Resource)

Example 4 with Resource

use of org.osgi.service.obr.Resource in project felix by apache.

the class OsgiBundleRepositoryRenderHelper method doDeploy.

@Override
final void doDeploy(String[] bundles, boolean start, boolean optional) {
    // 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 (bundle == null || bundle.equals("-")) {
            continue;
        }
        String filter = "(id=" + bundle + ")";
        Resource[] resources = repoAdmin.discoverResources(filter);
        if (resources != null && resources.length > 0) {
            resolver.add(resources[0]);
        }
    }
    OsgiDeployer.deploy(resolver, logger, start);
}
Also used : RepositoryAdmin(org.osgi.service.obr.RepositoryAdmin) Resolver(org.osgi.service.obr.Resolver) Resource(org.osgi.service.obr.Resource)

Example 5 with Resource

use of org.osgi.service.obr.Resource in project felix by apache.

the class OsgiBundleRepositoryRenderHelper method toJSON.

private final void toJSON(JSONWriter json, 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();
        }
    }
    json.object();
    // $NON-NLS-1$
    json.key("id");
    json.value(resource.getId());
    // $NON-NLS-1$
    json.key("presentationname");
    json.value(resource.getPresentationName());
    // $NON-NLS-1$
    json.key("symbolicname");
    json.value(symbolicName);
    // $NON-NLS-1$
    json.key("url");
    json.value(resource.getURL());
    // $NON-NLS-1$
    json.key("version");
    json.value(version);
    // $NON-NLS-1$
    json.key("categories");
    json.value(resource.getCategories());
    // $NON-NLS-1$
    json.key("installed");
    json.value(installed);
    if (details) {
        Capability[] caps = resource.getCapabilities();
        if (caps != null) {
            // $NON-NLS-1$
            json.key("capabilities");
            json.array();
            for (int i = 0; i < caps.length; i++) {
                // $NON-NLS-1$
                json.key("name");
                json.value(caps[i].getName());
                // $NON-NLS-1$
                json.key("properties");
                toJSON(json, caps[i].getProperties());
            }
            json.endArray();
        }
        Requirement[] reqs = resource.getRequirements();
        if (caps != null) {
            // $NON-NLS-1$
            json.key("requirements");
            json.array();
            for (int i = 0; i < reqs.length; i++) {
                // $NON-NLS-1$
                json.key("name");
                json.value(reqs[i].getName());
                // $NON-NLS-1$
                json.key("filter");
                json.value(reqs[i].getFilter());
                // $NON-NLS-1$
                json.key("optional");
                json.value(reqs[i].isOptional());
            }
            json.endArray();
        }
        final RepositoryAdmin admin = (RepositoryAdmin) getRepositoryAdmin();
        Resolver resolver = admin.resolver();
        resolver.add(resource);
        // (Resolver.NO_OPTIONAL_RESOURCES);
        resolver.resolve();
        Resource[] required = resolver.getRequiredResources();
        if (required != null) {
            // $NON-NLS-1$
            json.key("required");
            json.array();
            for (int i = 0; i < required.length; i++) {
                toJSON(json, required[i], bundles, false);
            }
            json.endArray();
        }
        Resource[] optional = resolver.getOptionalResources();
        if (optional != null) {
            // $NON-NLS-1$
            json.key("optional");
            json.array();
            for (int i = 0; optional != null && i < optional.length; i++) {
                toJSON(json, optional[i], bundles, false);
            }
            json.endArray();
        }
        Requirement[] /*Reason*/
        unsatisfied = resolver.getUnsatisfiedRequirements();
        if (unsatisfied != null) {
            // $NON-NLS-1$
            json.key("unsatisfied");
            json.array();
            for (int i = 0; i < unsatisfied.length; i++) {
                // $NON-NLS-1$
                json.key("name");
                json.value(unsatisfied[i].getName());
                // $NON-NLS-1$
                json.key("filter");
                json.value(unsatisfied[i].getFilter());
                // $NON-NLS-1$
                json.key("optional");
                json.value(unsatisfied[i].isOptional());
            }
            json.endArray();
        }
    }
    json.endObject();
}
Also used : Requirement(org.osgi.service.obr.Requirement) RepositoryAdmin(org.osgi.service.obr.RepositoryAdmin) Capability(org.osgi.service.obr.Capability) Resolver(org.osgi.service.obr.Resolver) Version(org.osgi.framework.Version) Resource(org.osgi.service.obr.Resource)

Aggregations

RepositoryAdmin (org.osgi.service.obr.RepositoryAdmin)5 Resource (org.osgi.service.obr.Resource)5 Resolver (org.osgi.service.obr.Resolver)4 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ServletException (javax.servlet.ServletException)1 JSONWriter (org.apache.felix.utils.json.JSONWriter)1 ServiceReference (org.osgi.framework.ServiceReference)1 Version (org.osgi.framework.Version)1 Capability (org.osgi.service.obr.Capability)1 Repository (org.osgi.service.obr.Repository)1 Requirement (org.osgi.service.obr.Requirement)1