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());
}
}
}
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();
}
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);
}
}
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 "{}";
}
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);
}
Aggregations