use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.
the class TestModuleContainer method testSingleton04.
@Test
public void testSingleton04() throws BundleException, IOException {
final Collection<BundleRevision> disabled = new ArrayList<BundleRevision>();
ResolverHookFactory resolverHookFactory = 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) {
candidates.removeAll(disabled);
}
@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_v1 = installDummyModule("singleton1_v1.MF", "s1_v1", container);
Module s1_v2 = installDummyModule("singleton1_v2.MF", "s1_v2", container);
Module s1_v3 = installDummyModule("singleton1_v3.MF", "s1_v3", container);
Module s2_v1 = installDummyModule("singleton2_v1.MF", "s1_v1", container);
Module s2_v2 = installDummyModule("singleton2_v2.MF", "s1_v2", container);
Module s2_v3 = installDummyModule("singleton2_v3.MF", "s1_v3", container);
container.resolve(null, false);
Assert.assertFalse("Singleton v1 is resolved.", Module.RESOLVED_SET.contains(s1_v1.getState()));
Assert.assertFalse("Singleton v2 is resolved.", Module.RESOLVED_SET.contains(s1_v2.getState()));
Assert.assertTrue("Singleton v3 is not resolved.", Module.RESOLVED_SET.contains(s1_v3.getState()));
Assert.assertFalse("client v1 is resolved.", Module.RESOLVED_SET.contains(s2_v1.getState()));
Assert.assertFalse("client v2 is resolved.", Module.RESOLVED_SET.contains(s2_v2.getState()));
Assert.assertTrue("client v3 is not resolved.", Module.RESOLVED_SET.contains(s2_v3.getState()));
// now disable s1_v3
disabled.add(s1_v3.getCurrentRevision());
container.refresh(Arrays.asList(s1_v3));
Assert.assertFalse("Singleton v1 is resolved.", Module.RESOLVED_SET.contains(s1_v1.getState()));
Assert.assertTrue("Singleton v2 is not resolved.", Module.RESOLVED_SET.contains(s1_v2.getState()));
Assert.assertFalse("Singleton v3 is resolved.", Module.RESOLVED_SET.contains(s1_v3.getState()));
Assert.assertFalse("client v1 is resolved.", Module.RESOLVED_SET.contains(s2_v1.getState()));
Assert.assertTrue("client v2 is not resolved.", Module.RESOLVED_SET.contains(s2_v2.getState()));
Assert.assertFalse("client v3 is resolved.", Module.RESOLVED_SET.contains(s2_v3.getState()));
// now disable s1_v2
disabled.add(s1_v2.getCurrentRevision());
container.refresh(Arrays.asList(s1_v2));
Assert.assertTrue("Singleton v1 is not resolved.", Module.RESOLVED_SET.contains(s1_v1.getState()));
Assert.assertFalse("Singleton v2 is resolved.", Module.RESOLVED_SET.contains(s1_v2.getState()));
Assert.assertFalse("Singleton v3 is resolved.", Module.RESOLVED_SET.contains(s1_v3.getState()));
Assert.assertTrue("client v1 is not resolved.", Module.RESOLVED_SET.contains(s2_v1.getState()));
Assert.assertFalse("client v2 is resolved.", Module.RESOLVED_SET.contains(s2_v2.getState()));
Assert.assertFalse("client v3 is resolved.", Module.RESOLVED_SET.contains(s2_v3.getState()));
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.
the class SystemBundleTests method testBug413879.
public void testBug413879() {
// $NON-NLS-1$
File config = OSGiTestsActivator.getContext().getDataFile(getName());
Map<String, Object> configuration = new HashMap<String, Object>();
configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
Equinox equinox = new Equinox(configuration);
try {
equinox.start();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected exception in start()", e);
}
BundleContext systemContext = equinox.getBundleContext();
// $NON-NLS-1$
assertNotNull("System context is null", systemContext);
Bundle test1 = null;
try {
test1 = systemContext.installBundle(installer.getBundleLocation("substitutes.a"));
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected error installing bundle", e);
}
final Bundle testFinal1 = test1;
ServiceRegistration reg = systemContext.registerService(WeavingHook.class, new WeavingHook() {
public void weave(WovenClass wovenClass) {
if (!testFinal1.equals(wovenClass.getBundleWiring().getBundle()))
return;
if (!"substitutes.x.Ax".equals(wovenClass.getClassName()))
return;
List dynamicImports = wovenClass.getDynamicImports();
dynamicImports.add("*");
}
}, null);
ServiceRegistration<ResolverHookFactory> resolverHookReg = systemContext.registerService(ResolverHookFactory.class, new ResolverHookFactory() {
@Override
public ResolverHook begin(Collection<BundleRevision> triggers) {
// just trying to delay the resolve so that we get two threads trying to apply off the same snapshot
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
return null;
}
}, null);
final Set<Throwable> errors = Collections.newSetFromMap(new ConcurrentHashMap<Throwable, Boolean>());
try {
Runnable dynamicLoadClass = new Runnable() {
@Override
public void run() {
try {
testFinal1.loadClass("substitutes.x.Ax");
testFinal1.loadClass("org.osgi.framework.hooks.bundle.FindHook");
} catch (Throwable t) {
errors.add(t);
}
}
};
Thread t1 = new Thread(dynamicLoadClass, getName() + "-1");
Thread t2 = new Thread(dynamicLoadClass, getName() + "-2");
t1.start();
t2.start();
t1.join();
t2.join();
} catch (Throwable t) {
fail("Unexpected testing bundle", t);
} finally {
reg.unregister();
resolverHookReg.unregister();
}
// put the framework back to the RESOLVED state
try {
equinox.stop();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected error stopping framework", e);
}
try {
equinox.waitForStop(10000);
} catch (InterruptedException e) {
// $NON-NLS-1$
fail("Unexpected interrupted exception", e);
}
if (!errors.isEmpty()) {
fail("Failed to resolve dynamic", errors.iterator().next());
}
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project aries by apache.
the class Aries1383Test method test11.
/*
* (11) Subsystem with apache-aries-provision-dependencies:=resolve undergoes
* the following state transitions when starting fails due to a runtime
* resolution failure: INSTALLING -> INSTALLED -> RESOLVING -> INSTALLED.
*/
@Test
public void test11() throws Exception {
Subsystem root = getRootSubsystem();
subsystemEvents.clear();
Subsystem subsystem = root.install(APPLICATION_DEPENDENCY_IN_ARCHIVE, applicationDependencyInArchive());
ServiceRegistration<ResolverHookFactory> registration = bundleContext.registerService(ResolverHookFactory.class, new ResolverHookFactory() {
@Override
public ResolverHook begin(Collection<BundleRevision> triggers) {
return new ResolverHook() {
@Override
public void filterResolvable(Collection<BundleRevision> candidates) {
for (Iterator<BundleRevision> i = candidates.iterator(); i.hasNext(); ) {
BundleRevision revision = i.next();
if (revision.getSymbolicName().equals(BUNDLE_B)) {
i.remove();
}
}
}
@Override
public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
// Nothing.
}
@Override
public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
// Nothing.
}
@Override
public void end() {
// Nothing.
}
};
}
}, null);
try {
subsystem.start();
stopSubsystemSilently(subsystem);
fail("Subsystem should not have started");
} catch (SubsystemException e) {
e.printStackTrace();
long id = lastSubsystemId();
assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLING, subsystemEvents.poll(id, 5000), ServiceEvent.REGISTERED);
assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLED, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.RESOLVING, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLED, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
} finally {
registration.unregister();
uninstallSubsystemSilently(subsystem);
}
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project felix by apache.
the class StatefulResolver method prepareResolverHooks.
private ResolverHookRecord prepareResolverHooks(Set<BundleRevision> mandatory, Set<BundleRevision> optional) throws BundleException, ResolutionException {
// This map maps the hook factory service to the actual hook objects. It
// needs to be a map that preserves insertion order to ensure that we call
// hooks in the correct order.
// The hooks are added in the order that m_felix.getHooks() returns them which
// is also the order in which they should be called.
Map<ServiceReference<ResolverHookFactory>, ResolverHook> hookMap = new LinkedHashMap<ServiceReference<ResolverHookFactory>, ResolverHook>();
// Get resolver hook factories.
Set<ServiceReference<ResolverHookFactory>> hookRefs = m_felix.getHookRegistry().getHooks(ResolverHookFactory.class);
Collection<BundleRevision> whitelist;
if (!hookRefs.isEmpty()) {
// Create triggers list.
Set<BundleRevision> triggers;
if (!mandatory.isEmpty() && !optional.isEmpty()) {
triggers = new HashSet<BundleRevision>(mandatory);
triggers.addAll(optional);
} else {
triggers = (mandatory.isEmpty()) ? optional : mandatory;
}
triggers = Collections.unmodifiableSet(triggers);
BundleException rethrow = null;
// Create resolver hook objects by calling begin() on factory.
for (ServiceReference<ResolverHookFactory> ref : hookRefs) {
try {
ResolverHookFactory rhf = m_felix.getService(m_felix, ref, false);
if (rhf != null) {
ResolverHook hook = Felix.m_secureAction.invokeResolverHookFactory(rhf, triggers);
if (hook != null) {
hookMap.put(ref, hook);
}
}
} catch (Throwable ex) {
rethrow = new BundleException("Resolver hook exception: " + ex.getMessage(), BundleException.REJECTED_BY_HOOK, ex);
// So we break here to make sure that no further resolver hooks are created.
break;
}
}
if (rethrow != null) {
for (ResolverHook hook : hookMap.values()) {
try {
Felix.m_secureAction.invokeResolverHookEnd(hook);
} catch (Exception ex) {
rethrow = new BundleException("Resolver hook exception: " + ex.getMessage(), BundleException.REJECTED_BY_HOOK, ex);
}
}
throw rethrow;
}
// Ask hooks to indicate which revisions should not be resolved.
whitelist = new ShrinkableCollection<BundleRevision>(getUnresolvedRevisions());
int originalSize = whitelist.size();
for (ResolverHook hook : hookMap.values()) {
try {
Felix.m_secureAction.invokeResolverHookResolvable(hook, whitelist);
} catch (Throwable ex) {
rethrow = new BundleException("Resolver hook exception: " + ex.getMessage(), BundleException.REJECTED_BY_HOOK, ex);
// So we break here to make sure that no further resolver operations are executed.
break;
}
}
if (rethrow != null) {
for (ResolverHook hook : hookMap.values()) {
try {
Felix.m_secureAction.invokeResolverHookEnd(hook);
} catch (Exception ex) {
rethrow = new BundleException("Resolver hook exception: " + ex.getMessage(), BundleException.REJECTED_BY_HOOK, ex);
}
}
throw rethrow;
}
// as an optimization.
if (whitelist.size() == originalSize) {
whitelist = null;
}
// Check to make sure the target revisions are allowed to resolve.
if (whitelist != null) {
// trigger revision in the mandatory set, so ignore that case.
if (mandatory.isEmpty() || !optional.isEmpty() || (mandatory.iterator().next().getWiring() == null)) {
mandatory.retainAll(whitelist);
optional.retainAll(whitelist);
if (mandatory.isEmpty() && optional.isEmpty()) {
throw new ResolveException("Resolver hook prevented resolution.", null, null);
}
}
}
} else {
whitelist = null;
}
return new ResolverHookRecord(hookMap, whitelist);
}
use of org.osgi.framework.hooks.resolver.ResolverHookFactory 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()));
}
Aggregations