Search in sources :

Example 11 with RepositoryAdmin

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

the class FelixBundleRepositoryRenderHelper method doAction.

@Override
final void doAction(String action, String urlParam) throws IOException, ServletException {
    RepositoryAdmin admin = (RepositoryAdmin) getRepositoryAdmin();
    Repository[] repos = admin.listRepositories();
    Repository repo = getRepository(repos, urlParam);
    String uri = repo != null ? repo.getURI() : 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.apache.felix.bundlerepository.Repository) RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) IOException(java.io.IOException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 12 with RepositoryAdmin

use of org.apache.felix.bundlerepository.RepositoryAdmin 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)

Example 13 with RepositoryAdmin

use of org.apache.felix.bundlerepository.RepositoryAdmin 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 14 with RepositoryAdmin

use of org.apache.felix.bundlerepository.RepositoryAdmin 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 15 with RepositoryAdmin

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

the class OBRAppManagerTest method testAppWithApplicationManifest.

@Test
public void testAppWithApplicationManifest() throws Exception {
    RepositoryAdmin repositoryAdmin = context().getService(RepositoryAdmin.class);
    repositoryAdmin.addRepository(new File("repository.xml").toURI().toURL());
    Repository[] repos = repositoryAdmin.listRepositories();
    for (Repository repo : repos) {
        Resource[] resources = repo.getResources();
        for (Resource r : resources) {
            Capability[] cs = r.getCapabilities();
            for (Capability c : cs) {
                System.out.println(c.getName() + " : " + c.getProperties());
            }
        }
    }
    AriesApplicationManager manager = context().getService(AriesApplicationManager.class);
    AriesApplication app = manager.createApplication(FileSystem.getFSRoot(new File("test.eba")));
    app = manager.resolve(app);
    // installing requires a valid url for the bundle in repository.xml.
    AriesApplicationContext ctx = manager.install(app);
    ctx.start();
    HelloWorld hw = context().getService(HelloWorld.class);
    String result = hw.getMessage();
    assertEquals(result, "hello world");
    ctx.stop();
    manager.uninstall(ctx);
}
Also used : RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) Capability(org.apache.felix.bundlerepository.Capability) AriesApplicationManager(org.apache.aries.application.management.AriesApplicationManager) Resource(org.apache.felix.bundlerepository.Resource) AriesApplication(org.apache.aries.application.management.AriesApplication) HelloWorld(org.apache.aries.sample.HelloWorld) Repository(org.apache.felix.bundlerepository.Repository) AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) File(java.io.File) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.itest.AbstractIntegrationTest)

Aggregations

RepositoryAdmin (org.apache.felix.bundlerepository.RepositoryAdmin)19 Repository (org.apache.felix.bundlerepository.Repository)15 File (java.io.File)9 AriesApplication (org.apache.aries.application.management.AriesApplication)8 AriesApplicationManager (org.apache.aries.application.management.AriesApplicationManager)8 AbstractIntegrationTest (org.apache.aries.itest.AbstractIntegrationTest)8 Test (org.junit.Test)8 Resource (org.apache.felix.bundlerepository.Resource)7 AriesApplicationContext (org.apache.aries.application.management.AriesApplicationContext)5 DataModelHelper (org.apache.felix.bundlerepository.DataModelHelper)3 BundleContext (org.osgi.framework.BundleContext)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ServletException (javax.servlet.ServletException)2 DeploymentContent (org.apache.aries.application.DeploymentContent)2 DeploymentMetadata (org.apache.aries.application.DeploymentMetadata)2 HelloWorld (org.apache.aries.sample.HelloWorld)2 Capability (org.apache.felix.bundlerepository.Capability)2 Resolver (org.apache.felix.bundlerepository.Resolver)2