[seam-commits] Seam SVN: r12572 - modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/util.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Apr 22 13:43:55 EDT 2010


Author: lincolnthree
Date: 2010-04-22 13:43:54 -0400 (Thu, 22 Apr 2010)
New Revision: 12572

Added:
   modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java
Log:
* A utility for bean manager operations, must be @Injected

Added: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/util/BeanManagerUtils.java	2010-04-22 17:43:54 UTC (rev 12572)
@@ -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