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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Aug 12 11:41:38 EDT 2006


Author: alesj
Date: 2006-08-12 11:41:32 -0400 (Sat, 12 Aug 2006)
New Revision: 55843

Added:
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/injection/
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/injection/InjectionUtil.java
Modified:
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractInjectionValueMetaData.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/annotations/Inject.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java
Log:
Added InjectionUtil - no duplicated code.

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 09:10:05 UTC (rev 55842)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractInjectionValueMetaData.java	2006-08-12 15:41:32 UTC (rev 55843)
@@ -30,6 +30,7 @@
 import org.jboss.dependency.spi.ControllerState;
 import org.jboss.kernel.spi.dependency.KernelController;
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.kernel.plugins.injection.InjectionUtil;
 import org.jboss.reflect.spi.TypeInfo;
 import org.jboss.util.JBossStringBuilder;
 
@@ -108,31 +109,20 @@
    {
       if (value == null)
       {
-         // what else to use here - if not info.getType?
-         Set<ControllerContext> contexts = controller.getInstantiatedContexts(info.getType());
-         int numberOfMatchingContexts = contexts.size();
-         if (numberOfMatchingContexts > 1)
-         {
-            throw new Error("Should not be here, too many matching contexts - dependency failed! " + this);
-         }
-         else if (numberOfMatchingContexts == 0)
-         {
-            if (InjectionType.STRICT.equals(injectionType))
-            {
-               throw new Error("Should not be here, no context matches class type - dependency failed! " + this);
-            }
-            // we are not 'strict' - can return null
-            return null;
-         }
-         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();
+         // what else to use here - if not info.getType?
+         return InjectionUtil.resolveInjection(
+               controller,
+               info.getType(),
+               propertyMetaData.getName(), // should not be used - value set before, see visit(MDV visitor)
+               state,
+               getInjectionMode(),
+               getInjectionType(),
+               this);
       }
       return super.getValue(info, cl);
    }

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/annotations/Inject.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/annotations/Inject.java	2006-08-12 09:10:05 UTC (rev 55842)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/annotations/Inject.java	2006-08-12 15:41:32 UTC (rev 55843)
@@ -27,6 +27,10 @@
 import java.lang.annotation.RetentionPolicy;
 
 /**
+ * Beans when injected by class type are by default changed to configured
+ * state - if not yet configured.
+ * You can change this behavior by setting state.
+ *
  * @author <a href="mailto:ales.justin at genera-lynx.com">Ales Justin</a>
  */
 @Retention(RetentionPolicy.RUNTIME)

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 09:10:05 UTC (rev 55842)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java	2006-08-12 15:41:32 UTC (rev 55843)
@@ -31,6 +31,7 @@
 import org.jboss.beans.metadata.injection.InjectionType;
 import org.jboss.joinpoint.spi.TargettedJoinpoint;
 import org.jboss.kernel.Kernel;
+import org.jboss.kernel.plugins.injection.InjectionUtil;
 import org.jboss.kernel.spi.config.KernelConfigurator;
 import org.jboss.kernel.spi.dependency.KernelController;
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
@@ -166,36 +167,15 @@
                {
                   log.warn("Ignoring property - contextual injection: " + pi);
                }
-
-               if (InjectionMode.BY_TYPE.equals(injectionMode))
-               {
-                  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 contexts - dependency failed! " + pi);
-                  }
-                  else if (numberOfMatchingBeans == 0 && InjectionType.STRICT.equals(injectionType))
-                  {
-                     throw new Error("Should not be here, no context matches class type - dependency failed! " + pi);
-                  }
-                  ControllerContext context = contexts.iterator().next();
-                  // todo - should we do this?
-                  controller.change(context, state);
-                  result = context.getTarget();
-               }
-               else if (InjectionMode.BY_NAME.equals(injectionMode))
-               {
-                  ControllerContext context = controller.getContext(pi.getName(), state);
-                  if (context != null)
-                  {
-                     result = context.getTarget();
-                  }
-               }
-               else
-               {
-                  throw new IllegalArgumentException("Illegal injection mode: " + injectionMode);
-               }
+               result = InjectionUtil.resolveInjection(
+                     controller,
+                     pi.getType().getType(),
+                     pi.getName(),
+                     state,
+                     injectionMode,
+                     injectionType,
+                     pi
+               );
             }
             setter.invoke(target, new Object[]{result});
          }

Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/injection/InjectionUtil.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/injection/InjectionUtil.java	2006-08-12 09:10:05 UTC (rev 55842)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/injection/InjectionUtil.java	2006-08-12 15:41:32 UTC (rev 55843)
@@ -0,0 +1,100 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.kernel.plugins.injection;
+
+import org.jboss.beans.metadata.injection.InjectionMode;
+import org.jboss.beans.metadata.injection.InjectionType;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.spi.dependency.KernelController;
+
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:ales.justin at gmail.com">Ales Justin</a>
+ */
+public final class InjectionUtil
+{
+   // static utl class
+   private InjectionUtil()
+   {
+   }
+
+   /**
+    * Resolves injection bean.
+    *
+    * @param controller KernelController
+    * @param targetClass class to which we match other contexts
+    * @param propertyName used in ConfigureAction
+    * @param state
+    * @param mode
+    * @param type
+    * @param messageObject object that gives us the most info about exception
+    * @return null if type InjectionType.LOOSE, else found bean or throws throwable
+    * @throws Throwable
+    */
+   public static Object resolveInjection(KernelController controller,
+                                  Class targetClass,
+                                  String propertyName,
+                                  ControllerState state,
+                                  InjectionMode mode,
+                                  InjectionType type,
+                                  Object messageObject) throws Throwable
+   {
+      Object result = null;
+      if (InjectionMode.BY_TYPE.equals(mode))
+      {
+         Set<ControllerContext> contexts = controller.getInstantiatedContexts(targetClass);
+         int numberOfMatchingBeans = contexts.size();
+         if (numberOfMatchingBeans > 1)
+         {
+            throw new Error("Should not be here, too many matching contexts - dependency failed! " + messageObject);
+         }
+         else if (numberOfMatchingBeans == 1)
+         {
+            ControllerContext context = contexts.iterator().next();
+            // todo - should we do this?
+            controller.change(context, state);
+            result = context.getTarget();
+         }
+      }
+      else if (InjectionMode.BY_NAME.equals(mode))
+      {
+         ControllerContext context = controller.getContext(propertyName, state);
+         if (context != null)
+         {
+            result = context.getTarget();
+         }
+      }
+      else
+      {
+         throw new IllegalArgumentException("Illegal injection mode: " + mode);
+      }
+      // check strict mode
+      if (InjectionType.STRICT.equals(type) && result == null)
+      {
+         throw new Error("Should not be here, no context matches restrictions - dependency failed! " + messageObject);
+      }
+      return result;
+   }
+
+}




More information about the jboss-cvs-commits mailing list