felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dsav...@apache.org
Subject svn commit: r820299 - in /felix/trunk/sigil/common/core/src/org/apache/felix/sigil: core/repository/BundleResolver.java model/IRequirementModelElement.java model/osgi/IPackageImport.java model/osgi/IRequiredBundle.java
Date Wed, 30 Sep 2009 15:24:22 GMT
Author: dsavage
Date: Wed Sep 30 15:24:22 2009
New Revision: 820299

URL: http://svn.apache.org/viewvc?rev=820299&view=rev
Log:
refactored BundleResolver to first find all requirements then explicitly sort them before
resolving dependencies (FELIX-1664)
also minor refactoring to add isOptional/setOptional to parent IRequirementModelElement interface

Modified:
    felix/trunk/sigil/common/core/src/org/apache/felix/sigil/core/repository/BundleResolver.java
    felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/IRequirementModelElement.java
    felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IPackageImport.java
    felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IRequiredBundle.java

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/core/repository/BundleResolver.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/core/repository/BundleResolver.java?rev=820299&r1=820298&r2=820299&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/core/repository/BundleResolver.java
(original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/core/repository/BundleResolver.java
Wed Sep 30 15:24:22 2009
@@ -35,6 +35,8 @@
 import org.apache.felix.sigil.core.BldCore;
 import org.apache.felix.sigil.model.ICompoundModelElement;
 import org.apache.felix.sigil.model.IModelElement;
+import org.apache.felix.sigil.model.IModelWalker;
+import org.apache.felix.sigil.model.IRequirementModelElement;
 import org.apache.felix.sigil.model.eclipse.ILibrary;
 import org.apache.felix.sigil.model.eclipse.ILibraryImport;
 import org.apache.felix.sigil.model.eclipse.ISigilBundle;
@@ -56,7 +58,7 @@
 public class BundleResolver implements IBundleResolver
 {
 
-    private class BundleOrderComparator implements Comparator<ISigilBundle>
+    private static class BundleOrderComparator implements Comparator<ISigilBundle>
     {
         private IModelElement requirement;
 
@@ -155,7 +157,7 @@
 
     }
 
-    private class ResolutionContext
+    private static class ResolutionContext
     {
         private final IModelElement root;
         private final ResolutionConfig config;
@@ -210,18 +212,18 @@
         }
 
 
-        public void startRequirement( IModelElement element )
+        public void startRequirement( IRequirementModelElement element )
         {
             requirements.add( element );
             monitor.startResolution( element );
         }
 
 
-        public void endRequirement( IModelElement element )
+        public void endRequirement( IRequirementModelElement element )
         {
             ISigilBundle provider = resolution.getProvider( element );
 
-            setValid( provider != null || isOptional( element ) || config.isIgnoreErrors()
);
+            setValid( provider != null || element.isOptional() || config.isIgnoreErrors()
);
 
             if ( isValid() )
             {
@@ -234,7 +236,7 @@
         }
     }
 
-    private class Resolution implements IResolution
+    private static class Resolution implements IResolution
     {
         private Map<ISigilBundle, List<IModelElement>> providees = new HashMap<ISigilBundle,
List<IModelElement>>();
         private Map<IModelElement, ISigilBundle> providers = new HashMap<IModelElement,
ISigilBundle>();
@@ -359,6 +361,30 @@
         }
     };
 
+    private static final Comparator<IRequirementModelElement> REQUIREMENT_COMPARATOR
= new Comparator<IRequirementModelElement>()
+    {
+        public int compare(IRequirementModelElement o1, IRequirementModelElement o2)
+        {
+            if ( o1 instanceof IPackageImport ) {
+                if (o2 instanceof IPackageImport ) {
+                    return 0;
+                }
+                else {
+                    return -1;
+                }
+            }
+            else // assumes only alternative is IRequiredBundle
+            {
+                if ( o2 instanceof IRequiredBundle) {
+                    return 0;
+                }
+                else {
+                    return 1;
+                }
+            }
+        }
+    };
+
     private IRepositoryManager repositoryManager;
 
 
@@ -387,53 +413,56 @@
         return ctx.resolution;
     }
 
-
     private void resolveElement( IModelElement element, ResolutionContext ctx ) throws ResolutionException
     {
-        if ( isRequirement( element ) )
-        {
-            resolveRequirement( element, ctx );
-        }
-
-        if ( ctx.isValid() && element instanceof ICompoundModelElement )
-        {
-            resolveCompound( ( ICompoundModelElement ) element, ctx );
+        if ( ctx.isValid() ) {
+            for (IRequirementModelElement req : findRequirements(element, ctx)) {
+                if ( ctx.monitor.isCanceled())
+                    break;
+                resolveRequirement(req, ctx);
+            }
         }
     }
-
-
-    private void resolveCompound( ICompoundModelElement compound, ResolutionContext ctx )
throws ResolutionException
+    
+    private List<IRequirementModelElement> findRequirements(IModelElement element,
+        final ResolutionContext ctx)
     {
-        for ( IModelElement element : compound.children() )
+        final LinkedList<IRequirementModelElement> reqs = new LinkedList<IRequirementModelElement>();
+        
+        if (element instanceof IRequirementModelElement)
         {
-            if ( ctx.isNewModelElement( element ) )
+            reqs.add((IRequirementModelElement) element);
+        }
+        else if (element instanceof ICompoundModelElement)
+        {
+            ICompoundModelElement compound = (ICompoundModelElement) element;
+            compound.visit(new IModelWalker()
             {
-                if ( isRequirement( element ) )
-                {
-                    resolveRequirement( element, ctx );
-                }
-                else if ( element instanceof ICompoundModelElement )
+                public boolean visit(IModelElement element)
                 {
-                    if ( !ctx.monitor.isCanceled() )
+                    if (element instanceof IRequirementModelElement)
                     {
-                        ctx.enterModelElement( element );
-                        resolveElement( ( ICompoundModelElement ) element, ctx );
-                        ctx.exitModelElement( element );
+                        reqs.add((IRequirementModelElement) element);
                     }
+                    else if ( element instanceof ILibrary ) {
+                        ILibrary lib = repositoryManager.resolveLibrary( ( ILibraryImport
) element );
+                        reqs.addAll( lib.getImports() );
+                    }
+                    
+                    return !ctx.monitor.isCanceled();
                 }
-
-                if ( !ctx.isValid() )
-                {
-                    break;
-                }
-            }
+            });
         }
-    }
 
+        if ( !ctx.monitor.isCanceled() ) {
+            Collections.sort(reqs, REQUIREMENT_COMPARATOR);
+        }        
+        return reqs;
+    }
 
-    private void resolveRequirement( IModelElement requirement, ResolutionContext ctx ) throws
ResolutionException
+    private void resolveRequirement( IRequirementModelElement requirement, ResolutionContext
ctx ) throws ResolutionException
     {
-        if ( ctx.config.isOptional() || !isOptional( requirement ) )
+        if ( ctx.config.isOptional() || !requirement.isOptional() )
         {
             ctx.startRequirement( requirement );
 
@@ -488,7 +517,7 @@
     }
 
 
-    private List<ISigilBundle> findProvidersAtPriority( int i, IModelElement requirement,
ResolutionContext ctx )
+    private List<ISigilBundle> findProvidersAtPriority( int i, IRequirementModelElement
requirement, ResolutionContext ctx )
         throws ResolutionException
     {
         ArrayList<ISigilBundle> providers = new ArrayList<ISigilBundle>();
@@ -506,7 +535,7 @@
     }
 
 
-    private Collection<ISigilBundle> findProviders( IModelElement requirement, ResolutionConfig
config,
+    private Collection<ISigilBundle> findProviders( IRequirementModelElement requirement,
ResolutionConfig config,
         IBundleRepository rep ) throws ResolutionException
     {
         ArrayList<ISigilBundle> found = new ArrayList<ISigilBundle>();
@@ -521,14 +550,6 @@
             IRequiredBundle rb = ( IRequiredBundle ) requirement;
             found.addAll( rep.findAllProviders( rb, config.getOptions() ) );
         }
-        else if ( requirement instanceof ILibraryImport )
-        {
-            ILibrary lib = repositoryManager.resolveLibrary( ( ILibraryImport ) requirement
);
-            if ( lib != null )
-            {
-                found.addAll( rep.findProviders( lib, config.getOptions() ) );
-            }
-        }
         else
         {
             // shouldn't get here - developer error if do
@@ -538,43 +559,4 @@
 
         return found;
     }
-
-
-    private boolean isOptional( IModelElement element )
-    {
-        if ( element instanceof IPackageImport )
-        {
-            return ( ( IPackageImport ) element ).isOptional();
-        }
-        else if ( element instanceof IRequiredBundle )
-        {
-            return ( ( IRequiredBundle ) element ).isOptional();
-        }
-        else if ( element instanceof ILibraryImport )
-        {
-            ILibrary lib = repositoryManager.resolveLibrary( ( ILibraryImport ) element );
-            for ( IPackageImport pi : lib.getImports() )
-            {
-                if ( !isOptional( pi ) )
-                {
-                    return false;
-                }
-            }
-            return true;
-        }
-        else
-        {
-            // should never get this due to isRequirement test prior to calling this
-            // developer error if found
-            throw new IllegalStateException( "Invalid optional element test for " + element
);
-        }
-    }
-
-
-    private boolean isRequirement( IModelElement element )
-    {
-        return element instanceof IPackageImport || element instanceof IRequiredBundle
-            || element instanceof ILibraryImport;
-    }
-
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/IRequirementModelElement.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/IRequirementModelElement.java?rev=820299&r1=820298&r2=820299&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/IRequirementModelElement.java
(original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/IRequirementModelElement.java
Wed Sep 30 15:24:22 2009
@@ -20,7 +20,15 @@
 package org.apache.felix.sigil.model;
 
 
-public interface IRequirementModelElement
+public interface IRequirementModelElement extends IModelElement
 {
     boolean accepts( IModelElement provider );
+    
+    /**
+     * indicates whether the OSGi attribute "resolution=optional" is specified.
+     */
+    boolean isOptional();
+
+
+    void setOptional( boolean optional );
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IPackageImport.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IPackageImport.java?rev=820299&r1=820298&r2=820299&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IPackageImport.java
(original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IPackageImport.java
Wed Sep 30 15:24:22 2009
@@ -27,15 +27,6 @@
     Comparable<IPackageImport>
 {
     /**
-     * indicates whether the OSGi attribute "resolution=optional" is specified.
-     */
-    boolean isOptional();
-
-
-    void setOptional( boolean optional );
-
-
-    /**
      * indicates whether import is needed at compile-time.
      * Default true. Used in conjunction with OSGiHeader.ALWAYS,
      * to add an OSGI import, without creating a dependency.

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IRequiredBundle.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IRequiredBundle.java?rev=820299&r1=820298&r2=820299&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IRequiredBundle.java
(original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/osgi/IRequiredBundle.java
Wed Sep 30 15:24:22 2009
@@ -37,10 +37,4 @@
 
 
     void setVersions( VersionRange versions );
-
-
-    boolean isOptional();
-
-
-    void setOptional( boolean optional );
 }
\ No newline at end of file



Mime
View raw message