use of org.osgi.service.resolver.ResolutionException in project felix by apache.
the class ResolverTest method testScenario11.
/**
* Test dynamic resolution with an unresolved fragment
*/
@Test
public void testScenario11() throws Exception {
ResolverImpl resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
ResourceImpl a1 = new ResourceImpl("A");
Capability a1_hostCap = addCap(a1, HostNamespace.HOST_NAMESPACE, "A");
ResourceImpl f1 = new ResourceImpl("F1", IdentityNamespace.TYPE_FRAGMENT, Version.emptyVersion);
Requirement f1_hostReq = addReq(f1, HostNamespace.HOST_NAMESPACE, "A");
Capability f1_pkgCap = addCap(f1, PackageNamespace.PACKAGE_NAMESPACE, "org.foo.a");
ResourceImpl b1 = new ResourceImpl("B");
Requirement b_pkgReq1 = addReq(b1, PackageNamespace.PACKAGE_NAMESPACE, "org.foo.a");
candMap.put(b_pkgReq1, Collections.singletonList(f1_pkgCap));
candMap.put(f1_hostReq, Collections.singletonList(a1_hostCap));
Map<Resource, List<Wire>> wires = new HashMap<Resource, List<Wire>>();
wires.put(a1, new ArrayList<Wire>());
wires.put(b1, new ArrayList<Wire>());
Map<Resource, List<Wire>> invertedWires = new HashMap<Resource, List<Wire>>();
invertedWires.put(a1, new ArrayList<Wire>());
invertedWires.put(b1, new ArrayList<Wire>());
wirings.put(a1, new SimpleWiring(a1, Collections.<Capability>emptyList(), wires, invertedWires));
wirings.put(b1, new SimpleWiring(b1, Collections.<Capability>emptyList(), wires, invertedWires));
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, Collections.<Resource>emptyList(), Collections.<Resource>emptyList());
List<Capability> caps = new ArrayList<Capability>();
caps.add(f1_pkgCap);
try {
resolver.resolve(rci, b1, b_pkgReq1, caps);
fail("Should fail to dynamic requirement to fragment when host is resolved already.");
} catch (ResolutionException e) {
// expected
assertTrue(e.getUnresolvedRequirements().contains(b_pkgReq1));
}
// now remove host wiring
wirings.remove(a1);
caps.clear();
caps.add(f1_pkgCap);
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci, b1, b_pkgReq1, caps);
assertEquals(3, wireMap.size());
List<Wire> wiresB = wireMap.get(b1);
assertNotNull(wiresB);
assertEquals(1, wiresB.size());
// should be wired to A through the fragment capability
assertEquals(a1, wiresB.get(0).getProvider());
assertEquals(f1_pkgCap, wiresB.get(0).getCapability());
}
use of org.osgi.service.resolver.ResolutionException in project felix by apache.
the class ResolverTest method testScenario4.
@Test
public void testScenario4() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario4(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
try {
resolver.resolve(rci);
fail("Should have thrown a resolution exception as bundle A in scenario 4 cannot be resolved due to constraint violations.");
} catch (ResolutionException re) {
// good
}
}
use of org.osgi.service.resolver.ResolutionException in project felix by apache.
the class ResolverTest method testScenario5.
@Test
public void testScenario5() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario5(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
try {
resolver.resolve(rci);
fail("Should have thrown a resolution exception as bundle A in scenario 5 cannot be resolved due to constraint violations.");
} catch (ResolutionException re) {
// good
}
}
use of org.osgi.service.resolver.ResolutionException in project felix by apache.
the class BundleWiringImpl method findResourcesByDelegation.
private Enumeration findResourcesByDelegation(String name) {
Enumeration urls = null;
List completeUrlList = new ArrayList();
// Get the package of the target class/resource.
String pkgName = Util.getResourcePackage(name);
// property to the parent class loader.
if (shouldBootDelegate(pkgName)) {
try {
// Get the appropriate class loader for delegation.
ClassLoader bdcl = getBootDelegationClassLoader();
urls = bdcl.getResources(name);
} catch (IOException ex) {
// This shouldn't happen and even if it does, there
// is nothing we can do, so just ignore it.
}
// search; otherwise, continue to look locally.
if (pkgName.startsWith("java.")) {
return urls;
}
completeUrlList.add(urls);
}
// Look in the revisions's imported packages. If the package is
// imported, then we stop searching no matter the result since
// imported packages cannot be split.
BundleRevision provider = m_importedPkgs.get(pkgName);
if (provider != null) {
// Delegate to the provider revision.
urls = ((BundleWiringImpl) provider.getWiring()).getResourcesByDelegation(name);
// If we find any resources, then add them.
if ((urls != null) && (urls.hasMoreElements())) {
completeUrlList.add(urls);
}
// across required bundles or the revision's content.
return new CompoundEnumeration((Enumeration[]) completeUrlList.toArray(new Enumeration[completeUrlList.size()]));
}
// See whether we can get the resource from the required bundles and
// regardless of whether or not this is the case continue to the next
// step potentially passing on the result of this search (if any).
List<BundleRevision> providers = m_requiredPkgs.get(pkgName);
if (providers != null) {
for (BundleRevision p : providers) {
// Delegate to the provider revision.
urls = ((BundleWiringImpl) p.getWiring()).getResourcesByDelegation(name);
// If we find any resources, then add them.
if ((urls != null) && (urls.hasMoreElements())) {
completeUrlList.add(urls);
}
// Do not return here, since required packages can be split
// across the revision's content.
}
}
// Try the module's own class path. If we can find the resource then
// return it together with the results from the other searches else
// try to look into the dynamic imports.
urls = m_revision.getResourcesLocal(name);
if ((urls != null) && (urls.hasMoreElements())) {
completeUrlList.add(urls);
} else {
// class/resource via a dynamic import, if possible.
try {
provider = m_resolver.resolve(m_revision, pkgName);
} catch (ResolutionException ex) {
// Ignore this since it is likely normal.
} catch (BundleException ex) {
// Ignore this since it is likely the result of a resolver hook.
}
if (provider != null) {
// Delegate to the provider revision.
urls = ((BundleWiringImpl) provider.getWiring()).getResourcesByDelegation(name);
// If we find any resources, then add them.
if ((urls != null) && (urls.hasMoreElements())) {
completeUrlList.add(urls);
}
}
}
return new CompoundEnumeration((Enumeration[]) completeUrlList.toArray(new Enumeration[completeUrlList.size()]));
}
use of org.osgi.service.resolver.ResolutionException in project rt.equinox.framework by eclipse.
the class SystemModule method init.
/**
* Initializes the module container
* @throws BundleException if an exeption occurred while initializing
*/
public final void init() throws BundleException {
getRevisions().getContainer().checkAdminPermission(getBundle(), AdminPermission.EXECUTE);
boolean lockedStarted = false;
// Indicate we are in the middle of a start.
// This must be incremented before we acquire the STARTED lock the first time.
inStart.incrementAndGet();
try {
lockStateChange(ModuleEvent.STARTED);
lockedStarted = true;
getContainer().getAdaptor().initBegin();
checkValid();
if (ACTIVE_SET.contains(getState()))
return;
getRevisions().getContainer().open();
if (getState().equals(State.INSTALLED)) {
// must unlock to avoid out of order locks when multiple unresolved
// bundles are started at the same time from different threads
unlockStateChange(ModuleEvent.STARTED);
lockedStarted = false;
ResolutionReport report;
try {
report = getRevisions().getContainer().resolve(Arrays.asList((Module) this), true);
} finally {
lockStateChange(ModuleEvent.STARTED);
lockedStarted = true;
}
// need to check valid again in case someone uninstalled the bundle
checkValid();
ResolutionException e = report.getResolutionException();
if (e != null) {
if (e.getCause() instanceof BundleException) {
throw (BundleException) e.getCause();
}
}
if (ACTIVE_SET.contains(getState()))
return;
if (getState().equals(State.INSTALLED)) {
String reportMessage = report.getResolutionReportMessage(getCurrentRevision());
throw new BundleException(Msg.Module_ResolveError + reportMessage, BundleException.RESOLVE_ERROR);
}
}
setState(State.STARTING);
AtomicReference<ContainerEvent> existingForStop = forStop;
if (existingForStop.get() != null) {
// There was a previous launch, reset the reference forStop
forStop = new AtomicReference<>();
}
publishEvent(ModuleEvent.STARTING);
try {
initWorker();
} catch (Throwable t) {
setState(State.STOPPING);
publishEvent(ModuleEvent.STOPPING);
setState(State.RESOLVED);
publishEvent(ModuleEvent.STOPPED);
getRevisions().getContainer().close();
if (t instanceof BundleException) {
throw (BundleException) t;
}
// $NON-NLS-1$
throw new BundleException("Error initializing container.", BundleException.ACTIVATOR_ERROR, t);
}
} finally {
getContainer().getAdaptor().initEnd();
if (lockedStarted) {
unlockStateChange(ModuleEvent.STARTED);
}
inStart.decrementAndGet();
}
}
Aggregations