You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by st...@apache.org on 2011/05/18 21:29:07 UTC

svn commit: r1124392 - in /myfaces/extensions/cdi/trunk: ./ core/api/ core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/ core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/ core/api/src/test/resourc...

Author: struberg
Date: Wed May 18 19:29:06 2011
New Revision: 1124392

URL: http://svn.apache.org/viewvc?rev=1124392&view=rev
Log:
EXTCDI-188 add methods for direct contextual instance resolvement

I also upgrade testng to the latest 5.1 version

Added:
    myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/
    myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/BeanManagerProviderTest.java   (with props)
    myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/TestBean.java   (with props)
    myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/beans.xml   (with props)
    myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Modified:
    myfaces/extensions/cdi/trunk/core/api/pom.xml
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/BeanManagerProvider.java
    myfaces/extensions/cdi/trunk/pom.xml

Modified: myfaces/extensions/cdi/trunk/core/api/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/pom.xml?rev=1124392&r1=1124391&r2=1124392&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/pom.xml (original)
+++ myfaces/extensions/cdi/trunk/core/api/pom.xml Wed May 18 19:29:06 2011
@@ -19,12 +19,6 @@
 -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     <modelVersion>4.0.0</modelVersion>
-    <packaging>jar</packaging>
-
-    <groupId>org.apache.myfaces.extensions.cdi.core</groupId>
-    <artifactId>myfaces-extcdi-core-api</artifactId>
-
-    <name>MyFaces Extensions-CDI Core-API</name>
 
     <parent>
         <groupId>org.apache.myfaces.extensions.cdi.core</groupId>
@@ -32,6 +26,40 @@
         <version>1.0.0-SNAPSHOT</version>
     </parent>
 
+    <groupId>org.apache.myfaces.extensions.cdi.core</groupId>
+    <artifactId>myfaces-extcdi-core-api</artifactId>
+
+    <name>MyFaces Extensions-CDI Core-API</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.openwebbeans.test</groupId>
+            <artifactId>cditest</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.openwebbeans.test</groupId>
+            <artifactId>cditest-owb</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <!--
+            We currently need this for the CDI test container to emulate the @SessionScoped.
+            TODO: Finally this should not be necessary anymore.
+        -->
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-servlet_2.5_spec</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-el_2.2_spec</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+    </dependencies>
+
     <build>
         <plugins>
             <plugin>

Modified: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/BeanManagerProvider.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/BeanManagerProvider.java?rev=1124392&r1=1124391&r2=1124392&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/BeanManagerProvider.java (original)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/provider/BeanManagerProvider.java Wed May 18 19:29:06 2011
@@ -21,13 +21,18 @@ package org.apache.myfaces.extensions.cd
 import org.apache.myfaces.extensions.cdi.core.api.startup.CodiStartupBroadcaster;
 import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
 
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.Extension;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -105,6 +110,39 @@ public class BeanManagerProvider impleme
     }
 
     /**
+     * Get a Contextual Reference by it's type and annotation.
+     * You can use this method
+     *
+     * @param type the java type it represents. E.g. 'MailService.class'
+     * @param qualifiers additional qualifiers which further distinct the resolved bean
+     * @return the resolved Contextual Reference
+     */
+    public Object getContextualReference(Type type, Annotation... qualifiers)
+    {
+        BeanManager bm = getBeanManager();
+        Set<Bean<?>> beans = bm.getBeans(type, qualifiers);
+        Bean<?> bean = bm.resolve(beans);
+        CreationalContext<?> cc = bm.createCreationalContext(bean);
+        return bm.getReference(bean, type, cc);
+    }
+
+    /**
+     * Get a Contextual Reference by it's EL Name.
+     * This only works for beans with the &#064;Named annotation.
+     *
+     * @param name the EL name of the bean
+     * @return the resolved Contextual Reference
+     */
+    public Object getContextualReference(String name)
+    {
+        BeanManager bm = getBeanManager();
+        Set<Bean<?>> beans = bm.getBeans(name);
+        Bean<?> bean = bm.resolve(beans);
+        CreationalContext<?> cc = bm.createCreationalContext(bean);
+        return bm.getReference(bean, Object.class, cc);
+    }
+
+    /**
      * Get the BeanManager from the JNDI registry.
      *
      * Workaround for jboss 6 (EXTCDI-74)

Added: myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/BeanManagerProviderTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/BeanManagerProviderTest.java?rev=1124392&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/BeanManagerProviderTest.java (added)
+++ myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/BeanManagerProviderTest.java Wed May 18 19:29:06 2011
@@ -0,0 +1,56 @@
+/*
+* 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.myfaces.extensions.cdi.core.test.api.provider;
+
+
+import org.apache.myfaces.extensions.cdi.core.api.provider.BeanManagerProvider;
+import org.apache.webbeans.cditest.CdiTestContainer;
+import org.apache.webbeans.cditest.CdiTestContainerLoader;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import javax.enterprise.inject.spi.BeanManager;
+
+public class BeanManagerProviderTest
+{
+    @Test
+    public void testBeanManagerProvider() throws Exception {
+        CdiTestContainer cdiContainer = CdiTestContainerLoader.getCdiContainer();
+        Assert.assertNotNull(cdiContainer);
+        cdiContainer.bootContainer();
+        try
+        {
+            BeanManagerProvider bmp = BeanManagerProvider.getInstance();
+            Assert.assertNotNull(bmp);
+
+            BeanManager bm = bmp.getBeanManager();
+            Assert.assertNotNull(bm);
+
+            TestBean tb1 = (TestBean) bmp.getContextualReference(TestBean.class);
+            Assert.assertNotNull(tb1);
+
+            TestBean tb2 = (TestBean) bmp.getContextualReference("extraNameBean");
+            Assert.assertNotNull(tb2);
+        }
+        finally
+        {
+            cdiContainer.shutdownContainer();
+        }
+    }
+}

Propchange: myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/BeanManagerProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/TestBean.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/TestBean.java?rev=1124392&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/TestBean.java (added)
+++ myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/TestBean.java Wed May 18 19:29:06 2011
@@ -0,0 +1,39 @@
+/*
+* 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.myfaces.extensions.cdi.core.test.api.provider;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+
+@Named("extraNameBean")
+@ApplicationScoped
+public class TestBean
+{
+    int i=4711;
+
+    public int getI()
+    {
+        return i;
+    }
+
+    public void setI(int i)
+    {
+        this.i = i;
+    }
+}

Propchange: myfaces/extensions/cdi/trunk/core/api/src/test/java/org/apache/myfaces/extensions/cdi/core/test/api/provider/TestBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/beans.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/beans.xml?rev=1124392&view=auto
==============================================================================
    (empty)

Propchange: myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/beans.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/services/javax.enterprise.inject.spi.Extension
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/services/javax.enterprise.inject.spi.Extension?rev=1124392&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/services/javax.enterprise.inject.spi.Extension (added)
+++ myfaces/extensions/cdi/trunk/core/api/src/test/resources/META-INF/services/javax.enterprise.inject.spi.Extension Wed May 18 19:29:06 2011
@@ -0,0 +1,21 @@
+#####################################################################################
+# 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.
+#####################################################################################
+
+# myfaces-codi BeanManager provider
+org.apache.myfaces.extensions.cdi.core.api.provider.BeanManagerProvider

Modified: myfaces/extensions/cdi/trunk/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/pom.xml?rev=1124392&r1=1124391&r2=1124392&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/pom.xml (original)
+++ myfaces/extensions/cdi/trunk/pom.xml Wed May 18 19:29:06 2011
@@ -441,8 +441,7 @@
         <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
-            <classifier>jdk15</classifier>
-            <version>5.1</version>
+            <version>5.14.10</version>
             <scope>test</scope>
         </dependency>