use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.
the class TestModuleContainer method getDatabase.
private DummyModuleDatabase getDatabase() throws BundleException {
BundleContext context = ((BundleReference) getClass().getClassLoader()).getBundle().getBundleContext();
DummyContainerAdaptor adaptor = createDummyAdaptor();
final ModuleContainer container = adaptor.getContainer();
Bundle systemBundle = context.getBundle(0);
String extraPackages = context.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
String extraCapabilities = context.getProperty(Constants.FRAMEWORK_SYSTEMCAPABILITIES);
extraCapabilities = (extraCapabilities == null ? "" : (extraCapabilities + ", "));
String osName = context.getProperty(OSGI_OS);
String wsName = context.getProperty(OSGI_WS);
String archName = context.getProperty(OSGI_ARCH);
extraCapabilities += EclipsePlatformNamespace.ECLIPSE_PLATFORM_NAMESPACE + "; " + OSGI_OS + "=" + osName + "; " + OSGI_WS + "=" + wsName + "; " + OSGI_ARCH + "=" + archName;
ModuleRevisionBuilder systembuilder = OSGiManifestBuilderFactory.createBuilder(asMap(systemBundle.getHeaders("")), Constants.SYSTEM_BUNDLE_SYMBOLICNAME, extraPackages, extraCapabilities);
container.install(null, systemBundle.getLocation(), systembuilder, null);
final List<Throwable> installErrors = new ArrayList<Throwable>(0);
// just trying to pound the container with a bunch of installs
ExecutorService executor = Executors.newFixedThreadPool(10);
Bundle[] bundles = context.getBundles();
for (final Bundle bundle : bundles) {
if (bundle.getBundleId() == 0)
continue;
executor.execute(new Runnable() {
@Override
public void run() {
try {
ModuleRevisionBuilder builder = OSGiManifestBuilderFactory.createBuilder(asMap(bundle.getHeaders("")));
container.install(null, bundle.getLocation(), builder, null);
} catch (Throwable t) {
t.printStackTrace();
synchronized (installErrors) {
installErrors.add(t);
}
}
}
});
}
executor.shutdown();
try {
executor.awaitTermination(2, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (installErrors) {
if (!installErrors.isEmpty()) {
Assert.assertNull("Unexpected install errors.", installErrors);
}
}
container.resolve(new ArrayList<Module>(), false);
List<Module> modules = container.getModules();
for (Module module : modules) {
if (module.getCurrentRevision().getWiring() == null) {
System.out.println("Could not resolve module: " + module.getCurrentRevision());
}
}
return adaptor.getDatabase();
}
use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.
the class TestModuleContainer method testStoreInvalidAttributes.
@Test
public void testStoreInvalidAttributes() throws BundleException, IOException {
DummyContainerAdaptor adaptor = createDummyAdaptor();
ModuleContainer container = adaptor.getContainer();
// install the system.bundle
installDummyModule("system.bundle.MF", Constants.SYSTEM_BUNDLE_LOCATION, Constants.SYSTEM_BUNDLE_SYMBOLICNAME, null, null, container);
Integer testInt = Integer.valueOf(1);
List<Integer> testIntList = Collections.singletonList(testInt);
ModuleRevisionBuilder builder = new ModuleRevisionBuilder();
builder.setSymbolicName("invalid.attr");
builder.setVersion(Version.valueOf("1.0.0"));
builder.addCapability("test", Collections.<String, String>emptyMap(), Collections.singletonMap("test", (Object) testInt));
builder.addCapability("test.list", Collections.<String, String>emptyMap(), Collections.singletonMap("test.list", (Object) testIntList));
Module invalid = container.install(null, builder.getSymbolicName(), builder, null);
Object testAttr = invalid.getCurrentRevision().getCapabilities("test").get(0).getAttributes().get("test");
assertEquals("Wrong test attr", testInt, testAttr);
Object testAttrList = invalid.getCurrentRevision().getCapabilities("test.list").get(0).getAttributes().get("test.list");
assertEquals("Wrong test list attr", testIntList, testAttrList);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream data = new DataOutputStream(bytes);
adaptor.getDatabase().store(data, true);
List<DummyContainerEvent> events = adaptor.getDatabase().getContainerEvents();
// make sure we see the errors
assertEquals("Wrong number of events.", 2, events.size());
for (DummyContainerEvent event : events) {
assertEquals("Wrong type of event.", ContainerEvent.ERROR, event.type);
assertTrue("Wrong type of exception.", event.error instanceof BundleException);
}
// reload into a new container
adaptor = createDummyAdaptor();
container = adaptor.getContainer();
adaptor.getDatabase().load(new DataInputStream(new ByteArrayInputStream(bytes.toByteArray())));
invalid = container.getModule("invalid.attr");
assertNotNull("Could not find module.", invalid);
String testIntString = String.valueOf(testInt);
List<String> testIntStringList = Collections.singletonList(testIntString);
testAttr = invalid.getCurrentRevision().getCapabilities("test").get(0).getAttributes().get("test");
assertEquals("Wrong test attr", testIntString, testAttr);
testAttrList = invalid.getCurrentRevision().getCapabilities("test.list").get(0).getAttributes().get("test.list");
assertEquals("Wrong test list attr", testIntStringList, testAttrList);
}
use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.
the class Storage method getBuilder.
public ModuleRevisionBuilder getBuilder(Generation generation) throws BundleException {
Dictionary<String, String> headers = generation.getHeaders();
Map<String, String> mapHeaders;
if (headers instanceof Map) {
@SuppressWarnings("unchecked") Map<String, String> unchecked = (Map<String, String>) headers;
mapHeaders = unchecked;
} else {
mapHeaders = new HashMap<>();
for (Enumeration<String> eKeys = headers.keys(); eKeys.hasMoreElements(); ) {
String key = eKeys.nextElement();
mapHeaders.put(key, headers.get(key));
}
}
if (generation.getBundleInfo().getBundleId() != 0) {
// $NON-NLS-1$
ModuleRevisionBuilder builder = allowRestrictedProvides ? OSGiManifestBuilderFactory.createBuilder(mapHeaders, null, null, "") : OSGiManifestBuilderFactory.createBuilder(mapHeaders);
if ((builder.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
for (ModuleRevisionBuilder.GenericInfo reqInfo : builder.getRequirements()) {
if (HostNamespace.HOST_NAMESPACE.equals(reqInfo.getNamespace())) {
if (HostNamespace.EXTENSION_BOOTCLASSPATH.equals(reqInfo.getDirectives().get(HostNamespace.REQUIREMENT_EXTENSION_DIRECTIVE))) {
// $NON-NLS-1$
throw new BundleException("Boot classpath extensions are not supported.", BundleException.UNSUPPORTED_OPERATION, new UnsupportedOperationException());
}
}
}
}
return builder;
}
// First we must make sure the VM profile has been loaded
loadVMProfile(generation);
// dealing with system bundle find the extra capabilities and exports
String extraCapabilities = getSystemExtraCapabilities();
String extraExports = getSystemExtraPackages();
return OSGiManifestBuilderFactory.createBuilder(mapHeaders, Constants.SYSTEM_BUNDLE_SYMBOLICNAME, extraExports, extraCapabilities);
}
use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.
the class OSGiManifestBuilderFactory method createBuilder.
/**
* Creates a builder for the specified bundle manifest. An alias can be supplied
* for the symbolic name. Also extra package exports and extra provided capabilities
* may be specified outside of the supplied manifest. This is useful for creating
* a builder for the system module which takes into account the configuration
* properties {@link Constants#FRAMEWORK_SYSTEMPACKAGES_EXTRA} and
* {@link Constants#FRAMEWORK_SYSTEMCAPABILITIES_EXTRA}.
* @param manifest the bundle manifest
* @param symbolicNameAlias the symbolic name alias. A <code>null</code> value is allowed.
* @param extraExports the extra package exports. A <code>null</code> value is allowed.
* @param extraCapabilities the extra provided capabilities. A <code>null</code> value is allowed.
* @return a builder for the specified bundle manifest
* @throws BundleException if the bundle manifest is invalid
*/
public static ModuleRevisionBuilder createBuilder(Map<String, String> manifest, String symbolicNameAlias, String extraExports, String extraCapabilities) throws BundleException {
ModuleRevisionBuilder builder = new ModuleRevisionBuilder();
int manifestVersion = getManifestVersion(manifest);
if (manifestVersion >= 2) {
validateHeaders(manifest);
}
Object symbolicName = getSymbolicNameAndVersion(builder, manifest, symbolicNameAlias, manifestVersion);
Collection<Map<String, Object>> exportedPackages = new ArrayList<>();
getPackageExports(builder, ManifestElement.parseHeader(Constants.EXPORT_PACKAGE, manifest.get(Constants.EXPORT_PACKAGE)), symbolicName, exportedPackages);
getPackageExports(builder, ManifestElement.parseHeader(HEADER_OLD_PROVIDE_PACKAGE, manifest.get(HEADER_OLD_PROVIDE_PACKAGE)), symbolicName, exportedPackages);
if (extraExports != null && !extraExports.isEmpty()) {
getPackageExports(builder, ManifestElement.parseHeader(Constants.EXPORT_PACKAGE, extraExports), symbolicName, exportedPackages);
}
getPackageImports(builder, manifest, exportedPackages, manifestVersion);
getRequireBundle(builder, ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, manifest.get(Constants.REQUIRE_BUNDLE)));
getProvideCapabilities(builder, ManifestElement.parseHeader(Constants.PROVIDE_CAPABILITY, manifest.get(Constants.PROVIDE_CAPABILITY)), extraCapabilities == null);
if (extraCapabilities != null && !extraCapabilities.isEmpty()) {
getProvideCapabilities(builder, ManifestElement.parseHeader(Constants.PROVIDE_CAPABILITY, extraCapabilities), false);
}
getRequireCapabilities(builder, ManifestElement.parseHeader(Constants.REQUIRE_CAPABILITY, manifest.get(Constants.REQUIRE_CAPABILITY)));
addRequireEclipsePlatform(builder, manifest);
getEquinoxDataCapability(builder, manifest);
getFragmentHost(builder, ManifestElement.parseHeader(Constants.FRAGMENT_HOST, manifest.get(Constants.FRAGMENT_HOST)));
convertBREEs(builder, manifest);
getNativeCode(builder, manifest);
return builder;
}
use of org.eclipse.osgi.container.ModuleRevisionBuilder in project rt.equinox.framework by eclipse.
the class Storage method checkSystemBundle.
private void checkSystemBundle() {
Module systemModule = moduleContainer.getModule(0);
Generation newGeneration = null;
try {
if (systemModule == null) {
BundleInfo info = new BundleInfo(this, 0, Constants.SYSTEM_BUNDLE_LOCATION, 0);
newGeneration = info.createGeneration();
File contentFile = getSystemContent();
newGeneration.setContent(contentFile, false);
ModuleRevisionBuilder builder = getBuilder(newGeneration);
systemModule = moduleContainer.install(null, Constants.SYSTEM_BUNDLE_LOCATION, builder, newGeneration);
moduleContainer.resolve(Arrays.asList(systemModule), false);
} else {
ModuleRevision currentRevision = systemModule.getCurrentRevision();
Generation currentGeneration = currentRevision == null ? null : (Generation) currentRevision.getRevisionInfo();
if (currentGeneration == null) {
// $NON-NLS-1$
throw new IllegalStateException("No current revision for system bundle.");
}
try {
ModuleRevisionBuilder newBuilder = getBuilder(currentGeneration);
if (needUpdate(currentRevision, newBuilder)) {
newGeneration = currentGeneration.getBundleInfo().createGeneration();
File contentFile = getSystemContent();
newGeneration.setContent(contentFile, false);
moduleContainer.update(systemModule, newBuilder, newGeneration);
moduleContainer.refresh(Collections.singleton(systemModule));
} else {
if (currentRevision.getWiring() == null) {
// must resolve before continuing to ensure extensions get attached
moduleContainer.resolve(Collections.singleton(systemModule), true);
}
}
} catch (BundleException e) {
// $NON-NLS-1$
throw new IllegalStateException("Could not create a builder for the system bundle.", e);
}
}
ModuleRevision currentRevision = systemModule.getCurrentRevision();
List<ModuleCapability> nativeEnvironments = currentRevision.getModuleCapabilities(NativeNamespace.NATIVE_NAMESPACE);
Map<String, Object> configMap = equinoxContainer.getConfiguration().getInitialConfig();
for (ModuleCapability nativeEnvironment : nativeEnvironments) {
nativeEnvironment.setTransientAttrs(configMap);
}
// $NON-NLS-1$ //$NON-NLS-2$
Requirement osgiPackageReq = ModuleContainer.createRequirement(PackageNamespace.PACKAGE_NAMESPACE, Collections.singletonMap(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(" + PackageNamespace.PACKAGE_NAMESPACE + "=org.osgi.framework)"), Collections.<String, String>emptyMap());
Collection<BundleCapability> osgiPackages = moduleContainer.getFrameworkWiring().findProviders(osgiPackageReq);
for (BundleCapability packageCapability : osgiPackages) {
if (packageCapability.getRevision().getBundle().getBundleId() == 0) {
Version v = (Version) packageCapability.getAttributes().get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
if (v != null) {
this.equinoxContainer.getConfiguration().setConfiguration(Constants.FRAMEWORK_VERSION, v.toString());
break;
}
}
}
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
// $NON-NLS-1$
throw new RuntimeException("Error occurred while checking the system module.", e);
} finally {
if (newGeneration != null) {
newGeneration.getBundleInfo().unlockGeneration(newGeneration);
}
}
}
Aggregations