use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.
the class ResolverHookTests method testSingletonIdentity.
public void testSingletonIdentity() throws Exception {
final RuntimeException[] error = { null };
final boolean[] called = { false };
ResolverHookFactory resolverHookFactory = new ResolverHookFactory() {
public ResolverHook begin(Collection triggers) {
return new ResolverHook() {
public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
if (error[0] != null)
return;
called[0] = true;
try {
assertEquals("Wrong namespace", IdentityNamespace.IDENTITY_NAMESPACE, singleton.getNamespace());
assertEquals("Wrong singleton directive", "true", singleton.getDirectives().get(IdentityNamespace.CAPABILITY_SINGLETON_DIRECTIVE));
String symbolicName = (String) singleton.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
for (Iterator iCandidates = collisionCandidates.iterator(); iCandidates.hasNext(); ) {
BundleCapability candidate = (BundleCapability) iCandidates.next();
assertEquals("Wrong namespace", IdentityNamespace.IDENTITY_NAMESPACE, candidate.getNamespace());
assertEquals("Wrong singleton directive", "true", candidate.getDirectives().get(IdentityNamespace.CAPABILITY_SINGLETON_DIRECTIVE));
assertEquals("Wrong symbolic name", symbolicName, (String) candidate.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE));
}
} catch (RuntimeException e) {
error[0] = e;
}
}
public void filterResolvable(Collection candidates) {
// nothing
}
public void filterMatches(BundleRequirement requirement, Collection candidates) {
// nothing
}
public void end() {
// nothing
}
};
}
};
ServiceRegistration hookReg = getContext().registerService(ResolverHookFactory.class, resolverHookFactory, null);
try {
Bundle tb1v1 = installer.installBundle("singleton.tb1v1");
Bundle tb1v2 = installer.installBundle("singleton.tb1v2");
assertFalse(getContext().getBundle(0).adapt(FrameworkWiring.class).resolveBundles(Arrays.asList(new Bundle[] { tb1v1, tb1v2 })));
assertTrue("ResolverHook was not called", called[0]);
if (error[0] != null)
throw error[0];
} finally {
hookReg.unregister();
}
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.
the class StateImpl method begin.
private ResolverHook begin(Collection<BundleRevision> triggers) {
ResolverHookFactory current;
synchronized (this.monitor) {
current = this.hookFactory;
}
ResolverHook newHook = current.begin(triggers);
synchronized (this.monitor) {
this.hook = newHook;
}
return newHook;
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.
the class StateImpl method linkDynamicImport.
public ExportPackageDescription linkDynamicImport(BundleDescription importingBundle, String requestedPackage) {
if (resolver == null)
// $NON-NLS-1$
throw new IllegalStateException("no resolver set");
BundleDescriptionImpl importer = (BundleDescriptionImpl) importingBundle;
if (importer.getDynamicStamp(requestedPackage) == getTimeStamp())
return null;
fullyLoad();
synchronized (this.monitor) {
ResolverHook currentHook = null;
try {
resolving = true;
ResolverHookFactory currentFactory = hookFactory;
if (currentFactory != null) {
Collection<BundleRevision> triggers = new ArrayList<>(1);
triggers.add(importingBundle);
triggers = Collections.unmodifiableCollection(triggers);
currentHook = begin(triggers);
}
// ask the resolver to resolve our dynamic import
ExportPackageDescriptionImpl result = (ExportPackageDescriptionImpl) resolver.resolveDynamicImport(importingBundle, requestedPackage);
if (result == null)
importer.setDynamicStamp(requestedPackage, new Long(getTimeStamp()));
else {
// remove any cached timestamp
importer.setDynamicStamp(requestedPackage, null);
// need to add the result to the list of resolved imports
importer.addDynamicResolvedImport(result);
}
setDynamicCacheChanged(true);
return result;
} finally {
resolving = false;
if (currentHook != null)
currentHook.end();
}
}
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.
the class ClassLoadingBundleTests method testBug348805.
public void testBug348805() {
final boolean[] endCalled = { false };
ResolverHookFactory error = new ResolverHookFactory() {
public ResolverHook begin(Collection triggers) {
return new ResolverHook() {
public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
// Nothing
}
public void filterResolvable(Collection candidates) {
throw new RuntimeException("Error");
}
public void filterMatches(BundleRequirement requirement, Collection candidates) {
// Nothing
}
public void end() {
endCalled[0] = true;
}
};
}
};
ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, error, null);
try {
// $NON-NLS-1$
Bundle test = installer.installBundle("test");
try {
test.start();
fail("Should not be able to start this bundle");
} catch (BundleException e) {
// expected
assertEquals("Wrong exception type.", BundleException.REJECTED_BY_HOOK, e.getType());
}
} catch (BundleException e) {
fail("Unexpected install fail", e);
} finally {
reg.unregister();
}
assertTrue("end is not called", endCalled[0]);
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.
the class PackageAdminBundleTests method testUninstallWhileResolving.
public void testUninstallWhileResolving() throws BundleException {
ServiceRegistration<ResolverHookFactory> resolverHookReg = getContext().registerService(ResolverHookFactory.class, new ResolverHookFactory() {
@Override
public ResolverHook begin(Collection<BundleRevision> triggers) {
return new ResolverHook() {
@Override
public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
// Nothing
}
@Override
public void filterResolvable(Collection<BundleRevision> candidates) {
// prevent all resolves
candidates.clear();
}
@Override
public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
// nothing
}
@Override
public void end() {
// nothing
}
};
}
}, null);
try {
// $NON-NLS-1$
Bundle b1 = installer.installBundle("test.uninstall.start1");
// $NON-NLS-1$
Bundle b2 = installer.installBundle("test.uninstall.start2");
try {
b1.start();
} catch (BundleException e) {
// expected
}
try {
b2.start();
} catch (BundleException e) {
// expected
}
resolverHookReg.unregister();
resolverHookReg = null;
getContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).adapt(FrameworkWiring.class).resolveBundles(Arrays.asList(b1, b2));
} finally {
if (resolverHookReg != null) {
resolverHookReg.unregister();
}
}
}
Aggregations