[weld-commits] Weld SVN: r6745 - extensions/trunk/src/main/java/org/jboss/weld/extensions/util.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Thu Jul 22 04:58:29 EDT 2010


Author: swd847
Date: 2010-07-22 04:58:28 -0400 (Thu, 22 Jul 2010)
New Revision: 6745

Added:
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AmbiguousBeanException.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanNotFoundException.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolutionException.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolver.java
Modified:
   extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java
Log:
add utility class to resolve beans 



Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AmbiguousBeanException.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AmbiguousBeanException.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AmbiguousBeanException.java	2010-07-22 08:58:28 UTC (rev 6745)
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Set;
+
+import javax.enterprise.inject.spi.Bean;
+
+/**
+ * Exception thrown when more than 1 beans with the given type and qualifiers
+ * could be found
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class AmbiguousBeanException extends BeanResolutionException
+{
+
+   private static final long serialVersionUID = -6513493477765900752L;
+
+   public AmbiguousBeanException(Type type, Annotation[] qualifiers, Set<Bean<?>> beans)
+   {
+      super(type, qualifiers, "More than one bean found with type " + type + "and qualifiers. Bean found: " + beans);
+   }
+
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanNotFoundException.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanNotFoundException.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanNotFoundException.java	2010-07-22 08:58:28 UTC (rev 6745)
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+/**
+ * Exception thrown when no beans with the given type and qualifiers could be
+ * found
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class BeanNotFoundException extends BeanResolutionException
+{
+
+   private static final long serialVersionUID = -6513493477765900752L;
+
+   public BeanNotFoundException(Type type, Annotation[] qualifiers)
+   {
+      super(type, qualifiers, "No bean found with type " + type + "and qualifiers " + qualifiers);
+
+   }
+
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolutionException.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolutionException.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolutionException.java	2010-07-22 08:58:28 UTC (rev 6745)
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+/**
+ * Superclass for exceptions that result from bean lookup
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class BeanResolutionException extends Exception
+{
+   private static final long serialVersionUID = -4376259139316630962L;
+   private final Type type;
+   private final Annotation[] qualifiers;
+
+   public BeanResolutionException(Type type, Annotation[] qualifiers, String message)
+   {
+      this.type = type;
+      this.qualifiers = qualifiers;
+   }
+
+   public Type getType()
+   {
+      return type;
+   }
+
+   public Annotation[] getQualifiers()
+   {
+      return qualifiers;
+   }
+
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolver.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolver.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/BeanResolver.java	2010-07-22 08:58:28 UTC (rev 6745)
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+
+/**
+ * Utility class to resolve and acquire references to Beans
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class BeanResolver
+{
+   private BeanResolver()
+   {
+   }
+
+   /**
+    * Resolves a bean
+    * 
+    */
+   public static Bean<?> resolveBean(Type beanType, Annotation[] qualifiers, BeanManager manager) throws AmbiguousBeanException, BeanResolutionException, BeanNotFoundException
+   {
+      Set<Bean<?>> beans = manager.getBeans(beanType, qualifiers);
+      if (beans.size() == 0)
+      {
+         throw new BeanNotFoundException(beanType, qualifiers);
+      }
+      if (beans.size() != 1)
+      {
+         throw new AmbiguousBeanException(beanType, qualifiers, beans);
+      }
+      return beans.iterator().next();
+   }
+
+   /**
+    * gets a reference to a bean with the given type and qualifiers
+    */
+   public static Object getReference(Type beanType, Annotation[] qualifiers, BeanManager manager) throws AmbiguousBeanException, BeanResolutionException, BeanNotFoundException
+   {
+      Bean<?> bean = resolveBean(beanType, qualifiers, manager);
+      CreationalContext<?> context = manager.createCreationalContext(bean);
+      return manager.getReference(bean, beanType, context);
+   }
+
+   /**
+    * gets a reference to a bean with the given type and qualifiers
+    */
+   public static <T> T getReference(Class<T> beanType, Annotation[] qualifiers, BeanManager manager) throws AmbiguousBeanException, BeanResolutionException, BeanNotFoundException
+   {
+      return (T) getReference((Type) beanType, qualifiers, manager);
+   }
+}

Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java	2010-07-22 06:46:07 UTC (rev 6744)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java	2010-07-22 08:58:28 UTC (rev 6745)
@@ -227,4 +227,23 @@
       }
    }
 
+   public static Object invokeAndWrap(Method method, Object target, Object... args)
+   {
+      try
+      {
+         return method.invoke(target, args);
+      }
+      catch (Exception e)
+      {
+         if (e instanceof RuntimeException)
+         {
+            throw (RuntimeException) e;
+         }
+         else
+         {
+            throw new RuntimeException("exception invoking: " + method.getName(), e);
+         }
+      }
+   }
+
 }



More information about the weld-commits mailing list