use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class BundleWiringImpl method getRequirements.
@Override
public List<BundleRequirement> getRequirements(String namespace) {
if (isInUse()) {
List<BundleRequirement> searchReqs = m_resolvedReqs;
List<BundleRequirement> wovenReqs = m_wovenReqs;
List<BundleRequirement> result = m_resolvedReqs;
if (wovenReqs != null) {
searchReqs = new ArrayList<BundleRequirement>(m_resolvedReqs);
searchReqs.addAll(wovenReqs);
result = searchReqs;
}
if (namespace != null) {
result = new ArrayList<BundleRequirement>();
for (BundleRequirement req : searchReqs) {
if (req.getNamespace().equals(namespace)) {
result.add(req);
}
}
}
return result;
}
return null;
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class BundleWiringImpl method diagnoseClassLoadError.
private static String diagnoseClassLoadError(StatefulResolver resolver, BundleRevision revision, String name) {
// We will try to do some diagnostics here to help the developer
// deal with this exception.
// Get package name.
String pkgName = Util.getClassPackage(name);
if (pkgName.length() == 0) {
return null;
}
// First, get the bundle string of the revision doing the class loader.
String importer = revision.getBundle().toString();
// Next, check to see if the revision imports the package.
List<BundleWire> wires = (revision.getWiring() == null) ? null : revision.getWiring().getProvidedWires(null);
for (int i = 0; (wires != null) && (i < wires.size()); i++) {
if (wires.get(i).getCapability().getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE) && wires.get(i).getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(pkgName)) {
String exporter = wires.get(i).getProviderWiring().getBundle().toString();
StringBuffer sb = new StringBuffer("*** Package '");
sb.append(pkgName);
sb.append("' is imported by bundle ");
sb.append(importer);
sb.append(" from bundle ");
sb.append(exporter);
sb.append(", but the exported package from bundle ");
sb.append(exporter);
sb.append(" does not contain the requested class '");
sb.append(name);
sb.append("'. Please verify that the class name is correct in the importing bundle ");
sb.append(importer);
sb.append(" and/or that the exported package is correctly bundled in ");
sb.append(exporter);
sb.append(". ***");
return sb.toString();
}
}
// Next, check to see if the package was optionally imported and
// whether or not there is an exporter available.
List<BundleRequirement> reqs = revision.getWiring().getRequirements(null);
// Next, check to see if the package is dynamically imported by the revision.
if (resolver.isAllowedDynamicImport(revision, pkgName)) {
// Try to see if there is an exporter available.
Map<String, String> dirs = Collections.EMPTY_MAP;
Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, dirs, attrs);
List<BundleCapability> exporters = resolver.findProviders(req, false);
BundleRevision provider = null;
try {
provider = resolver.resolve(revision, pkgName);
} catch (Exception ex) {
provider = null;
}
String exporter = (exporters.isEmpty()) ? null : exporters.iterator().next().toString();
StringBuffer sb = new StringBuffer("*** Class '");
sb.append(name);
sb.append("' was not found, but this is likely normal since package '");
sb.append(pkgName);
sb.append("' is dynamically imported by bundle ");
sb.append(importer);
sb.append(".");
if ((exporters.size() > 0) && (provider == null)) {
sb.append(" However, bundle ");
sb.append(exporter);
sb.append(" does export this package with attributes that do not match.");
}
sb.append(" ***");
return sb.toString();
}
// Next, check to see if there are any exporters for the package at all.
Map<String, String> dirs = Collections.EMPTY_MAP;
Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, dirs, attrs);
List<BundleCapability> exports = resolver.findProviders(req, false);
if (exports.size() > 0) {
boolean classpath = false;
try {
BundleRevisionImpl.getSecureAction().getClassLoader(BundleClassLoader.class).loadClass(name);
classpath = true;
} catch (NoClassDefFoundError err) {
// Ignore
} catch (Exception ex) {
// Ignore
}
String exporter = exports.iterator().next().toString();
StringBuffer sb = new StringBuffer("*** Class '");
sb.append(name);
sb.append("' was not found because bundle ");
sb.append(importer);
sb.append(" does not import '");
sb.append(pkgName);
sb.append("' even though bundle ");
sb.append(exporter);
sb.append(" does export it.");
if (classpath) {
sb.append(" Additionally, the class is also available from the system class loader. There are two fixes: 1) Add an import for '");
sb.append(pkgName);
sb.append("' to bundle ");
sb.append(importer);
sb.append("; imports are necessary for each class directly touched by bundle code or indirectly touched, such as super classes if their methods are used. ");
sb.append("2) Add package '");
sb.append(pkgName);
sb.append("' to the '");
sb.append(Constants.FRAMEWORK_BOOTDELEGATION);
sb.append("' property; a library or VM bug can cause classes to be loaded by the wrong class loader. The first approach is preferable for preserving modularity.");
} else {
sb.append(" To resolve this issue, add an import for '");
sb.append(pkgName);
sb.append("' to bundle ");
sb.append(importer);
sb.append(".");
}
sb.append(" ***");
return sb.toString();
}
// class loader.
try {
BundleRevisionImpl.getSecureAction().getClassLoader(BundleClassLoader.class).loadClass(name);
StringBuffer sb = new StringBuffer("*** Package '");
sb.append(pkgName);
sb.append("' is not imported by bundle ");
sb.append(importer);
sb.append(", nor is there any bundle that exports package '");
sb.append(pkgName);
sb.append("'. However, the class '");
sb.append(name);
sb.append("' is available from the system class loader. There are two fixes: 1) Add package '");
sb.append(pkgName);
sb.append("' to the '");
sb.append(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
sb.append("' property and modify bundle ");
sb.append(importer);
sb.append(" to import this package; this causes the system bundle to export class path packages. 2) Add package '");
sb.append(pkgName);
sb.append("' to the '");
sb.append(Constants.FRAMEWORK_BOOTDELEGATION);
sb.append("' property; a library or VM bug can cause classes to be loaded by the wrong class loader. The first approach is preferable for preserving modularity.");
sb.append(" ***");
return sb.toString();
} catch (Exception ex2) {
}
// Finally, if there are no imports or exports for the package
// and it is not available on the system class path, simply
// log a message saying so.
StringBuffer sb = new StringBuffer("*** Class '");
sb.append(name);
sb.append("' was not found. Bundle ");
sb.append(importer);
sb.append(" does not import package '");
sb.append(pkgName);
sb.append("', nor is the package exported by any other bundle or available from the system class loader.");
sb.append(" ***");
return sb.toString();
}
use of org.osgi.framework.wiring.BundleRequirement in project rt.equinox.framework by eclipse.
the class TestModuleContainer method testR3.
@Test
public void testR3() throws BundleException, IOException {
DummyContainerAdaptor adaptor = createDummyAdaptor();
ModuleContainer container = adaptor.getContainer();
// install the system.bundle
Module systemBundle = installDummyModule("system.bundle.MF", Constants.SYSTEM_BUNDLE_LOCATION, Constants.SYSTEM_BUNDLE_SYMBOLICNAME, null, null, container);
ResolutionReport report = container.resolve(Arrays.asList(systemBundle), true);
Assert.assertNull("Failed to resolve system.bundle.", report.getResolutionException());
// R3 bundle
Map<String, String> exporterManifest = new HashMap<String, String>();
exporterManifest = new HashMap<String, String>();
exporterManifest.put(Constants.BUNDLE_SYMBOLICNAME, "exporter");
exporterManifest.put(Constants.EXPORT_PACKAGE, "exporter; version=\"1.1\"");
Module moduleExport = installDummyModule(exporterManifest, "exporter", container);
report = container.resolve(Arrays.asList(moduleExport, moduleExport), true);
Assert.assertNull("Failed to resolve", report.getResolutionException());
List<BundleRequirement> reqs = moduleExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
assertEquals("Wrong number of imports.", 0, reqs.size());
// R3 bundle
exporterManifest.clear();
exporterManifest.put(Constants.BUNDLE_SYMBOLICNAME, "dynamicExporter");
exporterManifest.put(Constants.EXPORT_PACKAGE, "exporter; version=\"1.0\"");
exporterManifest.put(Constants.DYNAMICIMPORT_PACKAGE, "exporter");
Module moduleWithDynExport = installDummyModule(exporterManifest, "dynamicExporter", container);
report = container.resolve(Arrays.asList(moduleWithDynExport), true);
Assert.assertNull("Failed to resolve", report.getResolutionException());
reqs = moduleWithDynExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
assertEquals("Wrong number of imports.", 2, reqs.size());
report = container.resolve(Arrays.asList(moduleWithDynExport), true);
Assert.assertNull("Failed to resolve", report.getResolutionException());
reqs = moduleWithDynExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
assertEquals("Wrong number of imports.", 2, reqs.size());
ModuleWiring wiring = moduleWithDynExport.getCurrentRevision().getWiring();
List<ModuleWire> packageWires = wiring.getRequiredModuleWires(PackageNamespace.PACKAGE_NAMESPACE);
Assert.assertEquals("Unexpected number of wires", 1, packageWires.size());
Assert.assertEquals("Wrong exporter", packageWires.get(0).getProviderWiring().getRevision(), moduleExport.getCurrentRevision());
}
use of org.osgi.framework.wiring.BundleRequirement in project rt.equinox.framework by eclipse.
the class TestModuleContainer method testSingleton02.
@Test
public void testSingleton02() throws BundleException, IOException {
ResolverHookFactory resolverHookFactory = new ResolverHookFactory() {
@Override
public ResolverHook begin(Collection<BundleRevision> triggers) {
return new ResolverHook() {
@Override
public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
collisionCandidates.clear();
}
@Override
public void filterResolvable(Collection<BundleRevision> candidates) {
// nothing
}
@Override
public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
// nothing
}
@Override
public void end() {
// nothing
}
};
}
};
DummyContainerAdaptor adaptor = new DummyContainerAdaptor(new DummyCollisionHook(false), Collections.<String, String>emptyMap(), resolverHookFactory);
ModuleContainer container = adaptor.getContainer();
Module s1 = installDummyModule("singleton1_v1.MF", "s1_v1", container);
Module s2 = installDummyModule("singleton1_v2.MF", "s1_v2", container);
Module s3 = installDummyModule("singleton1_v3.MF", "s1_v3", container);
container.resolve(null, false);
Assert.assertTrue("Singleton v1 is not resolved.", Module.RESOLVED_SET.contains(s1.getState()));
Assert.assertTrue("Singleton v2 is not resolved.", Module.RESOLVED_SET.contains(s2.getState()));
Assert.assertTrue("Singleton v3 is not resolved.", Module.RESOLVED_SET.contains(s3.getState()));
}
use of org.osgi.framework.wiring.BundleRequirement in project rt.equinox.framework by eclipse.
the class SystemBundleTests method doTestBug351519Refresh.
private void doTestBug351519Refresh(Boolean refreshDuplicates) {
// Create a framework with equinox.refresh.duplicate.bsn=false configuration
// $NON-NLS-1$
File config = OSGiTestsActivator.getContext().getDataFile(getName());
Map<String, Object> configuration = new HashMap<String, Object>();
configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
if (refreshDuplicates != null) {
configuration.put("equinox.refresh.duplicate.bsn", refreshDuplicates.toString());
} else {
// we default to false now
refreshDuplicates = Boolean.FALSE;
}
Equinox equinox = new Equinox(configuration);
try {
equinox.start();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected exception in init()", e);
}
BundleContext systemContext = equinox.getBundleContext();
systemContext.registerService(ResolverHookFactory.class, new ResolverHookFactory() {
public ResolverHook begin(Collection triggers) {
return new ResolverHook() {
public void filterResolvable(Collection candidates) {
// nothing
}
public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
// resolve all singletons
collisionCandidates.clear();
}
public void filterMatches(BundleRequirement requirement, Collection candidates) {
// nothing
}
public void end() {
// nothing
}
};
}
}, null);
BundleInstaller testBundleInstaller = null;
BundleInstaller testBundleResolver = null;
try {
testBundleResolver = new BundleInstaller(OSGiTestsActivator.TEST_FILES_ROOT + "wiringTests/bundles", systemContext);
testBundleInstaller = new BundleInstaller(OSGiTestsActivator.TEST_FILES_ROOT + "wiringTests/bundles", getContext());
} catch (InvalidSyntaxException e) {
fail("Failed to create installers.", e);
}
// $NON-NLS-1$
assertNotNull("System context is null", systemContext);
// try installing a bundle before starting
Bundle tb1v1 = null, tb1v2 = null;
try {
// $NON-NLS-1$
tb1v1 = systemContext.installBundle(testBundleInstaller.getBundleLocation("singleton.tb1v1"));
// $NON-NLS-1$
tb1v2 = systemContext.installBundle(testBundleInstaller.getBundleLocation("singleton.tb1v2"));
} catch (BundleException e1) {
// $NON-NLS-1$
fail("failed to install a bundle", e1);
}
assertTrue("Could not resolve test bundles", testBundleResolver.resolveBundles(new Bundle[] { tb1v1, tb1v2 }));
Bundle[] refreshed = testBundleResolver.refreshPackages(new Bundle[] { tb1v1 });
if (refreshDuplicates) {
List refreshedList = Arrays.asList(refreshed);
assertEquals("Wrong number of refreshed bundles", 2, refreshed.length);
assertTrue("Refreshed bundles does not include v1", refreshedList.contains(tb1v1));
assertTrue("Refreshed bundles does not include v2", refreshedList.contains(tb1v2));
} else {
assertEquals("Wrong number of refreshed bundles", 1, refreshed.length);
assertEquals("Refreshed bundles does not include v1", refreshed[0], tb1v1);
}
try {
equinox.stop();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected erorr stopping framework", e);
}
try {
equinox.waitForStop(10000);
} catch (InterruptedException e) {
// $NON-NLS-1$
fail("Unexpected interrupted exception", e);
}
// $NON-NLS-1$
assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
}
Aggregations