Search in sources :

Example 1 with ResolverHookFactory

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();
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with ResolverHookFactory

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;
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook)

Example 3 with ResolverHookFactory

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();
        }
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 4 with ResolverHookFactory

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]);
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 5 with ResolverHookFactory

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();
        }
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook)

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