[Jboss-cvs] JBossAS SVN: r55842 - in projects/microcontainer/trunk: dependency/src/main/org/jboss/dependency/plugins dependency/src/main/org/jboss/dependency/spi kernel/src/main/org/jboss/beans/metadata/plugins kernel/src/main/org/jboss/kernel/plugins/dependency

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Aug 12 05:10:15 EDT 2006


Author: alesj
Date: 2006-08-12 05:10:05 -0400 (Sat, 12 Aug 2006)
New Revision: 55842

Modified:
   projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/plugins/AbstractController.java
   projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/spi/Controller.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractInjectionValueMetaData.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/InstantiateAction.java
Log:
Improved 'contextual injection'.

Modified: projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/plugins/AbstractController.java
===================================================================
--- projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/plugins/AbstractController.java	2006-08-12 07:21:58 UTC (rev 55841)
+++ projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/plugins/AbstractController.java	2006-08-12 09:10:05 UTC (rev 55842)
@@ -57,6 +57,9 @@
    /** The contexts by state Map<ControllerState, Set<ControllerContext>> */
    protected Map<ControllerState, Set<ControllerContext>> contextsByState = CollectionsFactory.createConcurrentReaderMap();
 
+   /** The contexts by class Map<Class, Set<ControllerContext>> */
+   protected Map<Class, Set<ControllerContext>> contextsByClass = CollectionsFactory.createConcurrentReaderMap();
+
    /** The error contexts Set<ControllerContext> */
    protected Set<ControllerContext> errorContexts = CollectionsFactory.createCopyOnWriteSet();
 
@@ -775,19 +778,99 @@
       lock.writeLock().unlock();
    }
 
-   // todo - already implemented - on another location - forgot to commit ;-(
+   /**
+    * @return all instantiated contexts whose target is instance of this class clazz param
+    */
+   public Set<ControllerContext> getInstantiatedContexts(Class clazz)
+   {
+      lockRead();
+      try
+      {
+         return contextsByClass.get(clazz);
+      }
+      finally
+      {
+         unlockRead();
+      }
+   }
 
-   public Set getInstantiatedBeans(Class clazz)
+   /**
+    * add instantiated context into contextsByClass map
+    * look at all target's superclasses and interfaces
+    */
+   public void addInstantiatedContext(ControllerContext context)
    {
-      return null;
+      prepareToTraverse(context, true);
    }
 
-   public void addInstantiatedBean(Object bean)
+   /**
+    * remove instantiated context from contextsByClass map
+    * look at all target's superclasses and interfaces
+    */
+   public void removeInstantiatedContext(ControllerContext context)
    {
+      prepareToTraverse(context, false);
    }
 
-   public void removeInstantiatedBean(Object bean)
+   protected void prepareToTraverse(ControllerContext context, boolean addition)
    {
+      lockWrite();
+      try
+      {
+         Object target = context.getTarget();
+         if (target != null)
+         {
+            traverseBean(context, target.getClass(), addition);
+         }
+      }
+      finally
+      {
+         unlockWrite();
+      }
    }
 
+   /**
+    * Traverse over target and map it to all its superclasses
+    * and interfaces - using recursion.
+    *
+    * @param context context whose target is instance of clazz
+    * @param clazz current class to map context to
+    */
+   protected void traverseBean(ControllerContext context, Class clazz, boolean addition)
+   {
+      if (clazz == null || clazz == Object.class)
+      {
+         return;
+      }
+      Set<ControllerContext> beans = contextsByClass.get(clazz);
+      if (addition)
+      {
+         if (beans == null)
+         {
+            beans = new HashSet<ControllerContext>();
+            contextsByClass.put(clazz, beans);
+         }
+         beans.add(context);
+      }
+      else
+      {
+         if (beans != null)
+         {
+            beans.remove(context);
+            if (beans.isEmpty())
+            {
+               contextsByClass.remove(clazz);
+            }
+         }
+      }
+      // traverse superclass
+      traverseBean(context, clazz.getSuperclass(), addition);
+      Class[] interfaces = clazz.getInterfaces();
+      // traverse interfaces
+      for(Class intface : interfaces)
+      {
+         traverseBean(context, intface, addition);
+      }
+   }
+
 }

Modified: projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/spi/Controller.java
===================================================================
--- projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/spi/Controller.java	2006-08-12 07:21:58 UTC (rev 55841)
+++ projects/microcontainer/trunk/dependency/src/main/org/jboss/dependency/spi/Controller.java	2006-08-12 09:10:05 UTC (rev 55842)
@@ -112,20 +112,20 @@
    List<ControllerState> getStates();
 
    /**
-    * @return all instantiated beans that are instance of this class clazz param
+    * @return all instantiated contexts whose target is instance of this class clazz param
     */
-   Set getInstantiatedBeans(Class clazz);
+   Set<ControllerContext> getInstantiatedContexts(Class clazz);
 
    /**
-    * add instantiated bean into beansByClass map
-    * look at all superclasses and interfaces
+    * add instantiated context into contextsByClass map
+    * look at all target's superclasses and interfaces
     */
-   void addInstantiatedBean(Object bean);
+   void addInstantiatedContext(ControllerContext context);
 
    /**
-    * remove instantiated bean from beansByClass map
-    * look at all superclasses and interfaces
+    * remove instantiated context from contextsByClass map
+    * look at all target's superclasses and interfaces
     */
-   void removeInstantiatedBean(Object bean);
+   void removeInstantiatedContext(ControllerContext context);
 
 }

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractInjectionValueMetaData.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractInjectionValueMetaData.java	2006-08-12 07:21:58 UTC (rev 55841)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractInjectionValueMetaData.java	2006-08-12 09:10:05 UTC (rev 55842)
@@ -26,6 +26,8 @@
 import org.jboss.beans.metadata.injection.InjectionMode;
 import org.jboss.beans.metadata.injection.InjectionType;
 import org.jboss.beans.metadata.spi.MetaDataVisitor;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
 import org.jboss.kernel.spi.dependency.KernelController;
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
 import org.jboss.reflect.spi.TypeInfo;
@@ -107,21 +109,30 @@
       if (value == null)
       {
          // what else to use here - if not info.getType?
-         Set beans = controller.getInstantiatedBeans(info.getType());
-         int numberOfMatchingBeans = beans.size();
-         if (numberOfMatchingBeans > 1)
+         Set<ControllerContext> contexts = controller.getInstantiatedContexts(info.getType());
+         int numberOfMatchingContexts = contexts.size();
+         if (numberOfMatchingContexts > 1)
          {
-            throw new Error("Should not be here, too many matching beans - dependency failed! " + this);
+            throw new Error("Should not be here, too many matching contexts - dependency failed! " + this);
          }
-         else if (numberOfMatchingBeans == 0)
+         else if (numberOfMatchingContexts == 0)
          {
             if (InjectionType.STRICT.equals(injectionType))
             {
-               throw new Error("Should not be here, no bean matches class type - dependency failed! " + this);
+               throw new Error("Should not be here, no context matches class type - dependency failed! " + this);
             }
+            // we are not 'strict' - can return null
             return null;
          }
-         return beans.iterator().next();
+         ControllerContext context = contexts.iterator().next();
+         // todo - should we do this?
+         ControllerState state = dependentState;
+         if (state == null)
+         {
+            state = ControllerState.INSTALLED;
+         }
+         controller.change(context, state);
+         return context.getTarget();
       }
       return super.getValue(info, cl);
    }

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java	2006-08-12 07:21:58 UTC (rev 55841)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java	2006-08-12 09:10:05 UTC (rev 55842)
@@ -40,7 +40,7 @@
 
 /**
  * ConfigureAction.
- * 
+ *
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @version $Revision$
  */
@@ -84,10 +84,10 @@
 
    /**
     * Set the attributes
-    * 
+    *
     * @param context the context
     * @param target the target
-    * @param joinPoints the attribute setter joinpoints 
+    * @param joinPoints the attribute setter joinpoints
     * @param ignoreErrors whether to ignore errors
     * @throws Throwable for any unignored error
     */
@@ -169,17 +169,20 @@
 
                if (InjectionMode.BY_TYPE.equals(injectionMode))
                {
-                  Set beans = controller.getInstantiatedBeans(pi.getType().getType());
-                  int numberOfMatchingBeans = beans.size();
+                  Set<ControllerContext> contexts = controller.getInstantiatedContexts(pi.getType().getType());
+                  int numberOfMatchingBeans = contexts.size();
                   if (numberOfMatchingBeans > 1)
                   {
-                     throw new Error("Should not be here, too many matching beans - dependency failed! " + pi);
+                     throw new Error("Should not be here, too many matching contexts - dependency failed! " + pi);
                   }
                   else if (numberOfMatchingBeans == 0 && InjectionType.STRICT.equals(injectionType))
                   {
-                     throw new Error("Should not be here, no bean matches class type - dependency failed! " + pi);
+                     throw new Error("Should not be here, no context matches class type - dependency failed! " + pi);
                   }
-                  result = beans.iterator().next();
+                  ControllerContext context = contexts.iterator().next();
+                  // todo - should we do this?
+                  controller.change(context, state);
+                  result = context.getTarget();
                }
                else if (InjectionMode.BY_NAME.equals(injectionMode))
                {

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/InstantiateAction.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/InstantiateAction.java	2006-08-12 07:21:58 UTC (rev 55841)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/InstantiateAction.java	2006-08-12 09:10:05 UTC (rev 55842)
@@ -71,7 +71,7 @@
             if (object instanceof KernelControllerContextAware)
                ((KernelControllerContextAware) object).setKernelControllerContext(context);
 
-//            controller.addInstantiatedBean(object);
+//            controller.addInstantiatedContext(context);
          }
       }
       catch (Throwable t)
@@ -92,7 +92,7 @@
                ((KernelControllerContextAware) object).unsetKernelControllerContext(context);
 
 //            KernelController controller = (KernelController) context.getController();
-//            controller.removeInstantiatedBean(object);
+//            controller.removeInstantiatedContext(context);
          }
 
       }




More information about the jboss-cvs-commits mailing list