Search in sources :

Example 1 with RepositoryAdmin

use of org.osgi.service.obr.RepositoryAdmin 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 RepositoryAdmin

use of org.osgi.service.obr.RepositoryAdmin 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 RepositoryAdmin

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

the class OsgiBundleRepositoryRenderHelper method doAction.

@Override
void doAction(String action, String urlParam) throws IOException, ServletException {
    RepositoryAdmin admin = (RepositoryAdmin) getRepositoryAdmin();
    Repository[] repos = admin.listRepositories();
    Repository repo = getRepository(repos, urlParam);
    URL uri = repo != null ? repo.getURL() : new URL(urlParam);
    if (// $NON-NLS-1$
    "delete".equals(action)) {
        if (!admin.removeRepository(uri)) {
            throw new ServletException("Failed to remove repository with URL " + uri);
        }
    } else if (// $NON-NLS-1$ //$NON-NLS-2$
    "add".equals(action) || "refresh".equals(action)) {
        try {
            admin.addRepository(uri);
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new ServletException("Failed to " + action + " repository " + uri + ": " + e.toString());
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) Repository(org.osgi.service.obr.Repository) RepositoryAdmin(org.osgi.service.obr.RepositoryAdmin) IOException(java.io.IOException) URL(java.net.URL) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 4 with RepositoryAdmin

use of org.osgi.service.obr.RepositoryAdmin 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 5 with RepositoryAdmin

use of org.osgi.service.obr.RepositoryAdmin 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)

Aggregations

RepositoryAdmin (org.osgi.service.obr.RepositoryAdmin)6 Resource (org.osgi.service.obr.Resource)5 Resolver (org.osgi.service.obr.Resolver)4 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 Repository (org.osgi.service.obr.Repository)2 StringWriter (java.io.StringWriter)1 URL (java.net.URL)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 Requirement (org.osgi.service.obr.Requirement)1