Search in sources :

Example 11 with ResolverHookFactory

use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.

the class StateImpl method resolve.

private StateDelta resolve(boolean incremental, BundleDescription[] reResolve, BundleDescription[] triggers) {
    fullyLoad();
    synchronized (this.monitor) {
        if (resolver == null)
            // $NON-NLS-1$
            throw new IllegalStateException("no resolver set");
        if (resolving == true)
            // $NON-NLS-1$
            throw new IllegalStateException("An attempt to start a nested resolve process has been detected.");
        try {
            resolving = true;
            if (!incremental) {
                resolved = false;
                reResolve = getBundles();
                // need to get any removal pendings before flushing
                if (removalPendings.size() > 0) {
                    BundleDescription[] removed = internalGetRemovalPending();
                    reResolve = mergeBundles(reResolve, removed);
                }
                flush(reResolve);
            } else {
                if (resolved && reResolve == null)
                    return new StateDeltaImpl(this);
                if (developmentMode) {
                    // in dev mode we need to aggressively flush removal pendings
                    if (removalPendings.size() > 0) {
                        BundleDescription[] removed = internalGetRemovalPending();
                        reResolve = mergeBundles(reResolve, removed);
                    }
                }
                if (reResolve == null)
                    reResolve = internalGetRemovalPending();
                if (triggers == null) {
                    Set<BundleDescription> triggerSet = new HashSet<>();
                    Collection<BundleDescription> closure = getDependencyClosure(Arrays.asList(reResolve));
                    for (BundleDescription toRefresh : closure) {
                        Bundle bundle = toRefresh.getBundle();
                        if (bundle != null && (bundle.getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED | Bundle.RESOLVED)) == 0)
                            triggerSet.add(toRefresh);
                    }
                    triggers = triggerSet.toArray(new BundleDescription[triggerSet.size()]);
                }
            }
            // use the Headers class to handle ignoring case while matching keys (bug 180817)
            @SuppressWarnings("unchecked") CaseInsensitiveDictionaryMap<Object, Object>[] tmpPlatformProperties = new CaseInsensitiveDictionaryMap[platformProperties.length];
            for (int i = 0; i < platformProperties.length; i++) {
                tmpPlatformProperties[i] = new CaseInsensitiveDictionaryMap<>(platformProperties[i].size());
                for (Enumeration<Object> keys = platformProperties[i].keys(); keys.hasMoreElements(); ) {
                    Object key = keys.nextElement();
                    tmpPlatformProperties[i].put(key, platformProperties[i].get(key));
                }
            }
            ResolverHookFactory currentFactory = hookFactory;
            if (currentFactory != null) {
                @SuppressWarnings("unchecked") Collection<BundleRevision> triggerRevisions = Collections.unmodifiableCollection(triggers == null ? Collections.EMPTY_LIST : Arrays.asList((BundleRevision[]) triggers));
                begin(triggerRevisions);
            }
            ResolverHookException error = null;
            try {
                resolver.resolve(reResolve, tmpPlatformProperties);
            } catch (ResolverHookException e) {
                error = e;
                resolverErrors.clear();
            }
            resolved = removalPendings.size() == 0;
            StateDeltaImpl savedChanges = changes == null ? new StateDeltaImpl(this) : changes;
            savedChanges.setResolverHookException(error);
            changes = new StateDeltaImpl(this);
            if (savedChanges.getChanges().length > 0)
                updateTimeStamp();
            return savedChanges;
        } finally {
            resolving = false;
        }
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 12 with ResolverHookFactory

use of org.osgi.framework.hooks.resolver.ResolverHookFactory 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());
}
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) Bundle(org.osgi.framework.Bundle) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Equinox(org.eclipse.osgi.launch.Equinox) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 13 with ResolverHookFactory

use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.

the class SecurityManagerTests method testDynamicImportWithSecurity.

public void testDynamicImportWithSecurity() throws BundleException {
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    configuration.put(Constants.FRAMEWORK_SECURITY, Constants.FRAMEWORK_SECURITY_OSGI);
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    BundleContext systemContext = equinox.getBundleContext();
    // register a no-op resolver hook to test security
    ResolverHookFactory dummyHook = new ResolverHookFactory() {

        @Override
        public ResolverHook begin(Collection<BundleRevision> triggers) {
            return new ResolverHook() {

                @Override
                public void filterResolvable(Collection<BundleRevision> candidates) {
                // nothing
                }

                @Override
                public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
                // nothing
                }

                @Override
                public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
                    // always remove candidates for dynamic import
                    if (PackageNamespace.RESOLUTION_DYNAMIC.equals(requirement.getDirectives().get(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE))) {
                        candidates.clear();
                    }
                }

                @Override
                public void end() {
                // nothing
                }
            };
        }
    };
    systemContext.registerService(ResolverHookFactory.class, dummyHook, null);
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext);
    // try installing host and fragment to test bug 245678
    // $NON-NLS-1$
    String testDynamicImportLocation = installer.getBundleLocation("test.dynamicimport");
    // set the security for the bundle
    ConditionalPermissionAdmin ca = (ConditionalPermissionAdmin) systemContext.getService(systemContext.getServiceReference(ConditionalPermissionAdmin.class.getName()));
    ConditionalPermissionUpdate update = ca.newConditionalPermissionUpdate();
    List rows = update.getConditionalPermissionInfos();
    rows.add(ca.newConditionalPermissionInfo(null, null, new PermissionInfo[] { allPackagePermission }, ConditionalPermissionInfo.ALLOW));
    // $NON-NLS-1$
    assertTrue("Cannot commit rows", update.commit());
    Bundle testDynamicImport = systemContext.installBundle(testDynamicImportLocation);
    try {
        testDynamicImport.start();
    } catch (BundleException e) {
        fail("Did not start test bundle successfully.", e);
    }
    // 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);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
    // $NON-NLS-1$
    assertNull("SecurityManager is not null", System.getSecurityManager());
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Equinox(org.eclipse.osgi.launch.Equinox) PermissionInfo(org.osgi.service.permissionadmin.PermissionInfo) File(java.io.File)

Example 14 with ResolverHookFactory

use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testBug370258_endException.

public void testBug370258_endException() {
    final boolean[] endCalled = { false };
    ResolverHookFactory endHook = 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;
                }
            };
        }
    };
    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) {
                // Nothing
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // Nothing
                }

                public void end() {
                    throw new RuntimeException("Error");
                }
            };
        }
    };
    ServiceRegistration errorReg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, error, null);
    ServiceRegistration endReg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, endHook, 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 {
        errorReg.unregister();
        endReg.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 15 with ResolverHookFactory

use of org.osgi.framework.hooks.resolver.ResolverHookFactory in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testBug348806.

public void testBug348806() {
    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) {
                // Nothing
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // Nothing
                }

                public void end() {
                    throw new RuntimeException("Error");
                }
            };
        }
    };
    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();
    }
}
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)

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