More code cleanup. (FELIX-2035)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@944402 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index b37e74b..a0327ba 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -4068,10 +4068,63 @@
return m_resolverState.getCandidates(reqModule, req, obeyMandatory);
}
+ // This method duplicates a lot of logic from:
+ // ResolverImpl.getDynamicImportCandidates()
public boolean isAllowedDynamicImport(Module module, String pkgName)
{
- return ResolverImpl.getDynamicImportCandidates(
- m_resolverState, module, pkgName) != null;
+ // Unresolved modules cannot dynamically import, nor can the default
+ // package be dynamically imported.
+ if (!module.isResolved() || pkgName.length() == 0)
+ {
+ return false;
+ }
+
+ // If the module doesn't have dynamic imports, then just return
+ // immediately.
+ List<Requirement> dynamics = module.getDynamicRequirements();
+ if ((dynamics == null) || (dynamics.size() == 0))
+ {
+ return false;
+ }
+
+ // If any of the module exports this package, then we cannot
+ // attempt to dynamically import it.
+ List<Capability> caps = module.getCapabilities();
+ for (int i = 0; (caps != null) && (i < caps.size()); i++)
+ {
+ if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE)
+ && caps.get(i).getAttribute(Capability.PACKAGE_ATTR).getValue().equals(pkgName))
+ {
+ return false;
+ }
+ }
+ // If any of our wires have this package, then we cannot
+ // attempt to dynamically import it.
+ List<Wire> wires = module.getWires();
+ for (int i = 0; (wires != null) && (i < wires.size()); i++)
+ {
+ if (wires.get(i).hasPackage(pkgName))
+ {
+ return false;
+ }
+ }
+
+ // 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.
+ List<Directive> dirs = Collections.EMPTY_LIST;
+ List<Attribute> attrs = new ArrayList(1);
+ attrs.add(new Attribute(Capability.PACKAGE_ATTR, pkgName, false));
+ Requirement req = new RequirementImpl(
+ module, Capability.PACKAGE_NAMESPACE, dirs, attrs);
+ Set<Capability> candidates = m_resolverState.getCandidates(module, req, false);
+
+ if (candidates.size() == 0)
+ {
+ return false;
+ }
+
+ return true;
}
private void markResolvedModules(Map<Module, List<Wire>> wireMap)
diff --git a/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java b/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java
index b167c67..bdeea79 100644
--- a/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java
@@ -85,62 +85,68 @@
if (!module.isResolved())
{
-// TODO: FELIX3 - Should we clear these in a finally block?
- m_usesPermutations.clear();
- m_importPermutations.clear();
-
- Map<Requirement, Set<Capability>> candidateMap =
- new HashMap<Requirement, Set<Capability>>();
-
- populateCandidates(state, module, candidateMap, new HashMap<Module, Object>());
- m_usesPermutations.add(candidateMap);
-
- ResolveException rethrow = null;
-
- Map<Capability, Set<Requirement>> capDepSet = new HashMap();
-
- do
+ try
{
- rethrow = null;
+ Map<Requirement, Set<Capability>> candidateMap =
+ new HashMap<Requirement, Set<Capability>>();
- modulePkgMap.clear();
- capDepSet.clear();
- m_packageSourcesCache.clear();
+ populateCandidates(
+ state, module, candidateMap, new HashMap<Module, Object>());
+ m_usesPermutations.add(candidateMap);
- candidateMap = (m_usesPermutations.size() > 0)
- ? m_usesPermutations.remove(0)
- : m_importPermutations.remove(0);
+ ResolveException rethrow = null;
+
+ Map<Capability, Set<Requirement>> capDepSet = new HashMap();
+
+ do
+ {
+ rethrow = null;
+
+ modulePkgMap.clear();
+ capDepSet.clear();
+ m_packageSourcesCache.clear();
+
+ candidateMap = (m_usesPermutations.size() > 0)
+ ? m_usesPermutations.remove(0)
+ : m_importPermutations.remove(0);
//dumpCandidateMap(state, candidateMap);
- calculatePackageSpaces(
- module, candidateMap, modulePkgMap,
- capDepSet, new HashMap(), new HashSet());
+ calculatePackageSpaces(
+ module, candidateMap, modulePkgMap,
+ capDepSet, new HashMap(), new HashSet());
//System.out.println("+++ PACKAGE SPACES START +++");
//dumpModulePkgMap(modulePkgMap);
//System.out.println("+++ PACKAGE SPACES END +++");
- try
- {
- checkPackageSpaceConsistency(
- module, candidateMap, modulePkgMap, capDepSet, new HashMap());
+ try
+ {
+ checkPackageSpaceConsistency(
+ module, candidateMap, modulePkgMap, capDepSet, new HashMap());
+ }
+ catch (ResolveException ex)
+ {
+ rethrow = ex;
+ System.out.println("RE: " + ex);
+ }
}
- catch (ResolveException ex)
- {
- rethrow = ex;
- System.out.println("RE: " + ex);
- }
- }
- while ((rethrow != null)
- && ((m_usesPermutations.size() > 0) || (m_importPermutations.size() > 0)));
+ while ((rethrow != null)
+ && ((m_usesPermutations.size() > 0) || (m_importPermutations.size() > 0)));
- if (rethrow != null)
+ if (rethrow != null)
+ {
+ throw rethrow;
+ }
+
+ wireMap =
+ populateWireMap(module, modulePkgMap, wireMap,
+ candidateMap);
+ }
+ finally
{
- throw rethrow;
+ // Always clear the state.
+ m_usesPermutations.clear();
+ m_importPermutations.clear();
}
-
- wireMap =
- populateWireMap(module, modulePkgMap, wireMap,
- candidateMap);
}
if (m_isInvokeCount)
@@ -174,67 +180,72 @@
getDynamicImportCandidates(state, module, pkgName);
if (candidateMap != null)
{
-// TODO: FELIX3 - Should we clear these in a finally block?
- m_usesPermutations.clear();
- m_importPermutations.clear();
-
- Map<Module, List<Wire>> wireMap = new HashMap();
- Map<Module, Packages> modulePkgMap = new HashMap();
-
- populateDynamicCandidates(state, module, candidateMap);
- m_usesPermutations.add(candidateMap);
-
- ResolveException rethrow = null;
-
- Map<Capability, Set<Requirement>> capDepSet = new HashMap();
-
- do
+ try
{
- rethrow = null;
+ Map<Module, List<Wire>> wireMap = new HashMap();
+ Map<Module, Packages> modulePkgMap = new HashMap();
- modulePkgMap.clear();
- capDepSet.clear();
+ populateDynamicCandidates(state, module, candidateMap);
+ m_usesPermutations.add(candidateMap);
- candidateMap = (m_usesPermutations.size() > 0)
- ? m_usesPermutations.remove(0)
- : m_importPermutations.remove(0);
+ ResolveException rethrow = null;
- calculatePackageSpaces(
- module, candidateMap, modulePkgMap,
- capDepSet, new HashMap(), new HashSet());
+ Map<Capability, Set<Requirement>> capDepSet = new HashMap();
- try
+ do
{
- checkPackageSpaceConsistency(
- module, candidateMap, modulePkgMap, capDepSet, new HashMap());
- }
- catch (ResolveException ex)
- {
- rethrow = ex;
- System.out.println("RE: " + ex);
- }
- }
- while ((rethrow != null)
- && ((m_usesPermutations.size() > 0) || (m_importPermutations.size() > 0)));
+ rethrow = null;
- if (rethrow != null)
- {
- throw rethrow;
- }
+ modulePkgMap.clear();
+ capDepSet.clear();
+
+ candidateMap = (m_usesPermutations.size() > 0)
+ ? m_usesPermutations.remove(0)
+ : m_importPermutations.remove(0);
+
+ calculatePackageSpaces(
+ module, candidateMap, modulePkgMap,
+ capDepSet, new HashMap(), new HashSet());
+
+ try
+ {
+ checkPackageSpaceConsistency(
+ module, candidateMap, modulePkgMap,
+ capDepSet, new HashMap());
+ }
+ catch (ResolveException ex)
+ {
+ rethrow = ex;
+ System.out.println("RE: " + ex);
+ }
+ }
+ while ((rethrow != null)
+ && ((m_usesPermutations.size() > 0)
+ || (m_importPermutations.size() > 0)));
+
+ if (rethrow != null)
+ {
+ throw rethrow;
+ }
//dumpModulePkgMap(modulePkgMap);
- wireMap = populateDynamicWireMap(
- module, pkgName, modulePkgMap, wireMap, candidateMap);
+ wireMap = populateDynamicWireMap(
+ module, pkgName, modulePkgMap, wireMap, candidateMap);
- return wireMap;
+ return wireMap;
+ }
+ finally
+ {
+ // Always clear the state.
+ m_usesPermutations.clear();
+ m_importPermutations.clear();
+ }
}
return null;
}
-// TODO: FELIX3 - It would be nice to make this private. We will likely have
-// to duplicate some of its code if we put the resolve in a separate module.
- public static Map<Requirement, Set<Capability>> getDynamicImportCandidates(
+ private static Map<Requirement, Set<Capability>> getDynamicImportCandidates(
ResolverState state, Module module, String pkgName)
{
if (m_isInvokeCount)
@@ -466,7 +477,7 @@
new ResolveException("Unable to resolve " + module
+ ": missing requirement " + req, module, req);
resultCache.put(module, ex);
- m_logger.log(Logger.LOG_DEBUG, ex.getMessage(), ex);
+ m_logger.log(Logger.LOG_DEBUG, "No viable candidates", ex);
throw ex;
}
// If we actually have candidates for the requirement, then
@@ -977,7 +988,7 @@
+ module + " between an imported constraint "
+ sourceBlame + " and an additional imported constraint "
+ blame, module, blame.m_reqs.get(0));
- m_logger.log(Logger.LOG_DEBUG, ex.getMessage(), ex);
+ m_logger.log(Logger.LOG_DEBUG, "Conflicting fragment import", ex);
}
}
}
@@ -1027,7 +1038,7 @@
if (rethrow != null)
{
m_usesPermutations.add(copyConflict);
- m_logger.log(Logger.LOG_DEBUG, rethrow.getMessage(), rethrow);
+ m_logger.log(Logger.LOG_DEBUG, "Conflict between an export and import", rethrow);
throw rethrow;
}
}
@@ -1125,7 +1136,7 @@
m_usesPermutations.add(copyConflict);
}
- m_logger.log(Logger.LOG_DEBUG, rethrow.getMessage(), rethrow);
+ m_logger.log(Logger.LOG_DEBUG, "Conflict between imports", rethrow);
throw rethrow;
}
}