Search in sources :

Example 6 with ResolverHookFactory

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()));
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) DummyResolverHookFactory(org.eclipse.osgi.tests.container.dummys.DummyResolverHookFactory) DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) DummyResolverHook(org.eclipse.osgi.tests.container.dummys.DummyResolverHook) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) DummyCollisionHook(org.eclipse.osgi.tests.container.dummys.DummyCollisionHook) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) BundleRevision(org.osgi.framework.wiring.BundleRevision) ArrayList(java.util.ArrayList) Collection(java.util.Collection) BundleCapability(org.osgi.framework.wiring.BundleCapability) Module(org.eclipse.osgi.container.Module) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Test(org.junit.Test)

Example 7 with ResolverHookFactory

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());
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) BundleRevision(org.osgi.framework.wiring.BundleRevision) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceRegistration(org.osgi.framework.ServiceRegistration) Bundle(org.osgi.framework.Bundle) Equinox(org.eclipse.osgi.launch.Equinox) File(java.io.File) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) BundleContext(org.osgi.framework.BundleContext)

Example 8 with ResolverHookFactory

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);
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) AriesSubsystem(org.apache.aries.subsystem.AriesSubsystem) Subsystem(org.osgi.service.subsystem.Subsystem) SubsystemException(org.osgi.service.subsystem.SubsystemException) BundleRevision(org.osgi.framework.wiring.BundleRevision) Iterator(java.util.Iterator) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) SubsystemTest(org.apache.aries.subsystem.itests.SubsystemTest) Test(org.junit.Test)

Example 9 with ResolverHookFactory

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);
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) BundleException(org.osgi.framework.BundleException) ResolveException(org.apache.felix.framework.resolver.ResolveException) ResolutionException(org.osgi.service.resolver.ResolutionException) NoSuchElementException(java.util.NoSuchElementException) ServiceReference(org.osgi.framework.ServiceReference) LinkedHashMap(java.util.LinkedHashMap) ResolveException(org.apache.felix.framework.resolver.ResolveException) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleException(org.osgi.framework.BundleException)

Example 10 with ResolverHookFactory

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()));
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) DummyResolverHookFactory(org.eclipse.osgi.tests.container.dummys.DummyResolverHookFactory) DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) DummyResolverHook(org.eclipse.osgi.tests.container.dummys.DummyResolverHook) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) DummyCollisionHook(org.eclipse.osgi.tests.container.dummys.DummyCollisionHook) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) Collection(java.util.Collection) BundleCapability(org.osgi.framework.wiring.BundleCapability) Module(org.eclipse.osgi.container.Module) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Test(org.junit.Test)

Aggregations

ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)17 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)15 Bundle (org.osgi.framework.Bundle)8 BundleCapability (org.osgi.framework.wiring.BundleCapability)8 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)8 Collection (java.util.Collection)7 BundleException (org.osgi.framework.BundleException)7 ServiceRegistration (org.osgi.framework.ServiceRegistration)7 BundleRevision (org.osgi.framework.wiring.BundleRevision)6 Test (org.junit.Test)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Equinox (org.eclipse.osgi.launch.Equinox)3 BundleContext (org.osgi.framework.BundleContext)3 HashMap (java.util.HashMap)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Module (org.eclipse.osgi.container.Module)2