You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ar...@apache.org on 2013/04/17 08:06:05 UTC

svn commit: r1468758 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/test/mock/

Author: arne
Date: Wed Apr 17 06:06:04 2013
New Revision: 1468758

URL: http://svn.apache.org/r1468758
Log:
OWB-843: Introduced AbstractBeanManager with utility methods

Added:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/AbstractBeanManager.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectableBeanManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java

Added: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/AbstractBeanManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/AbstractBeanManager.java?rev=1468758&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/AbstractBeanManager.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/AbstractBeanManager.java Wed Apr 17 06:06:04 2013
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.webbeans.container;
+
+import java.lang.annotation.Annotation;
+import javax.enterprise.inject.spi.BeanManager;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.util.AnnotationUtil;
+
+public abstract class AbstractBeanManager implements BeanManager
+{
+
+    protected abstract WebBeansContext getWebBeansContext();
+    
+    public boolean areInterceptorBindingsEquivalent(Annotation annotation1, Annotation annotation2)
+    {
+        return AnnotationUtil.isCdiAnnotationEqual(annotation1, annotation2);
+    }
+
+    public boolean areQualifiersEquivalent(Annotation annotation1, Annotation annotation2)
+    {
+        return AnnotationUtil.isCdiAnnotationEqual(annotation1, annotation2);
+    }
+
+    public int getInterceptorBindingHashCode(Annotation annotation)
+    {
+        return AnnotationUtil.getCdiAnnotationHashCode(annotation);
+    }
+
+    public int getQualifierHashCode(Annotation annotation)
+    {
+        return AnnotationUtil.getCdiAnnotationHashCode(annotation);
+    }
+}

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java?rev=1468758&r1=1468757&r2=1468758&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java Wed Apr 17 06:06:04 2013
@@ -45,8 +45,6 @@ import javax.enterprise.context.spi.Cont
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.Default;
 import javax.enterprise.inject.Stereotype;
-import javax.enterprise.inject.spi.AnnotatedField;
-import javax.enterprise.inject.spi.AnnotatedMember;
 import javax.enterprise.inject.spi.AnnotatedMethod;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.Bean;
@@ -108,7 +106,7 @@ import org.apache.webbeans.xml.WebBeansX
  * @see BeanManager 
  */
 @SuppressWarnings("unchecked")
-public class BeanManagerImpl implements BeanManager, Referenceable
+public class BeanManagerImpl extends AbstractBeanManager implements BeanManager, Referenceable
 {
     private static final long serialVersionUID = 2L;
 
@@ -147,13 +145,19 @@ public class BeanManagerImpl implements 
     /**XML configurator instance*/
     private WebBeansXMLConfigurator xmlConfigurator = null;
     
-
     /**
      * This list contains additional qualifiers which got set via the
      * {@link javax.enterprise.inject.spi.BeforeBeanDiscovery#addQualifier(Class)}
      * event function.
      */
     private List<Class<? extends Annotation>> additionalQualifiers = new ArrayList<Class<? extends Annotation>>();
+
+    /**
+     * This list contains additional interceptor bindings which got set via the
+     * {@link javax.enterprise.inject.spi.BeforeBeanDiscovery#addInterceptorBinding(Class)}
+     * event function.
+     */
+    private List<Class<? extends Annotation>> additionalInterceptorBindings = new ArrayList<Class<? extends Annotation>>();
     
     /**
      * This list contains additional scopes which got set via the 
@@ -535,21 +539,21 @@ public class BeanManagerImpl implements 
     /**
      * {@inheritDoc}
      */
-    public <T> BeanAttributesImpl<T> createBeanAttributes(AnnotatedMember<T> member)
-    {
-        if (member instanceof AnnotatedField)
-        {
-            return BeanAttributesBuilder.forContext(webBeansContext).newBeanAttibutes((AnnotatedField<T>)member).build();
-        }
-        else if (member instanceof AnnotatedMethod)
-        {
-            return BeanAttributesBuilder.forContext(webBeansContext).newBeanAttibutes((AnnotatedMethod<T>)member).build();
-        }
-        else
-        {
-            throw new IllegalArgumentException("Unsupported member type " + member.getClass().getName());
-        }
-    }
+//    public <T> BeanAttributes<T> createBeanAttributes(AnnotatedMember<?> member)
+//    {
+//        if (member instanceof AnnotatedField)
+//        {
+//            return BeanAttributesBuilder.forContext(webBeansContext).newBeanAttibutes((AnnotatedField<T>)member).build();
+//        }
+//        else if (member instanceof AnnotatedMethod)
+//        {
+//            return BeanAttributesBuilder.forContext(webBeansContext).newBeanAttibutes((AnnotatedMethod<T>)member).build();
+//        }
+//        else
+//        {
+//            throw new IllegalArgumentException("Unsupported member type " + member.getClass().getName());
+//        }
+//    }
 
     /**
      * {@inheritDoc}
@@ -988,6 +992,14 @@ public class BeanManagerImpl implements 
         }
     }
 
+    public void addAdditionalInterceptorBindings(Class<? extends Annotation> interceptorBinding)
+    {
+        if (!additionalInterceptorBindings.contains(interceptorBinding))
+        {
+            additionalInterceptorBindings.add(interceptorBinding);
+        }
+    }
+
     public void addAdditionalAnnotatedType(AnnotatedType<?> annotatedType)
     {
         webBeansContext.getAnnotatedElementFactory().setAnnotatedType(annotatedType);
@@ -1062,5 +1074,4 @@ public class BeanManagerImpl implements 
     {
         return inUse;
     }
-
 }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectableBeanManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectableBeanManager.java?rev=1468758&r1=1468757&r2=1468758&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectableBeanManager.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectableBeanManager.java Wed Apr 17 06:06:04 2013
@@ -18,11 +18,11 @@
  */
 package org.apache.webbeans.container;
 
-import java.io.Serializable;
 import java.io.Externalizable;
+import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import java.io.IOException;
+import java.io.Serializable;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.List;
@@ -43,6 +43,7 @@ import javax.enterprise.inject.spi.Inter
 import javax.enterprise.inject.spi.Interceptor;
 import javax.enterprise.inject.spi.ObserverMethod;
 
+import org.apache.webbeans.component.spi.BeanAttributes;
 import org.apache.webbeans.config.WebBeansContext;
 
 /**
@@ -55,24 +56,29 @@ import org.apache.webbeans.config.WebBea
  * This class is Serializable and always resolves the current
  * instance of the central BeanManager automatically.
  */
-public class InjectableBeanManager implements BeanManager, Serializable, Externalizable 
+public class InjectableBeanManager extends AbstractBeanManager implements BeanManager, Serializable, Externalizable 
 {
 
     private static final long serialVersionUID = 1L;
     
-    private transient BeanManager bm;
+    private transient BeanManagerImpl bm;
 
     /**
      * Used by serialization.
      */
     public InjectableBeanManager()
     {
-        bm = WebBeansContext.getInstance().getBeanManagerImpl();
+        this(WebBeansContext.getInstance().getBeanManagerImpl());
+    }
+
+    public InjectableBeanManager(BeanManagerImpl beanManager)
+    {
+        this.bm = beanManager;
     }
 
-    public InjectableBeanManager(BeanManager bm)
+    public WebBeansContext getWebBeansContext()
     {
-        this.bm = bm;
+        return bm.getWebBeansContext();
     }
 
     public <T> AnnotatedType<T> createAnnotatedType(Class<T> type)
@@ -200,6 +206,11 @@ public class InjectableBeanManager imple
         return bm.wrapExpressionFactory(expressionFactory);
     }
 
+    public <X> BeanAttributes<X> createBeanAttributes(AnnotatedType<X> annotatedType)
+    {
+        return bm.createBeanAttributes(annotatedType);
+    }
+
     public void writeExternal(ObjectOutput out) throws IOException 
     {    
     }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java?rev=1468758&r1=1468757&r2=1468758&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java Wed Apr 17 06:06:04 2013
@@ -310,6 +310,44 @@ public final class AnnotationUtil
     }
 
     /**
+     * Computes a hash code for the specified cdi annoation. cdi annotations may either be qualifiers or interceptor bindings.
+     *
+     * The hash code of CDI annotations consists of the hash code of the annotationType and all its
+     * method values, except those annotated with @Nonbinding.
+     *
+     * @param annotation
+     * @return 
+     */
+    public static int getCdiAnnotationHashCode(Annotation annotation)
+    {
+        Asserts.assertNotNull(annotation, "annotation argument can not be null");
+
+        int hashCode = 0;
+
+        Class<? extends Annotation> qualifierAnnotationType = annotation.annotationType();
+        if (qualifierAnnotationType != null)
+        {
+            hashCode = qualifierAnnotationType.hashCode();
+        }
+
+        // check the values of all qualifier-methods
+        // except those annotated with @Nonbinding
+        List<Method> bindingCdiAnnotationMethods
+                = getBindingCdiAnnotationMethods(qualifierAnnotationType);
+
+        for (Method method : bindingCdiAnnotationMethods)
+        {
+            Object value = callMethod(annotation, method);
+            if (value != null)
+            {
+                hashCode ^= value.hashCode();
+            }
+        }
+
+        return hashCode;
+    }
+
+    /**
      * Quecks if the two values are equal.
      *
      * @param value1

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java?rev=1468758&r1=1468757&r2=1468758&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/mock/MockManager.java Wed Apr 17 06:06:04 2013
@@ -42,12 +42,15 @@ import javax.enterprise.inject.spi.Obser
 import javax.enterprise.util.TypeLiteral;
 
 import org.apache.webbeans.component.AbstractOwbBean;
+import org.apache.webbeans.component.BeanAttributesImpl;
+import org.apache.webbeans.component.creation.BeanAttributesBuilder;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.config.WebBeansFinder;
+import org.apache.webbeans.container.AbstractBeanManager;
 import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.util.WebBeansUtil;
 
-public class MockManager implements BeanManager
+public class MockManager extends AbstractBeanManager implements BeanManager
 {
     private BeanManagerImpl manager = null;
 
@@ -61,6 +64,10 @@ public class MockManager implements Bean
         manager.addBean(webBeansContext.getWebBeansUtil().getManagerBean());
     }
 
+    public WebBeansContext getWebBeansContext()
+    {
+        return manager.getWebBeansContext();
+    }
 
     public void clear()
     {
@@ -172,6 +179,11 @@ public class MockManager implements Bean
         return this.manager.createCreationalContext(contextual);
     }
 
+    public <T> BeanAttributesImpl<T> createBeanAttributes(AnnotatedType<T> type)
+    {
+        return BeanAttributesBuilder.forContext(manager.getWebBeansContext()).newBeanAttibutes(type).build();
+    }
+
     @Override
     public Set<Bean<?>> getBeans(Type beanType, Annotation... bindings)
     {