[jboss-osgi-commits] JBoss-OSGI SVN: r93661 - in projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins: facade/classloading and 1 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Thu Sep 17 18:33:23 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-09-17 18:33:22 -0400 (Thu, 17 Sep 2009)
New Revision: 93661

Modified:
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageRequirement.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleCapability.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleRequirement.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleResolver.java
Log:
[JBOSGI-155] OutOfMemoryError on repeated install/unstall bundle
Fix leak in BundleResolver

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java	2009-09-17 21:04:22 UTC (rev 93660)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java	2009-09-17 22:33:22 UTC (rev 93661)
@@ -222,15 +222,6 @@
    }
 
    /**
-    * Destroy this bundle state
-    */
-   void destroy()
-   {
-      lastModified = System.currentTimeMillis();
-      osgiMetaData = null;
-   }
-   
-   /**
     * Get the osgiMetaData.
     * 
     * @return the osgiMetaData.

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java	2009-09-17 21:04:22 UTC (rev 93660)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java	2009-09-17 22:33:22 UTC (rev 93661)
@@ -447,7 +447,7 @@
       try
       {
          deployerClient.undeploy(unit.getName());
-         bundleState.destroy();
+         bundleState.modified();
       }
       catch (DeploymentException e)
       {

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java	2009-09-17 21:04:22 UTC (rev 93660)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java	2009-09-17 22:33:22 UTC (rev 93661)
@@ -386,11 +386,4 @@
       if (rethrow != null)
          throw rethrow;
    }
-
-   @Override
-   void destroy()
-   {
-      super.destroy();
-      unit = null;
-   }
 }

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageRequirement.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageRequirement.java	2009-09-17 21:04:22 UTC (rev 93660)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageRequirement.java	2009-09-17 22:33:22 UTC (rev 93661)
@@ -52,6 +52,8 @@
 
    /** The attributes */
    private PackageAttribute requirePackage;
+   
+   public static long count;
 
    /**
     * Create a new OSGiPackageRequirement.
@@ -110,7 +112,15 @@
          if (Constants.RESOLUTION_OPTIONAL.equals(resolution))
             setOptional(true);
       }
+      
+      count++;
    }
+   
+   @Override
+   protected void finalize() throws Throwable
+   {
+      count--;
+   }
 
    /**
     * Get the requirePackage metadata.

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleCapability.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleCapability.java	2009-09-17 21:04:22 UTC (rev 93660)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleCapability.java	2009-09-17 22:33:22 UTC (rev 93661)
@@ -31,7 +31,6 @@
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
 import org.jboss.osgi.plugins.facade.classloading.OSGiPackageCapability;
 import org.jboss.osgi.plugins.facade.classloading.OSGiPackageRequirement;
-import org.osgi.framework.Bundle;
 
 /**
  * An association of bundle/capability.
@@ -41,17 +40,22 @@
  */
 public class BundleCapability
 {
-   private Bundle bundle;
+   private OSGiBundleState bundle;
    private PackageCapability packageCapability;
    private List<BundleRequirement> wires;
    
-   BundleCapability(Bundle bundle, PackageCapability packageCapability)
+   BundleCapability(OSGiBundleState bundle, PackageCapability packageCapability)
    {
+      if (bundle == null)
+         throw new IllegalArgumentException("Null bundle");
+      if (packageCapability == null)
+         throw new IllegalArgumentException("Null packageCapability");
+      
       this.bundle = bundle;
       this.packageCapability = packageCapability;
    }
 
-   public Bundle getExportingBundle()
+   public OSGiBundleState getExportingBundle()
    {
       return bundle;
    }
@@ -68,27 +72,52 @@
       return packageCapability;
    }
 
-   public List<BundleRequirement> getWires()
+   public List<BundleRequirement> getWiredRequirements()
    {
+      if (wires == null)
+         return Collections.emptyList();
+      
       return Collections.unmodifiableList(wires);
    }
 
-   void addWire(BundleRequirement bundleRequirement)
+   boolean matches(BundleRequirement bundleRequirement)
    {
+      OSGiPackageCapability osgiPackageCapability = (OSGiPackageCapability)packageCapability;
+      OSGiPackageRequirement osgiPackageRequirement = (OSGiPackageRequirement)bundleRequirement.getPackageRequirement();
+      return osgiPackageCapability.match(osgiPackageRequirement);
+   }
+
+   void wireRequirement(BundleRequirement bundleRequirement)
+   {
       if (wires == null)
          wires = new ArrayList<BundleRequirement>();
       
       wires.add(bundleRequirement);
    }
    
-   boolean matches(BundleRequirement bundleRequirement)
+   void unwireRequirement(BundleRequirement bundleRequirement)
    {
-      OSGiPackageCapability osgiPackageCapability = (OSGiPackageCapability)packageCapability;
-      OSGiPackageRequirement osgiPackageRequirement = (OSGiPackageRequirement)bundleRequirement.getPackageRequirement();
-      return osgiPackageCapability.match(osgiPackageRequirement);
+      if (wires != null)
+         wires.remove(bundleRequirement);
    }
+   
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (obj instanceof BundleCapability == false)
+         return false;
+      if (obj == this)
+         return true;
+      return toString().equals(obj.toString());
+   }
 
    @Override
+   public int hashCode()
+   {
+      return toString().hashCode();
+   }
+
+   @Override
    public String toString()
    {
       String name = packageCapability.getName();

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleRequirement.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleRequirement.java	2009-09-17 21:04:22 UTC (rev 93660)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleRequirement.java	2009-09-17 22:33:22 UTC (rev 93661)
@@ -22,7 +22,7 @@
 package org.jboss.osgi.plugins.resolver;
 
 import org.jboss.classloading.plugins.metadata.PackageRequirement;
-import org.osgi.framework.Bundle;
+import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
 
 /**
  * An association of bundle/requirement.
@@ -32,36 +32,66 @@
  */
 public class BundleRequirement
 {
-   private Bundle bundle;
+   private OSGiBundleState bundle;
    private PackageRequirement packageRequirement;
    private BundleCapability wire;
    
-   BundleRequirement(Bundle bundle, PackageRequirement packageRequirement)
+   BundleRequirement(OSGiBundleState bundle, PackageRequirement packageRequirement)
    {
+      if (bundle == null)
+         throw new IllegalArgumentException("Null bundle");
+      if (packageRequirement == null)
+         throw new IllegalArgumentException("Null packageRequirement");
+      
       this.bundle = bundle;
       this.packageRequirement = packageRequirement;
    }
+   
+   public BundleCapability getWiredCapability()
+   {
+      return wire;
+   }
 
-   void setWire(BundleCapability bundleCapability)
+   public OSGiBundleState getImportingBundle()
    {
+      return bundle;
+   }
+
+   public PackageRequirement getPackageRequirement()
+   {
+      return packageRequirement;
+   }
+
+   void wireCapability(BundleCapability bundleCapability)
+   {
       wire = bundleCapability;
       if (bundleCapability != null)
-         bundleCapability.addWire(this);
+         bundleCapability.wireRequirement(this);
    }
 
-   public BundleCapability getWire()
+   void unwireCapability()
    {
-      return wire;
+      if (wire != null)
+      {
+         wire.unwireRequirement(this);
+         wire = null;
+      }
    }
-
-   public Bundle getImportingBundle()
+   
+   @Override
+   public boolean equals(Object obj)
    {
-      return bundle;
+      if (obj instanceof BundleRequirement == false)
+         return false;
+      if (obj == this)
+         return true;
+      return toString().equals(obj.toString());
    }
 
-   public PackageRequirement getPackageRequirement()
+   @Override
+   public int hashCode()
    {
-      return packageRequirement;
+      return toString().hashCode();
    }
 
    @Override

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleResolver.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleResolver.java	2009-09-17 21:04:22 UTC (rev 93660)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/resolver/BundleResolver.java	2009-09-17 22:33:22 UTC (rev 93661)
@@ -60,9 +60,8 @@
    private static final Logger log = Logger.getLogger(BundleResolver.class);
 
    private OSGiBundleManager bundleManager;
-   private List<BundleCapability> allCapabilities = new ArrayList<BundleCapability>();
-   private Map<Bundle, List<BundleCapability>> bundleCapabilitiesMap = new HashMap<Bundle, List<BundleCapability>>();
-   private Map<Bundle, List<BundleRequirement>> bundleRequirementsMap = new HashMap<Bundle, List<BundleRequirement>>();
+   private Map<OSGiBundleState, List<BundleCapability>> bundleCapabilitiesMap = new HashMap<OSGiBundleState, List<BundleCapability>>();
+   private Map<OSGiBundleState, List<BundleRequirement>> bundleRequirementsMap = new HashMap<OSGiBundleState, List<BundleRequirement>>();
 
    public BundleResolver(OSGiBundleManager bundleManager)
    {
@@ -91,6 +90,13 @@
       int resolved = 1;
       int resolveRounds = 0;
 
+      // Get the list of all capabilities
+      List<BundleCapability> allCapabilities = new ArrayList<BundleCapability>();
+      for (List<BundleCapability> list : bundleCapabilitiesMap.values())
+      {
+         allCapabilities.addAll(list);
+      }
+      
       List<OSGiBundleState> resolvedBundles = new ArrayList<OSGiBundleState>();
       while (resolved > 0 && unresolvedBundles.isEmpty() == false)
       {
@@ -105,7 +111,7 @@
          {
             OSGiBundleState bundleState = it.next();
             log.debug("Resolving: " + bundleState);
-            if (resolveBundle(bundleState))
+            if (resolveBundle(allCapabilities, bundleState))
             {
                resolvedBundles.add(bundleState);
                it.remove();
@@ -117,7 +123,7 @@
       log.debug("END *****************************************************************");
 
       // Log the unresolved bundles
-      for (Bundle bundle : unresolvedBundles)
+      for (OSGiBundleState bundle : unresolvedBundles)
       {
          StringBuffer message = new StringBuffer("Unresolved bundle: " + bundle);
          message.append("\n  Cannot find exporter for");
@@ -139,22 +145,18 @@
 
    public void removeBundle(Bundle bundle)
    {
-      //System.out.println("removeBundle: " + bundle);
+      OSGiBundleState bundleState = assertBundleState(bundle);
       
-      OSGiBundleState bundleState = assertBundleState(bundle);
-      Iterator<BundleCapability> it = allCapabilities.iterator();
-      while (it.hasNext())
+      bundleCapabilitiesMap.remove(bundleState);
+      
+      List<BundleRequirement> bundleRequirements = bundleRequirementsMap.remove(bundleState);
+      if (bundleRequirements != null)
       {
-         BundleCapability aux = it.next();
-         if (aux.getExportingBundle().equals(bundleState))
-            it.remove();
+         for (BundleRequirement requirement : bundleRequirements)
+         {
+            requirement.unwireCapability();
+         }
       }
-      bundleCapabilitiesMap.remove(bundleState);
-      bundleRequirementsMap.remove(bundleState);
-      
-      //System.out.println("allCapabilities: " + allCapabilities.size());
-      //System.out.println("bundleCapabilitiesMap: " + bundleCapabilitiesMap.size());
-      //System.out.println("bundleRequirementsMap: " + bundleRequirementsMap.size());
    }
 
    public BundleCapability getMatchingCapability(Module module, PackageRequirement packageRequirement)
@@ -173,7 +175,7 @@
       {
          if (req.getPackageRequirement() == packageRequirement)
          {
-            result = req.getWire();
+            result = req.getWiredCapability();
             break;
          }
       }
@@ -181,7 +183,7 @@
       return result;
    }
 
-   private boolean resolveBundle(OSGiBundleState bundle)
+   private boolean resolveBundle(List<BundleCapability> allCapabilities, OSGiBundleState bundle)
    {
       List<BundleCapability> bundleCapabilities = getBundleCapabilities(bundle);
       List<BundleRequirement> bundleRequirements = getBundleRequirements(bundle);
@@ -197,14 +199,14 @@
          if (bestMatch == null && packreq.isOptional() == false && packreq.isDynamic() == false)
             return false;
 
-         requirement.setWire(bestMatch);
+         requirement.wireCapability(bestMatch);
       }
 
       // Remove optional or dynamic requirements that don't have a wire 
       Iterator<BundleRequirement> it = bundleRequirements.iterator();
       while (it.hasNext())
       {
-         if (it.next().getWire() == null)
+         if (it.next().getWiredCapability() == null)
             it.remove();
       }
 
@@ -271,7 +273,7 @@
             for (int i = 0; i < (nameLengthMax - packNameVersion.length()); i++)
                line.append(" ");
 
-            BundleCapability wire = requirement.getWire();
+            BundleCapability wire = requirement.getWiredCapability();
             if (wire == null)
             {
                line.append(" <= null");
@@ -315,7 +317,7 @@
    /**
     * Get the set of bundle capabilities
     */
-   private List<BundleCapability> getBundleCapabilities(Bundle bundle)
+   private List<BundleCapability> getBundleCapabilities(OSGiBundleState bundle)
    {
       List<BundleCapability> result = new ArrayList<BundleCapability>();
 
@@ -341,7 +343,7 @@
    /**
     * Get the set of bundle requirements
     */
-   private List<BundleRequirement> getBundleRequirements(Bundle bundle)
+   private List<BundleRequirement> getBundleRequirements(OSGiBundleState bundle)
    {
       List<BundleRequirement> result = new ArrayList<BundleRequirement>();
 
@@ -415,7 +417,7 @@
 
                   OSGiPackageRequirement newPackageRequirement = new OSGiPackageRequirement(bundle, packageName, versionRange, null);
                   BundleRequirement newBundleRequirement = new BundleRequirement(bundle, newPackageRequirement);
-                  newBundleRequirement.setWire(otherCapability);
+                  newBundleRequirement.wireCapability(otherCapability);
                   bundleRequirements.add(newBundleRequirement);
 
                   if (Constants.VISIBILITY_REEXPORT.equals(visibility))



More information about the jboss-osgi-commits mailing list