You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2010/05/14 21:06:00 UTC

svn commit: r944402 - in /felix/trunk/framework/src/main/java/org/apache/felix/framework: Felix.java resolver/ResolverImpl.java

Author: rickhall
Date: Fri May 14 19:06:00 2010
New Revision: 944402

URL: http://svn.apache.org/viewvc?rev=944402&view=rev
Log:
More code cleanup. (FELIX-2035)

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?rev=944402&r1=944401&r2=944402&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java Fri May 14 19:06:00 2010
@@ -4068,10 +4068,63 @@ m_logger.log(Logger.LOG_DEBUG, "DYNAMIC 
             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)

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java?rev=944402&r1=944401&r2=944402&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java Fri May 14 19:06:00 2010
@@ -85,62 +85,68 @@ public class ResolverImpl implements Res
 
         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>>();
+            try
+            {
+                Map<Requirement, Set<Capability>> candidateMap =
+                    new HashMap<Requirement, Set<Capability>>();
 
-            populateCandidates(state, module, candidateMap, new HashMap<Module, Object>());
-            m_usesPermutations.add(candidateMap);
+                populateCandidates(
+                    state, module, candidateMap, new HashMap<Module, Object>());
+                m_usesPermutations.add(candidateMap);
 
-            ResolveException rethrow = null;
+                ResolveException rethrow = null;
 
-            Map<Capability, Set<Requirement>> capDepSet = new HashMap();
+                Map<Capability, Set<Requirement>> capDepSet = new HashMap();
 
-            do
-            {
-                rethrow = null;
+                do
+                {
+                    rethrow = null;
 
-                modulePkgMap.clear();
-                capDepSet.clear();
-                m_packageSourcesCache.clear();
+                    modulePkgMap.clear();
+                    capDepSet.clear();
+                    m_packageSourcesCache.clear();
 
-                candidateMap = (m_usesPermutations.size() > 0)
-                    ? m_usesPermutations.remove(0)
-                    : m_importPermutations.remove(0);
+                    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)
+                while ((rethrow != null)
+                    && ((m_usesPermutations.size() > 0) || (m_importPermutations.size() > 0)));
+
+                if (rethrow != null)
                 {
-                    rethrow = ex;
-                    System.out.println("RE: " + ex);
+                    throw rethrow;
                 }
-            }
-            while ((rethrow != null)
-                && ((m_usesPermutations.size() > 0) || (m_importPermutations.size() > 0)));
 
-            if (rethrow != null)
-            {
-                throw rethrow;
+                wireMap =
+                    populateWireMap(module, modulePkgMap, wireMap,
+                    candidateMap);
+            }
+            finally
+            {
+                // Always clear the state.
+                m_usesPermutations.clear();
+                m_importPermutations.clear();
             }
-
-            wireMap =
-                populateWireMap(module, modulePkgMap, wireMap,
-                candidateMap);
         }
 
         if (m_isInvokeCount)
@@ -174,67 +180,72 @@ public class ResolverImpl implements Res
             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();
+            try
+            {
+                Map<Module, List<Wire>> wireMap = new HashMap();
+                Map<Module, Packages> modulePkgMap = new HashMap();
 
-            populateDynamicCandidates(state, module, candidateMap);
-            m_usesPermutations.add(candidateMap);
+                populateDynamicCandidates(state, module, candidateMap);
+                m_usesPermutations.add(candidateMap);
 
-            ResolveException rethrow = null;
+                ResolveException rethrow = null;
 
-            Map<Capability, Set<Requirement>> capDepSet = new HashMap();
+                Map<Capability, Set<Requirement>> capDepSet = new HashMap();
 
-            do
-            {
-                rethrow = null;
+                do
+                {
+                    rethrow = null;
 
-                modulePkgMap.clear();
-                capDepSet.clear();
+                    modulePkgMap.clear();
+                    capDepSet.clear();
 
-                candidateMap = (m_usesPermutations.size() > 0)
-                    ? m_usesPermutations.remove(0)
-                    : m_importPermutations.remove(0);
+                    candidateMap = (m_usesPermutations.size() > 0)
+                        ? m_usesPermutations.remove(0)
+                        : m_importPermutations.remove(0);
 
-                calculatePackageSpaces(
-                    module, candidateMap, modulePkgMap,
-                    capDepSet, new HashMap(), new HashSet());
+                    calculatePackageSpaces(
+                        module, candidateMap, modulePkgMap,
+                        capDepSet, new HashMap(), new HashSet());
 
-                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)
+                while ((rethrow != null)
+                    && ((m_usesPermutations.size() > 0)
+                        || (m_importPermutations.size() > 0)));
+
+                if (rethrow != null)
                 {
-                    rethrow = ex;
-                    System.out.println("RE: " + ex);
+                    throw rethrow;
                 }
-            }
-            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 @@ public class ResolverImpl implements Res
                     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 @@ public class ResolverImpl implements Res
                             + 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 @@ public class ResolverImpl implements Res
             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 @@ public class ResolverImpl implements Res
                         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;
                 }
             }