[jboss-osgi-commits] JBoss-OSGI SVN: r93316 - 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
Wed Sep 9 11:55:44 EDT 2009
Author: thomas.diesler at jboss.com
Date: 2009-09-09 11:55:44 -0400 (Wed, 09 Sep 2009)
New Revision: 93316
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/classloading/OSGiPackageCapability.java
projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageCapabilityCache.java
projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/service/PackageAdminImpl.java
Log:
[JBOSGI-151] Cannot resolve circular dependencies
Associate capability cache with PackageAdmin
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-09 15:55:10 UTC (rev 93315)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java 2009-09-09 15:55:44 UTC (rev 93316)
@@ -61,7 +61,6 @@
import org.jboss.osgi.plugins.facade.api.PackageAdminServicePlugin;
import org.jboss.osgi.plugins.facade.api.Plugin;
import org.jboss.osgi.plugins.facade.api.ServicePlugin;
-import org.jboss.osgi.plugins.facade.classloading.OSGiPackageCapabilityCache;
import org.jboss.osgi.plugins.filter.NoFilter;
import org.jboss.osgi.plugins.metadata.AbstractOSGiMetaData;
import org.jboss.osgi.spi.metadata.OSGiMetaData;
@@ -640,12 +639,11 @@
ControllerState requiredState = context.getRequiredState();
DeploymentStage requiredStage = unit.getRequiredStage();
- OSGiPackageCapabilityCache capabilityCache = OSGiPackageCapabilityCache.getInstance(bundleState);
try
{
+ log.info("Resolve: " + bundleState.getCanonicalName());
deployerClient.change(unit.getName(), DeploymentStages.CLASSLOADER);
deployerClient.checkComplete(unit.getName());
- capabilityCache.commitPreliminaryMatches();
bundleState.changeState(Bundle.RESOLVED);
return true;
}
@@ -654,8 +652,6 @@
unit.setRequiredStage(requiredStage);
context.setRequiredState(requiredState);
- capabilityCache.rollbackPreliminaryMatches();
-
if (errorOnFail)
throw new IllegalStateException("Error resolving bundle: " + bundleState, ex);
Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageCapability.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageCapability.java 2009-09-09 15:55:10 UTC (rev 93315)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageCapability.java 2009-09-09 15:55:44 UTC (rev 93316)
@@ -144,20 +144,23 @@
PackageAttribute ourParameters = exportPackage;
PackageAttribute otherParameters = packageRequirement.getRequirePackage();
- OSGiPackageCapabilityCache capabilityCache = OSGiPackageCapabilityCache.getInstance(bundleState);
- if (capabilityCache.isBlacklistedMatch(requirement, capModule))
+ OSGiPackageCapabilityCache capabilityCache = OSGiPackageCapabilityCache.getInstance();
+ if (capabilityCache != null)
{
- log.debug("Blacklisted: " + requirement + " " + capModule);
- return false;
+ if (capabilityCache.isBlacklisted(packageRequirement, capModule))
+ {
+ log.debug("Blacklisted: " + requirement + " " + capModule);
+ return false;
+ }
+
+ Module cachedModule = capabilityCache.getCachedModule(packageRequirement);
+ if (cachedModule == capModule)
+ {
+ log.debug("Cached: " + requirement + " " + capModule);
+ return true;
+ }
}
- Module cachedModule = capabilityCache.getCachedMatch(requirement);
- if (cachedModule == capModule)
- {
- log.debug("Cached: " + requirement + " " + capModule);
- return true;
- }
-
boolean validMatch = true;
// Check all the manadatory attributes are present
@@ -212,8 +215,8 @@
}
}
- if (validMatch == true && capModule == reqModule)
- capabilityCache.addPreliminaryMatch(requirement, capModule);
+ if (capabilityCache != null && validMatch == true)
+ capabilityCache.addModuleAssociation(packageRequirement, capModule);
return validMatch;
}
Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageCapabilityCache.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageCapabilityCache.java 2009-09-09 15:55:10 UTC (rev 93315)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiPackageCapabilityCache.java 2009-09-09 15:55:44 UTC (rev 93316)
@@ -21,12 +21,13 @@
*/
package org.jboss.osgi.plugins.facade.classloading;
-// $Id: $
-
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import org.jboss.classloading.plugins.metadata.PackageRequirement;
import org.jboss.classloading.spi.dependency.Module;
import org.jboss.classloading.spi.dependency.RequirementDependencyItem;
import org.jboss.classloading.spi.metadata.Requirement;
@@ -38,9 +39,7 @@
import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
/**
- * A package capabilty cache that helps the OSGiPackageCapability to decide
- * if a given requirement matches against a capability in the context of both
- * modules.
+ * A package capabilty cache that helps the OSGiPackageCapability to decide if a given requirement matches against a capability in the context of both modules.
*
* @author Thomas.Diesler at jboss.com
* @since 08-Sep-2009
@@ -49,72 +48,97 @@
{
/** The log */
private static final Logger log = Logger.getLogger(OSGiPackageCapabilityCache.class);
-
- private OSGiBundleState bundleState;
- private Map<Requirement, Module> preliminaryMatches = new HashMap<Requirement, Module>();
- private Map<Requirement, Module> blacklistedMatches = new HashMap<Requirement, Module>();
- private Map<Requirement, Module> cachedMatches = new HashMap<Requirement, Module>();
- public static OSGiPackageCapabilityCache getInstance(OSGiBundleState bundleState)
+ private Map<String, List<Module>> moduleAssociations = new HashMap<String, List<Module>>();
+ private Map<String, Module> blacklistedModules = new HashMap<String, Module>();
+ private Map<String, Module> cachedModules = new HashMap<String, Module>();
+ private static ThreadLocal<OSGiPackageCapabilityCache> threadLocal = new ThreadLocal<OSGiPackageCapabilityCache>();
+
+ public static OSGiPackageCapabilityCache createInstance()
{
- DeploymentUnit unit = bundleState.getDeploymentUnit();
- OSGiPackageCapabilityCache cacheInstance = unit.getAttachment(OSGiPackageCapabilityCache.class);
- if (cacheInstance == null)
- {
- cacheInstance = new OSGiPackageCapabilityCache(bundleState);
- unit.addAttachment(OSGiPackageCapabilityCache.class, cacheInstance);
- }
+ OSGiPackageCapabilityCache cacheInstance = new OSGiPackageCapabilityCache();
+ threadLocal.set(cacheInstance);
return cacheInstance;
}
-
- private OSGiPackageCapabilityCache(OSGiBundleState bundleState)
+
+ public static OSGiPackageCapabilityCache getInstance()
{
- if (bundleState == null)
- throw new IllegalArgumentException("Null bundle");
- this.bundleState = bundleState;
+ OSGiPackageCapabilityCache cacheInstance = threadLocal.get();
+ return cacheInstance;
}
-
- public Module getCachedMatch(Requirement requirement)
+
+ public void release()
{
- return cachedMatches.get(requirement);
+ threadLocal.remove();
}
-
- public void addPreliminaryMatch(Requirement requirement, Module module)
+
+ private OSGiPackageCapabilityCache()
{
- if (preliminaryMatches.containsKey(requirement) == false)
+ }
+
+ public void addModuleAssociation(PackageRequirement requirement, Module module)
+ {
+ String packageName = requirement.getName();
+ if (isBlacklisted(requirement, module))
+ throw new IllegalStateException("Cannot add blacklisted association: [" + packageName + "=" + getModuleName(module) + "]");
+
+ List<Module> modules = moduleAssociations.get(packageName);
+ if (modules == null)
{
- log.debug("Add " + requirement + " " + getModuleName(module));
- preliminaryMatches.put(requirement, module);
+ modules = new ArrayList<Module>();
+ moduleAssociations.put(packageName, modules);
+ modules.add(module);
}
+ else
+ {
+ Module firstMatch = modules.get(0);
+ if (firstMatch != module && modules.contains(module) == false)
+ {
+ log.info("Add [" + packageName + "=[" + getModuleName(firstMatch)+ " ... " + getModuleName(module) + "]]");
+ modules.add(module);
+ }
+ }
}
-
- public boolean isBlacklistedMatch(Requirement requirement, Module module)
+
+ public Module getCachedModule(PackageRequirement requirement)
{
- Module other = blacklistedMatches.get(requirement);
+ String packageName = requirement.getName();
+ return cachedModules.get(packageName);
+ }
+
+ public boolean isBlacklisted(PackageRequirement requirement, Module module)
+ {
+ String packageName = requirement.getName();
+ Module other = blacklistedModules.get(packageName);
boolean blacklisted = (module == other);
- if (blacklisted)
- log.info("Blacklisted " + requirement + " " + getModuleName(module));
-
return blacklisted;
}
-
- public void commitPreliminaryMatches()
+
+ public void endorseModuleAssociations()
{
- log.info(getLogMessage("Commit preliminary matches", preliminaryMatches));
- cachedMatches.putAll(preliminaryMatches);
- preliminaryMatches.clear();
+ Map<String, Module> firstMatches = getMapOfFirstMatches(moduleAssociations);
+ if (firstMatches.isEmpty() == false)
+ {
+ log.info(getLogMessage("Endorse module associations", firstMatches));
+ cachedModules.putAll(firstMatches);
+ }
+ moduleAssociations.clear();
}
- public void rollbackPreliminaryMatches()
+ public void blacklistModuleAssociations(OSGiBundleState bundleState)
{
- log.info(getLogMessage("Rollback preliminary matches", preliminaryMatches));
- blacklistedMatches.putAll(preliminaryMatches);
- preliminaryMatches.clear();
-
+ Map<String, Module> firstMatches = getMapOfFirstMatches(moduleAssociations);
+ if (firstMatches.isEmpty() == false)
+ {
+ log.info(getLogMessage("Blacklist module associations", firstMatches));
+ blacklistedModules.putAll(firstMatches);
+ }
+ moduleAssociations.clear();
+
DeploymentUnit unit = bundleState.getDeploymentUnit();
ControllerContext context = unit.getAttachment(ControllerContext.class);
DependencyInfo di = context.getDependencyInfo();
-
+
StringBuffer message = new StringBuffer("Unresolved requirements: ");
for (DependencyItem iDependOn : di.getIDependOn(RequirementDependencyItem.class))
{
@@ -131,19 +155,32 @@
message.append("\n " + unresolved);
}
}
-
+
// Log all unresolved dependency items
log.info(message);
}
- private String getLogMessage(String message, Map<Requirement, Module> matches)
+ private Map<String, Module> getMapOfFirstMatches(Map<String, List<Module>> moduleAssociations)
{
+ Map<String, Module> firstMatches = new HashMap<String, Module>();
+ for (Entry<String, List<Module>> entry : moduleAssociations.entrySet())
+ {
+ String packageName = entry.getKey();
+ List<Module> modules = entry.getValue();
+ if (modules.size() > 1)
+ firstMatches.put(packageName, modules.get(0));
+ }
+ return firstMatches;
+ }
+
+ private String getLogMessage(String message, Map<String, Module> matches)
+ {
StringBuffer buffer = new StringBuffer(message);
- for (Entry<Requirement, Module> entry : matches.entrySet())
+ for (Entry<String, Module> entry : matches.entrySet())
{
- Requirement requirement = entry.getKey();
+ String packageName = entry.getKey();
Module module = entry.getValue();
- buffer.append("\n " + requirement + " " + getModuleName(module));
+ buffer.append("\n [" + packageName + "=" + getModuleName(module) + "]");
}
return buffer.toString();
}
Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/service/PackageAdminImpl.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/service/PackageAdminImpl.java 2009-09-09 15:55:10 UTC (rev 93315)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/service/PackageAdminImpl.java 2009-09-09 15:55:44 UTC (rev 93316)
@@ -42,6 +42,7 @@
import org.jboss.osgi.plugins.facade.bundle.OSGiBundleManager;
import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
import org.jboss.osgi.plugins.facade.bundle.OSGiBundleWrapper;
+import org.jboss.osgi.plugins.facade.classloading.OSGiPackageCapabilityCache;
import org.jboss.osgi.plugins.facade.plugins.AbstractServicePluginImpl;
import org.jboss.osgi.spi.NotImplementedException;
import org.osgi.framework.Bundle;
@@ -180,22 +181,36 @@
if (unresolvedBundles.isEmpty())
return true;
+ int resolved = 1;
+
// Repeatedly try to resolve the bundles until no more bundle can be resolved
- int resolved = 1;
- while (resolved > 0)
+ OSGiPackageCapabilityCache cacheInstance = OSGiPackageCapabilityCache.createInstance();
+ try
{
- resolved = 0;
- Iterator<AbstractBundleState> it = unresolvedBundles.iterator();
- while (it.hasNext())
+ while (resolved > 0)
{
- OSGiBundleState bundleState = assertBundleState(it.next());
- if (bundleManager.resolve(bundleState, false))
+ resolved = 0;
+ Iterator<AbstractBundleState> it = unresolvedBundles.iterator();
+ while (it.hasNext())
{
- it.remove();
- resolved++;
+ OSGiBundleState bundleState = assertBundleState(it.next());
+ if (bundleManager.resolve(bundleState, false))
+ {
+ cacheInstance.endorseModuleAssociations();
+ it.remove();
+ resolved++;
+ }
+ else
+ {
+ cacheInstance.blacklistModuleAssociations(bundleState);
+ }
}
}
}
+ finally
+ {
+ cacheInstance.release();
+ }
// Log unresolved bundles
for (AbstractBundleState aux : unresolvedBundles)
More information about the jboss-osgi-commits
mailing list