[jboss-cvs] JBossAS SVN: r58861 - in trunk/ejb3/src/main/org/jboss/injection: . lang lang/reflect

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Dec 6 06:39:10 EST 2006


Author: wolfc
Date: 2006-12-06 06:38:46 -0500 (Wed, 06 Dec 2006)
New Revision: 58861

Added:
   trunk/ejb3/src/main/org/jboss/injection/AbstractPropertyInjector.java
   trunk/ejb3/src/main/org/jboss/injection/EJBContextPropertyInjector.java
   trunk/ejb3/src/main/org/jboss/injection/JndiPropertyInjector.java
   trunk/ejb3/src/main/org/jboss/injection/TimerServicePropertyInjector.java
   trunk/ejb3/src/main/org/jboss/injection/UserTransactionPropertyInjector.java
   trunk/ejb3/src/main/org/jboss/injection/lang/reflect/
   trunk/ejb3/src/main/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java
   trunk/ejb3/src/main/org/jboss/injection/lang/reflect/BeanProperty.java
   trunk/ejb3/src/main/org/jboss/injection/lang/reflect/FieldBeanProperty.java
   trunk/ejb3/src/main/org/jboss/injection/lang/reflect/MethodBeanProperty.java
Modified:
   trunk/ejb3/src/main/org/jboss/injection/EJBContextFieldInjector.java
   trunk/ejb3/src/main/org/jboss/injection/EJBContextMethodInjector.java
   trunk/ejb3/src/main/org/jboss/injection/JndiFieldInjector.java
   trunk/ejb3/src/main/org/jboss/injection/JndiMethodInjector.java
   trunk/ejb3/src/main/org/jboss/injection/ResourceHandler.java
   trunk/ejb3/src/main/org/jboss/injection/TimerServiceFieldInjector.java
   trunk/ejb3/src/main/org/jboss/injection/TimerServiceMethodInjector.java
   trunk/ejb3/src/main/org/jboss/injection/UserTransactionFieldInjector.java
   trunk/ejb3/src/main/org/jboss/injection/UserTransactionMethodInjector.java
Log:
refactored ResourceHandler to clean up duplicate code

Added: trunk/ejb3/src/main/org/jboss/injection/AbstractPropertyInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/AbstractPropertyInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/AbstractPropertyInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection;
+
+import org.jboss.ejb3.BeanContext;
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public abstract class AbstractPropertyInjector implements Injector
+{
+   protected BeanProperty property;
+   
+   protected AbstractPropertyInjector(BeanProperty property)
+   {
+      this.property = property;
+   }
+   
+   public Class<?> getInjectionClass()
+   {
+      return property.getType();
+   }
+
+   public void inject(BeanContext ctx)
+   {
+      inject(ctx.getInstance());
+   }
+}

Modified: trunk/ejb3/src/main/org/jboss/injection/EJBContextFieldInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/EJBContextFieldInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/EJBContextFieldInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -29,6 +29,7 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated use EJBContextPropertyInjector
  */
 public class EJBContextFieldInjector implements Injector, PojoInjector
 {

Modified: trunk/ejb3/src/main/org/jboss/injection/EJBContextMethodInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/EJBContextMethodInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/EJBContextMethodInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -30,6 +30,7 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated use EJBContextPropertyInjector
  */
 public class EJBContextMethodInjector implements Injector, PojoInjector
 {

Added: trunk/ejb3/src/main/org/jboss/injection/EJBContextPropertyInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/EJBContextPropertyInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/EJBContextPropertyInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,62 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection;
+
+import org.jboss.ejb3.BeanContext;
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Injects an ejb context into an ejb.
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: 1.1 $
+ */
+public class EJBContextPropertyInjector implements Injector, PojoInjector
+{
+   private BeanProperty property;
+
+   public EJBContextPropertyInjector(BeanProperty property)
+   {
+      this.property = property;
+   }
+
+   public void inject(BeanContext ctx)
+   {
+      inject(ctx, ctx.getInstance());
+   }
+   
+   public void inject(BeanContext ctx, Object instance)
+   {
+      property.set(instance, ctx.getEJBContext());
+   }
+
+   public void inject(Object instance)
+   {
+      throw new RuntimeException("Illegal operation");
+   }
+
+   public Class getInjectionClass()
+   {
+      return property.getType();
+   }
+}

Modified: trunk/ejb3/src/main/org/jboss/injection/JndiFieldInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/JndiFieldInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/JndiFieldInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -34,6 +34,7 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated  use JndiPropertyInjector
  */
 public class JndiFieldInjector implements Injector, PojoInjector
 {
@@ -97,7 +98,7 @@
    {
       
       Object dependency = lookup(jndiName, field.getType());
- //     log.info("!! inject jndiName " + jndiName + " " + field.getType() + " " + dependency);
+      log.trace("!! inject jndiName " + jndiName + " " + field.getType() + " " + dependency);
       
       try
       {

Modified: trunk/ejb3/src/main/org/jboss/injection/JndiMethodInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/JndiMethodInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/JndiMethodInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -33,9 +33,11 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated use JndiPropertyInjector
  */
 public class JndiMethodInjector implements Injector, PojoInjector
 {
+   @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(JndiMethodInjector.class);
    
    private Method setMethod;

Added: trunk/ejb3/src/main/org/jboss/injection/JndiPropertyInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/JndiPropertyInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/JndiPropertyInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import org.jboss.ejb3.BeanContext;
+import org.jboss.injection.lang.reflect.BeanProperty;
+import org.jboss.logging.Logger;
+
+/**
+ * Injects a jndi dependency into a bean property.
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class JndiPropertyInjector extends AbstractPropertyInjector implements PojoInjector
+{
+   @SuppressWarnings("unused")
+   private static final Logger log = Logger.getLogger(JndiPropertyInjector.class);
+   
+   private String jndiName;
+   private Context ctx;
+
+   public JndiPropertyInjector(BeanProperty property, String jndiName, Context ctx)
+   {
+      super(property);
+      this.jndiName = jndiName;
+      this.ctx = ctx;
+   }
+
+   public void inject(BeanContext bctx)
+   {
+      inject(bctx, bctx.getInstance());
+   }
+   
+   public Class<?> getInjectionClass()
+   {
+      return property.getType();
+   }
+   
+   protected Object lookup(String jndiName)
+   {
+      Object dependency = null;
+      
+      try
+      {
+         dependency = ctx.lookup(jndiName);
+      }
+      catch (NamingException e)
+      {
+         throw new RuntimeException("Unable to inject jndi dependency: " + jndiName + " into property " + property, e);
+      }
+      return dependency;
+   }
+   
+   public void inject(BeanContext bctx, Object instance)
+   {
+      inject(instance);
+   }
+
+   public void inject(Object instance)
+   {
+      Object value = lookup(jndiName);
+      property.set(instance, value);
+   }
+}

Modified: trunk/ejb3/src/main/org/jboss/injection/ResourceHandler.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/ResourceHandler.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/ResourceHandler.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -22,6 +22,9 @@
 package org.jboss.injection;
 
 import org.jboss.ejb3.Container;
+import org.jboss.injection.lang.reflect.BeanProperty;
+import org.jboss.injection.lang.reflect.FieldBeanProperty;
+import org.jboss.injection.lang.reflect.MethodBeanProperty;
 import org.jboss.logging.Logger;
 import org.jboss.metamodel.descriptor.EnvEntry;
 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
@@ -197,6 +200,10 @@
       Resource ref = method.getAnnotation(Resource.class);
       if (ref == null) return;
 
+      log.trace("method " + method + " has @Resource");
+      
+      handlePropertyAnnotation(ref, new MethodBeanProperty(method), container, injectors);
+      /*
       String encName = ref.name();
       if (encName == null || encName.equals(""))
       {
@@ -282,7 +289,9 @@
          }
          injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
       }
+      */
    }
+   
    public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
    {
       Resource ref = field.getAnnotation(Resource.class);
@@ -290,6 +299,8 @@
 
       log.trace("field " + field + " has @Resource");
       
+      handlePropertyAnnotation(ref, new FieldBeanProperty(field), container, injectors);
+      /*
       String encName = ref.name();
       if (encName == null || encName.equals(""))
       {
@@ -373,7 +384,106 @@
          }
          injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
       }
+      */
    }
 
+   private void handlePropertyAnnotation(Resource ref, BeanProperty property, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
+   {
+      assert ref != null;
+      assert property != null;
+      assert container != null;
+      assert injectors != null;
+      
+      String encName = ref.name();
+      if (encName == null || encName.equals(""))
+      {
+         //encName = InjectionUtil.getEncName(field);
+         encName = property.getDeclaringClass().getName() + "/" + property.getName();
+      }
+      else
+      {
+         encName = "env/" + encName;
+      }
 
+      AccessibleObject accObj = property.getAccessibleObject();
+      
+      Class<?> type = property.getType();
+      if (!ref.type().equals(Object.class))
+      {
+         type = ref.type();
+      }
+      if (type.equals(UserTransaction.class))
+      {
+         injectors.put(accObj, new UserTransactionPropertyInjector(property, container));
+      }
+      else if (type.equals(TimerService.class))
+      {
+         injectors.put(accObj, new TimerServicePropertyInjector(property, (Container) container)); // only EJBs
+      }
+      else if (EJBContext.class.isAssignableFrom(type))
+      {
+         injectors.put(accObj, new EJBContextPropertyInjector(property));
+      }
+      else if (type.equals(WebServiceContext.class))
+      {
+         // FIXME: For now we skip it, and rely on the WS stack to perform the injection
+      }
+      else if (type.equals(String.class)
+              || type.equals(Character.class)
+              || type.equals(Byte.class)
+              || type.equals(Short.class)
+              || type.equals(Integer.class)
+              || type.equals(Long.class)
+              || type.equals(Boolean.class)
+              || type.equals(Double.class)
+              || type.equals(Float.class)
+              || type.isPrimitive()
+              )
+      {
+         // don't add an injector if no XML <env-entry is present as there will be no value to inject
+         if (container.getEncInjectors().containsKey(encName))
+         {
+            injectors.put(accObj, new JndiPropertyInjector(property, encName, container.getEnc()));
+         }
+         else if (ref.mappedName() != null && ref.mappedName().length() > 0)
+         {
+            // Use the mappedName as the string value
+            String s = ref.mappedName().trim();
+            try
+            {
+               Object value = ValueConvertor.convertValue(type, s);
+               container.getEncInjectors().put(encName, new ValueEncInjector(encName, value, "@Resource"));
+               injectors.put(accObj, new JndiPropertyInjector(property, encName, container.getEnc()));
+            }
+            catch(Throwable t)
+            {
+               throw new RuntimeException("Failed to convert: "+ref.mappedName()+" to type:"+type, t);
+            }
+         }
+         else
+         {
+            log.warn("Not injecting " + property.getName() + ", no matching enc injector " + encName + " found");
+         }
+      }
+      else
+      {
+         if (!container.getEncInjectors().containsKey(encName))
+         {
+            String mappedName = ref.mappedName();
+            if (mappedName == null || mappedName.equals(""))
+            {
+               // TODO: is this a nice trick?
+//               if(ConnectionFactory.class.isAssignableFrom(type))
+//               {
+//                  // neat little trick
+//                  mappedName = "java:/ConnectionFactory";
+//               }
+//               else
+                  throw new RuntimeException("You did not specify a @Resource.mappedName() on " + accObj + " and there is no binding for enc name " + encName + " in XML");
+            }
+            container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.mappedName(), "@Resource"));
+         }
+         injectors.put(accObj, new JndiPropertyInjector(property, encName, container.getEnc()));
+      }      
+   }
 }

Modified: trunk/ejb3/src/main/org/jboss/injection/TimerServiceFieldInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/TimerServiceFieldInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/TimerServiceFieldInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -30,6 +30,7 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated use TimerServicePropertyInjector
  */
 public class TimerServiceFieldInjector implements Injector
 {

Modified: trunk/ejb3/src/main/org/jboss/injection/TimerServiceMethodInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/TimerServiceMethodInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/TimerServiceMethodInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -31,6 +31,7 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated use TimerServicePropertyInjector
  */
 public class TimerServiceMethodInjector implements Injector
 {

Added: trunk/ejb3/src/main/org/jboss/injection/TimerServicePropertyInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/TimerServicePropertyInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/TimerServicePropertyInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection;
+
+import org.jboss.ejb3.BeanContext;
+import org.jboss.ejb3.Container;
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Injects a timer service into an ejb.
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: 1.1 $
+ */
+public class TimerServicePropertyInjector implements Injector
+{
+   private BeanProperty property;
+   private Container container;
+
+   public TimerServicePropertyInjector(BeanProperty property, Container container)
+   {
+      this.property = property;
+      this.container = container;
+   }
+
+   public void inject(Object instance)
+   {
+      throw new RuntimeException("Illegal operation");
+   }
+
+   public void inject(BeanContext ctx)
+   {
+      property.set(ctx.getInstance(), container.getTimerService());
+   }
+   
+   public Class getInjectionClass()
+   {
+      return property.getType();
+   }
+}

Modified: trunk/ejb3/src/main/org/jboss/injection/UserTransactionFieldInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/UserTransactionFieldInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/UserTransactionFieldInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -35,6 +35,7 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated use UserTransactionPropertyInjector
  */
 public class UserTransactionFieldInjector implements Injector
 {

Modified: trunk/ejb3/src/main/org/jboss/injection/UserTransactionMethodInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/UserTransactionMethodInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/UserTransactionMethodInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -37,6 +37,7 @@
  *
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
+ * @deprecated use UserTransactionPropertyInjector
  */
 public class UserTransactionMethodInjector implements Injector
 {

Added: trunk/ejb3/src/main/org/jboss/injection/UserTransactionPropertyInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/UserTransactionPropertyInjector.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/UserTransactionPropertyInjector.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,59 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection;
+
+import javax.ejb.TransactionManagementType;
+import javax.transaction.UserTransaction;
+
+import org.jboss.aop.Advisor;
+import org.jboss.ejb3.Container;
+import org.jboss.ejb3.tx.TxUtil;
+import org.jboss.ejb3.tx.UserTransactionImpl;
+import org.jboss.injection.lang.reflect.BeanProperty;
+
+/**
+ * Injects a user transaction into a bean property.
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class UserTransactionPropertyInjector extends AbstractPropertyInjector
+{
+   public UserTransactionPropertyInjector(BeanProperty property, InjectionContainer container)
+   {
+      super(property);
+      
+      if (container instanceof Container)
+      {
+         TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) container));
+         if (type != TransactionManagementType.BEAN)
+            throw new IllegalStateException("Container " + ((Container) container).getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean");
+      }
+   }
+
+   public void inject(Object instance)
+   {
+      UserTransaction ut = new UserTransactionImpl();
+      property.set(instance, ut);
+   }
+}

Added: trunk/ejb3/src/main/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/lang/reflect/AbstractAccessibleObjectBeanProperty.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,47 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection.lang.reflect;
+
+import java.lang.reflect.AccessibleObject;
+
+/**
+ * Makes sure that the accessible object is accessible.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public abstract class AbstractAccessibleObjectBeanProperty<T extends AccessibleObject>
+   implements BeanProperty
+{
+   private T obj;
+   
+   protected AbstractAccessibleObjectBeanProperty(T obj)
+   {
+      this.obj = obj;
+      obj.setAccessible(true);
+   }
+   
+   public T getAccessibleObject()
+   {
+      return obj;
+   }
+}

Added: trunk/ejb3/src/main/org/jboss/injection/lang/reflect/BeanProperty.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/lang/reflect/BeanProperty.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/lang/reflect/BeanProperty.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,44 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection.lang.reflect;
+
+import java.lang.reflect.AccessibleObject;
+
+/**
+ * A bean property defines a propery of a pojo from the EJB3 point of view.
+ * It can be used to inject values into it.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface BeanProperty
+{
+   AccessibleObject getAccessibleObject();
+   
+   Class<?> getDeclaringClass();
+   
+   String getName();
+   
+   Class<?> getType();
+   
+   void set(Object instance, Object value);
+}

Added: trunk/ejb3/src/main/org/jboss/injection/lang/reflect/FieldBeanProperty.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/lang/reflect/FieldBeanProperty.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/lang/reflect/FieldBeanProperty.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection.lang.reflect;
+
+import java.lang.reflect.Field;
+
+import org.jboss.logging.Logger;
+
+/**
+ * Morphes a field into a bean property.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class FieldBeanProperty extends AbstractAccessibleObjectBeanProperty<Field>
+{
+   private static final Logger log = Logger.getLogger(FieldBeanProperty.class);
+   
+   /**
+    * @param field
+    */
+   public FieldBeanProperty(Field field)
+   {
+      super(field);
+   }
+   
+   public Class<?> getDeclaringClass()
+   {
+      return getField().getDeclaringClass();
+   }
+   
+   protected Field getField()
+   {
+      return getAccessibleObject();
+   }
+   
+   public String getName()
+   {
+      return getField().getName();
+   }
+   
+   public Class<?> getType()
+   {
+      return getField().getType();
+   }
+   
+   /* (non-Javadoc)
+    * @see org.jboss.injection.lang.reflect.BeanProperty#set(java.lang.Object)
+    */
+   public void set(Object instance, Object value)
+   {
+      Field field = getField();
+      try
+      {
+         field.set(instance, value);
+      }
+      catch(IllegalAccessException e)
+      {
+         log.fatal("illegal access on field " + field, e);
+         throw new RuntimeException(e);
+      }
+      catch(IllegalArgumentException e)
+      {
+         String msg = "failed to set value " + value + " on field " + field;
+         log.error(msg, e);
+         throw new IllegalArgumentException(msg);
+      }
+   }
+}

Added: trunk/ejb3/src/main/org/jboss/injection/lang/reflect/MethodBeanProperty.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/lang/reflect/MethodBeanProperty.java	2006-12-06 11:38:41 UTC (rev 58860)
+++ trunk/ejb3/src/main/org/jboss/injection/lang/reflect/MethodBeanProperty.java	2006-12-06 11:38:46 UTC (rev 58861)
@@ -0,0 +1,119 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, 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.injection.lang.reflect;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.jboss.logging.Logger;
+
+/**
+ * Morphs a setter method into a bean property.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MethodBeanProperty extends AbstractAccessibleObjectBeanProperty<Method>
+{
+   private final Logger log = Logger.getLogger(MethodBeanProperty.class);
+   
+   private String name = null; // lazily initialized
+   
+   /**
+    * @param method
+    */
+   public MethodBeanProperty(Method method)
+   {
+      super(method);
+      
+      assert method.getReturnType() == Void.TYPE;
+      assert method.getParameterTypes().length == 1;
+      assert method.getName().startsWith("set");
+   }
+
+   public Class<?> getDeclaringClass()
+   {
+      return getMethod().getDeclaringClass();
+   }
+   
+   public String getName()
+   {
+      if(name == null)
+      {
+         String name = getMethod().getName().substring(3);
+         if (name.length() > 1)
+         {
+            name = name.substring(0, 1).toLowerCase() + name.substring(1);
+         }
+         else
+         {
+            name = name.toLowerCase();
+         }
+         this.name = name; // atomair, no synch
+      }
+      return name;
+   }
+   
+   protected Method getMethod()
+   {
+      return getAccessibleObject();
+   }
+   
+   public Class<?> getType()
+   {
+      return getMethod().getParameterTypes()[0];
+   }
+   
+   /* (non-Javadoc)
+    * @see org.jboss.injection.lang.reflect.BeanProperty#set(java.lang.Object, java.lang.Object)
+    */
+   public void set(Object instance, Object value)
+   {
+      Method method = getMethod();
+      Object args[] = { value };
+      try
+      {
+         method.invoke(instance, args);
+      }
+      catch (IllegalAccessException e)
+      {
+         log.fatal("illegal access on method " + method, e);
+         throw new RuntimeException(e);
+      }
+      catch (IllegalArgumentException e)
+      {
+         String msg = "failed to set value " + value + " with setter " + method;
+         log.error(msg, e);
+         throw new IllegalArgumentException(msg);
+      }
+      catch (InvocationTargetException e)
+      {
+         Throwable cause = e.getCause();
+         if(cause instanceof Error)
+            throw (Error) cause;
+         if(cause instanceof RuntimeException)
+            throw (RuntimeException) cause;
+         throw new RuntimeException(cause);
+      }
+   }
+
+}




More information about the jboss-cvs-commits mailing list