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;
}
}
}
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());
}
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());
}
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]);
}
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();
}
}
Aggregations