[seam-commits] Seam SVN: r12591 - in modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces: util and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Apr 22 18:00:42 EDT 2010


Author: lincolnthree
Date: 2010-04-22 18:00:41 -0400 (Thu, 22 Apr 2010)
New Revision: 12591

Added:
   modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java
Modified:
   modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/environment/SeamApplicationWrapper.java
Log:


Modified: modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/environment/SeamApplicationWrapper.java
===================================================================
--- modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/environment/SeamApplicationWrapper.java	2010-04-22 21:59:00 UTC (rev 12590)
+++ modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/environment/SeamApplicationWrapper.java	2010-04-22 22:00:41 UTC (rev 12591)
@@ -22,13 +22,8 @@
 
 package org.jboss.seam.faces.environment;
 
-import java.util.Set;
-
 import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
 import javax.faces.FactoryFinder;
 import javax.faces.application.Application;
 import javax.faces.application.ApplicationFactory;
@@ -38,11 +33,11 @@
 import javax.faces.validator.Validator;
 import javax.inject.Inject;
 
-import org.jboss.seam.faces.event.PhaseEventBridge;
+import org.jboss.seam.faces.util.BeanManagerUtils;
 
 /**
- * Provides @{@link Inject} support for JSF artifacts such as {@link Converter},
- * {@link Validator}.
+ * Provides contextual lifecycle and @{link Inject} support for JSF artifacts
+ * such as {@link Converter}, {@link Validator}.
  * 
  * @author <a href="mailto:lincolnbaxter at gmail.com>Lincoln Baxter, III</a>
  * 
@@ -53,7 +48,7 @@
    private Application parent;
 
    @Inject
-   BeanManager manager;
+   BeanManagerUtils managerUtils;
 
    public void installWrapper(@Observes final PostConstructApplicationEvent event)
    {
@@ -65,67 +60,33 @@
    @Override
    public Converter createConverter(final Class<?> targetClass)
    {
-      Converter result = null;
-
-      Set<Bean<?>> beans = manager.getBeans(targetClass);
-      if (!beans.isEmpty())
+      Converter result = parent.createConverter(targetClass);
+      if (result != null)
       {
-         Bean<?> bean = beans.iterator().next();
-         CreationalContext<?> context = manager.createCreationalContext(bean);
-         result = (Converter) manager.getReference(bean, PhaseEventBridge.class, context);
+         result = managerUtils.getContextualInstance(result.getClass());
       }
-
-      if (result == null)
-      {
-         result = parent.createConverter(targetClass);
-      }
-
       return result;
    }
 
    @Override
    public Converter createConverter(final String converterId)
    {
-      /*
-       * We need to ask for an instance because we have no way of getting the
-       * type information by id
-       */
       Converter result = parent.createConverter(converterId);
       if (result != null)
       {
-         Class<? extends Converter> targetClass = result.getClass();
-         Set<Bean<?>> beans = manager.getBeans(targetClass);
-         if (!beans.isEmpty())
-         {
-            Bean<?> bean = beans.iterator().next();
-            CreationalContext<?> context = manager.createCreationalContext(bean);
-            result = (Converter) manager.getReference(bean, targetClass, context);
-         }
+         result = managerUtils.getContextualInstance(result.getClass());
       }
-
       return result;
    }
 
    @Override
    public Validator createValidator(final String validatorId)
    {
-      /*
-       * We need to ask for an instance because we have no way of getting the
-       * type information by id
-       */
       Validator result = parent.createValidator(validatorId);
       if (result != null)
       {
-         Class<? extends Validator> targetClass = result.getClass();
-         Set<Bean<?>> beans = manager.getBeans(targetClass);
-         if (!beans.isEmpty())
-         {
-            Bean<?> bean = beans.iterator().next();
-            CreationalContext<?> context = manager.createCreationalContext(bean);
-            result = (Validator) manager.getReference(bean, targetClass, context);
-         }
+         result = managerUtils.getContextualInstance(result.getClass());
       }
-
       return result;
    }
 

Added: modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java
===================================================================
--- modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java	                        (rev 0)
+++ modules/branches/lincolnthree/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java	2010-04-22 22:00:41 UTC (rev 12591)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * 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.seam.faces.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.inject.Inject;
+
+/**
+ * A utility providing common functions to simply use of {@link BeanManager}
+ * 
+ * @author <a href="mailto:lincolnbaxter at gmail.com>Lincoln Baxter, III</a>
+ * 
+ */
+public class BeanManagerUtils
+{
+   @Inject
+   private BeanManager manager;
+
+   /**
+    * Perform @{@link Inject} on an object as if it were a bean managed by CDI.
+    * 
+    * @param instance
+    */
+   @SuppressWarnings("unchecked")
+   public void injectNonContextualInstance(final Object instance)
+   {
+      CreationalContext<Object> creationalContext = manager.createCreationalContext(null);
+      InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) manager.createInjectionTarget(manager.createAnnotatedType(instance.getClass()));
+      injectionTarget.inject(instance, creationalContext);
+   }
+
+   /**
+    * Get a single CDI managed instance of a specific class. Return only the
+    * first result if multiple beans are available.
+    * 
+    * @param type The class for which to return an instance.
+    */
+   @SuppressWarnings("unchecked")
+   public <T> T getContextualInstance(final Class<T> type)
+   {
+      Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
+      CreationalContext<T> context = manager.createCreationalContext(bean);
+      T result = (T) manager.getReference(bean, type, context);
+      return result;
+   }
+
+   /**
+    * Get all CDI managed instances of a specific class. Return results in a
+    * {@link List} in no specific order.
+    * 
+    * @param type The class for which to return instances.
+    */
+   @SuppressWarnings("unchecked")
+   public <T> List<T> getContextualInstances(final Class<T> type)
+   {
+      List<T> result = new ArrayList<T>();
+      for (Bean<?> bean : manager.getBeans(type))
+      {
+         CreationalContext<T> context = (CreationalContext<T>) manager.createCreationalContext(bean);
+         result.add((T) manager.getReference(bean, type, context));
+      }
+      return result;
+   }
+}



More information about the seam-commits mailing list