diff --git a/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java b/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java index ccec0da82c..244ac5a3d2 100644 --- a/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java +++ b/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java @@ -468,173 +468,185 @@ BundleRevision resolve(BundleRevision revision, String pkgName) // dynamic import is allowed without holding any locks, but this is // okay since the resolver will double check later after we have // acquired the global lock below. - if ((revision.getWiring() != null) && isAllowedDynamicImport(revision, pkgName)) + if (revision.getWiring() != null) { - // Acquire global lock. - boolean locked = m_felix.acquireGlobalLock(); - if (!locked) + if (isAllowedDynamicImport(revision, pkgName)) { - throw new ResolveException( - "Unable to acquire global lock for resolve.", revision, null); - } + // Acquire global lock. + boolean locked = m_felix.acquireGlobalLock(); + if (!locked) + { + throw new ResolveException( + "Unable to acquire global lock for resolve.", revision, null); + } - // Make sure we are not already resolving, which can be - // the case if a resolver hook does something bad. - if (m_isResolving) - { - m_felix.releaseGlobalLock(); - throw new IllegalStateException("Nested resolve operations not allowed."); - } - m_isResolving = true; + // Make sure we are not already resolving, which can be + // the case if a resolver hook does something bad. + if (m_isResolving) + { + m_felix.releaseGlobalLock(); + throw new IllegalStateException("Nested resolve operations not allowed."); + } + m_isResolving = true; - Map> wireMap = null; - try - { - // Double check to make sure that someone hasn't beaten us to - // dynamically importing the package, which can happen if two - // threads are racing to do so. If we have an existing wire, - // then just return it instead. - provider = ((BundleWiringImpl) revision.getWiring()) - .getImportedPackageSource(pkgName); - if (provider == null) + Map> wireMap = null; + try { - // Prepare resolver hooks, if any. - ResolverHookRecord record = - prepareResolverHooks( - Collections.singleton(revision), Collections.emptySet()); + // Double check to make sure that someone hasn't beaten us to + // dynamically importing the package, which can happen if two + // threads are racing to do so. If we have an existing wire, + // then just return it instead. + provider = ((BundleWiringImpl) revision.getWiring()) + .getImportedPackageSource(pkgName); + if (provider == null) + { + // Prepare resolver hooks, if any. + ResolverHookRecord record = + prepareResolverHooks( + Collections.singleton(revision), Collections.emptySet()); - // Select any singletons in the resolver state. - selectSingletons(record); + // Select any singletons in the resolver state. + selectSingletons(record); - // Catch any resolve exception to rethrow later because - // we may need to call end() on resolver hooks. - ResolutionException rethrow = null; - try - { - List dynamics = - Util.getDynamicRequirements(revision.getWiring().getRequirements(null)); - - // Loop through the importer's dynamic requirements to determine if - // there is a matching one for the package from which we want to - // load a class. - Map attrs = Collections.singletonMap( - BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName); - BundleRequirementImpl req = new BundleRequirementImpl( - revision, - BundleRevision.PACKAGE_NAMESPACE, - Collections.emptyMap(), - attrs); - final List candidates = findProvidersInternal(record, req, false, true); - - // Try to find a dynamic requirement that matches the capabilities. - final BundleRequirementImpl dynReq = findDynamicRequirement(dynamics, candidates); - - // If we found a matching dynamic requirement, then filter out - // any candidates that do not match it. - if (dynReq != null) + // Catch any resolve exception to rethrow later because + // we may need to call end() on resolver hooks. + ResolutionException rethrow = null; + try { - for (Iterator itCand = candidates.iterator(); - itCand.hasNext(); ) + List dynamics = + Util.getDynamicRequirements(revision.getWiring().getRequirements(null)); + + // Loop through the importer's dynamic requirements to determine if + // there is a matching one for the package from which we want to + // load a class. + Map attrs = Collections.singletonMap( + BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName); + BundleRequirementImpl req = new BundleRequirementImpl( + revision, + BundleRevision.PACKAGE_NAMESPACE, + Collections.emptyMap(), + attrs); + final List candidates = findProvidersInternal(record, req, false, true); + + // Try to find a dynamic requirement that matches the capabilities. + final BundleRequirementImpl dynReq = findDynamicRequirement(dynamics, candidates); + + // If we found a matching dynamic requirement, then filter out + // any candidates that do not match it. + if (dynReq != null) { - Capability cap = itCand.next(); - if (!CapabilitySet.matches( - cap, dynReq.getFilter())) + for (Iterator itCand = candidates.iterator(); + itCand.hasNext(); ) { - itCand.remove(); + Capability cap = itCand.next(); + if (!CapabilitySet.matches( + cap, dynReq.getFilter())) + { + itCand.remove(); + } } } - } - else - { - candidates.clear(); - } + else + { + candidates.clear(); + } - Map wirings = getWirings(); + Map wirings = getWirings(); - wireMap = dynReq != null && wirings.containsKey(revision) ? m_resolver.resolveDynamic( - new ResolveContextImpl( - this, - wirings, - record, - Collections.emptyList(), - Collections.emptyList(), - getFragments()) - { - @Override - public List findProviders(Requirement br) + wireMap = dynReq != null && wirings.containsKey(revision) ? m_resolver.resolveDynamic( + new ResolveContextImpl( + this, + wirings, + record, + Collections.emptyList(), + Collections.emptyList(), + getFragments()) { - return (List) (br == dynReq ? candidates : super.findProviders(br)); - } - }, - revision.getWiring(), dynReq) : Collections.>emptyMap(); - } - catch (ResolutionException ex) - { - rethrow = ex; - } + @Override + public List findProviders(Requirement br) + { + return (List) (br == dynReq ? candidates : super.findProviders(br)); + } + }, + revision.getWiring(), dynReq) : Collections.>emptyMap(); + } + catch (ResolutionException ex) + { + rethrow = ex; + } - // Release resolver hooks, if any. - releaseResolverHooks(record); + // Release resolver hooks, if any. + releaseResolverHooks(record); - // If the resolve failed, rethrow the exception. - if (rethrow != null) - { - throw rethrow; - } + // If the resolve failed, rethrow the exception. + if (rethrow != null) + { + throw rethrow; + } - if ((wireMap != null) && wireMap.containsKey(revision)) - { - List dynamicWires = wireMap.remove(revision); - Wire dynamicWire = dynamicWires.get(0); + if ((wireMap != null) && wireMap.containsKey(revision)) + { + List dynamicWires = wireMap.remove(revision); + Wire dynamicWire = dynamicWires.get(0); - // Mark all revisions as resolved. - markResolvedRevisions(wireMap); + // Mark all revisions as resolved. + markResolvedRevisions(wireMap); - // Dynamically add new wire to importing revision. - if (dynamicWire != null) - { - // TODO is a rw already a BundleWire? - // TODO can we optimize this? - if (dynamicWire.getRequirer() instanceof BundleRevision && - dynamicWire.getRequirement() instanceof BundleRequirement && - dynamicWire.getProvider() instanceof BundleRevision && - dynamicWire.getCapability() instanceof BundleCapability) + // Dynamically add new wire to importing revision. + if (dynamicWire != null) { - BundleRevision dwRequirer = (BundleRevision) dynamicWire.getRequirer(); - BundleRequirement dwRequirement = (BundleRequirement) dynamicWire.getRequirement(); - BundleRevision dwProvider = (BundleRevision) dynamicWire.getProvider(); - BundleCapability dwCapability = (BundleCapability) dynamicWire.getCapability(); + // TODO is a rw already a BundleWire? + // TODO can we optimize this? + if (dynamicWire.getRequirer() instanceof BundleRevision && + dynamicWire.getRequirement() instanceof BundleRequirement && + dynamicWire.getProvider() instanceof BundleRevision && + dynamicWire.getCapability() instanceof BundleCapability) + { + BundleRevision dwRequirer = (BundleRevision) dynamicWire.getRequirer(); + BundleRequirement dwRequirement = (BundleRequirement) dynamicWire.getRequirement(); + BundleRevision dwProvider = (BundleRevision) dynamicWire.getProvider(); + BundleCapability dwCapability = (BundleCapability) dynamicWire.getCapability(); - BundleWire bw = new BundleWireImpl( - dwRequirer, - dwRequirement, - dwProvider, - dwCapability); + BundleWire bw = new BundleWireImpl( + dwRequirer, + dwRequirement, + dwProvider, + dwCapability); - m_felix.getDependencies().addDependent(bw); + m_felix.getDependencies().addDependent(bw); - ((BundleWiringImpl) revision.getWiring()).addDynamicWire(bw); + ((BundleWiringImpl) revision.getWiring()).addDynamicWire(bw); - m_felix.getLogger().log( - Logger.LOG_DEBUG, - "DYNAMIC WIRE: " + dynamicWire); + m_felix.getLogger().log( + Logger.LOG_DEBUG, + "DYNAMIC WIRE: " + dynamicWire); - provider = ((BundleWiringImpl) revision.getWiring()) - .getImportedPackageSource(pkgName); + provider = ((BundleWiringImpl) revision.getWiring()) + .getImportedPackageSource(pkgName); + } } } } } + finally + { + // Clear resolving flag. + m_isResolving = false; + // Always release the global lock. + m_felix.releaseGlobalLock(); + } + + fireResolvedEvents(wireMap); } - finally + else { - // Clear resolving flag. - m_isResolving = false; - // Always release the global lock. - m_felix.releaseGlobalLock(); + // It is possible that isAllowedDynamicImport returns false + // because another thread has just successfully completed the + // dynamic import. In which case we should check our imported + // packages for that package. + provider = ((BundleWiringImpl) revision.getWiring()) + .getImportedPackageSource(pkgName); } - - fireResolvedEvents(wireMap); } return provider;