use of org.osgi.framework.hooks.resolver.ResolverHook 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.ResolverHook in project karaf by apache.
the class BundleInstallSupportImpl method resolveBundles.
/* (non-Javadoc)
* @see org.apache.karaf.features.internal.service.Regions#resolveBundles(java.util.Set, java.util.Map, java.util.Map)
*/
@Override
public void resolveBundles(Set<Bundle> bundles, final Map<Resource, List<Wire>> wiring, Map<Resource, Bundle> resToBnd) {
// Make sure it's only used for us
final Thread thread = Thread.currentThread();
// Translate wiring
final Map<Bundle, Resource> bndToRes = new HashMap<>();
for (Resource res : resToBnd.keySet()) {
bndToRes.put(resToBnd.get(res), res);
}
// Hook
final ResolverHook hook = new ResolverHook() {
@Override
public void filterResolvable(Collection<BundleRevision> candidates) {
}
@Override
public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
}
@Override
public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
if (Thread.currentThread() == thread) {
// osgi.ee capabilities are provided by the system bundle, so just ignore those
if (ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE.equals(requirement.getNamespace())) {
return;
}
Bundle sourceBundle = requirement.getRevision().getBundle();
Resource sourceResource = bndToRes.get(sourceBundle);
Set<Resource> wired = new HashSet<>();
// Get a list of allowed wired resources
wired.add(sourceResource);
for (Wire wire : wiring.get(sourceResource)) {
wired.add(wire.getProvider());
if (HostNamespace.HOST_NAMESPACE.equals(wire.getRequirement().getNamespace())) {
for (Wire hostWire : wiring.get(wire.getProvider())) {
wired.add(hostWire.getProvider());
}
}
}
// Remove candidates that are not allowed
for (Iterator<BundleCapability> candIter = candidates.iterator(); candIter.hasNext(); ) {
BundleCapability cand = candIter.next();
BundleRevision br = cand.getRevision();
if ((br.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
br = br.getWiring().getRequiredWires(null).get(0).getProvider();
}
Resource res = bndToRes.get(br.getBundle());
if (!wired.contains(br) && !wired.contains(res)) {
candIter.remove();
}
}
}
}
@Override
public void end() {
}
};
ResolverHookFactory factory = triggers -> hook;
ServiceRegistration<ResolverHookFactory> registration = systemBundleContext.registerService(ResolverHookFactory.class, factory, null);
try {
FrameworkWiring frameworkWiring = systemBundleContext.getBundle().adapt(FrameworkWiring.class);
frameworkWiring.resolveBundles(bundles);
} finally {
registration.unregister();
}
}
Aggregations