Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 146 additions & 134 deletions framework/src/main/java/org/apache/felix/framework/StatefulResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Resource, List<Wire>> 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<Resource, List<Wire>> 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<BundleRequirement> 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<String, Object> attrs = Collections.singletonMap(
BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
BundleRequirementImpl req = new BundleRequirementImpl(
revision,
BundleRevision.PACKAGE_NAMESPACE,
Collections.emptyMap(),
attrs);
final List<BundleCapability> 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<BundleCapability> itCand = candidates.iterator();
itCand.hasNext(); )
List<BundleRequirement> 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<String, Object> attrs = Collections.singletonMap(
BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
BundleRequirementImpl req = new BundleRequirementImpl(
revision,
BundleRevision.PACKAGE_NAMESPACE,
Collections.emptyMap(),
attrs);
final List<BundleCapability> 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<BundleCapability> 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<Resource, Wiring> wirings = getWirings();
Map<Resource, Wiring> wirings = getWirings();

wireMap = dynReq != null && wirings.containsKey(revision) ? m_resolver.resolveDynamic(
new ResolveContextImpl(
this,
wirings,
record,
Collections.<BundleRevision>emptyList(),
Collections.<BundleRevision>emptyList(),
getFragments())
{
@Override
public List<Capability> findProviders(Requirement br)
wireMap = dynReq != null && wirings.containsKey(revision) ? m_resolver.resolveDynamic(
new ResolveContextImpl(
this,
wirings,
record,
Collections.<BundleRevision>emptyList(),
Collections.<BundleRevision>emptyList(),
getFragments())
{
return (List) (br == dynReq ? candidates : super.findProviders(br));
}
},
revision.getWiring(), dynReq) : Collections.<Resource, List<Wire>>emptyMap();
}
catch (ResolutionException ex)
{
rethrow = ex;
}
@Override
public List<Capability> findProviders(Requirement br)
{
return (List) (br == dynReq ? candidates : super.findProviders(br));
}
},
revision.getWiring(), dynReq) : Collections.<Resource, List<Wire>>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<Wire> dynamicWires = wireMap.remove(revision);
Wire dynamicWire = dynamicWires.get(0);
if ((wireMap != null) && wireMap.containsKey(revision))
{
List<Wire> 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;
Expand Down