aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1736188 - in /aries/trunk/blueprint/blueprint-maven-plugin/src: main/java/org/apache/aries/blueprint/plugin/model/ test/java/org/apache/aries/blueprint/plugin/ test/java/org/apache/aries/blueprint/plugin/model/ test/java/org/apache/aries/b...
Date Tue, 22 Mar 2016 13:35:55 GMT
Author: cschneider
Date: Tue Mar 22 13:35:54 2016
New Revision: 1736188

URL: http://svn.apache.org/viewvc?rev=1736188&view=rev
Log:
[ARIES-1508] Let beans created with annotation @Produces could be named with @Named

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryNamedBean.java
Modified:
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java?rev=1736188&r1=1736187&r2=1736188&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
Tue Mar 22 13:35:54 2016
@@ -6,9 +6,9 @@
  * 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
- *
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
  * 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
@@ -18,6 +18,15 @@
  */
 package org.apache.aries.blueprint.plugin.model;
 
+import org.ops4j.pax.cdi.api.OsgiService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+import org.osgi.service.blueprint.container.Converter;
+
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+import javax.inject.Singleton;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.Arrays;
@@ -25,14 +34,6 @@ import java.util.Collection;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
-import javax.enterprise.inject.Produces;
-
-import org.ops4j.pax.cdi.api.OsgiService;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.service.blueprint.container.BlueprintContainer;
-import org.osgi.service.blueprint.container.Converter;
-
 public class Context implements Matcher {
 
     SortedSet<BeanRef> reg;
@@ -70,9 +71,19 @@ public class Context implements Matcher
     private void addProducedBeans(Class<?> clazz, BeanRef factoryBean) {
         for (Method method : clazz.getMethods()) {
             Produces produces = method.getAnnotation(Produces.class);
+            Named named = method.getAnnotation(Named.class);
+            Singleton singleton = method.getAnnotation(Singleton.class);
             if (produces != null) {
                 Class<?> producedClass = method.getReturnType();
-                ProducedBean producedBean = new ProducedBean(producedClass, factoryBean,
method.getName());
+                ProducedBean producedBean;
+                if (named != null) {
+                    producedBean = new ProducedBean(producedClass, named.value(), factoryBean,
method.getName());
+                } else {
+                    producedBean = new ProducedBean(producedClass, factoryBean, method.getName());
+                }
+                if (singleton != null) {
+                    producedBean.setSingleton();
+                }
                 reg.add(producedBean);
             }
         }
@@ -108,7 +119,7 @@ public class Context implements Matcher
         TreeSet<Bean> beans = new TreeSet<Bean>();
         for (BeanRef ref : reg) {
             if (ref instanceof Bean) {
-                beans.add((Bean)ref);
+                beans.add((Bean) ref);
             }
         }
         return beans;
@@ -118,7 +129,7 @@ public class Context implements Matcher
         TreeSet<OsgiServiceRef> serviceRefs = new TreeSet<OsgiServiceRef>();
         for (BeanRef ref : reg) {
             if (ref instanceof OsgiServiceRef) {
-                serviceRefs.add((OsgiServiceRef)ref);
+                serviceRefs.add((OsgiServiceRef) ref);
             }
         }
         return serviceRefs;

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java?rev=1736188&r1=1736187&r2=1736188&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
Tue Mar 22 13:35:54 2016
@@ -4,11 +4,21 @@ package org.apache.aries.blueprint.plugi
 public class ProducedBean extends Bean {
     public String factoryMethod;
     public BeanRef factoryBean;
-    
+
     public ProducedBean(Class<?> clazz, BeanRef factoryBean, String factoryMethod)
{
         super(clazz);
         this.factoryBean = factoryBean;
         this.factoryMethod = factoryMethod;
     }
 
+    public ProducedBean(Class<?> clazz, String id, BeanRef factoryBean, String factoryMethod)
{
+        super(clazz);
+        this.id = id;
+        this.factoryBean = factoryBean;
+        this.factoryMethod = factoryMethod;
+    }
+
+    public void setSingleton(){
+        this.isPrototype = false;
+    }
 }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java?rev=1736188&r1=1736187&r2=1736188&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
Tue Mar 22 13:35:54 2016
@@ -185,6 +185,24 @@ public class GeneratorTest {
         assertEquals("(mode=123)", xpath.evaluate("@filter", ser2));
     }
 
+    @Test
+    public void testProducesNamedBeans() throws Exception {
+        Node bean1 = getBeanById("produced1");
+        assertEquals("org.apache.aries.blueprint.plugin.test.MyProduced", xpath.evaluate("@class",
bean1));
+        assertEquals("myFactoryNamedBean", xpath.evaluate("@factory-ref", bean1));
+        assertEquals("createBean1", xpath.evaluate("@factory-method", bean1));
+        assertEquals("prototype", xpath.evaluate("@scope", bean1));
+
+        Node bean2 = getBeanById("produced2");
+        assertEquals("org.apache.aries.blueprint.plugin.test.MyProduced", xpath.evaluate("@class",
bean1));
+        assertEquals("myFactoryNamedBean", xpath.evaluate("@factory-ref", bean2));
+        assertEquals("createBean2", xpath.evaluate("@factory-method", bean2));
+        assertEquals("", xpath.evaluate("@scope", bean2));
+
+        Node myBean5 = getBeanById("myBean5");
+        assertEquals("produced2", xpath.evaluate("argument[8]/@ref", myBean5));
+    }
+
     private static Document readToDocument(ByteArrayOutputStream os) throws ParserConfigurationException,
             SAXException, IOException {
         InputStream is = new ByteArrayInputStream(os.toByteArray());

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java?rev=1736188&r1=1736187&r2=1736188&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
Tue Mar 22 13:35:54 2016
@@ -166,7 +166,7 @@ public class BeanTest {
         assertNull("There should be no destroyMethod", bean.destroyMethod);
         assertTrue("There should be no persistenceUnit", bean.persistenceFields.isEmpty());
         assertEquals(0, bean.properties.size());
-        assertEquals(7, bean.constructorArguments.size());
+        assertEquals(8, bean.constructorArguments.size());
         assertEquals("my2", bean.constructorArguments.get(0).getRef());
         assertEquals("serviceA", bean.constructorArguments.get(1).getRef());
         assertEquals("serviceB", bean.constructorArguments.get(2).getRef());
@@ -174,6 +174,7 @@ public class BeanTest {
         assertEquals("ser1", bean.constructorArguments.get(4).getRef());
         assertEquals("ser2", bean.constructorArguments.get(5).getRef());
         assertEquals("serviceA", bean.constructorArguments.get(6).getRef());
+        assertEquals("produced2", bean.constructorArguments.get(7).getRef());
     }
 
 }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java?rev=1736188&r1=1736187&r2=1736188&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java
Tue Mar 22 13:35:54 2016
@@ -49,7 +49,8 @@ public class MyBean5 {
                    @Value("100") int bla,
                    @OsgiService(filter = "myRef") @Named("ser1") ServiceC myReference,
                    @OsgiService(filter = "(mode=123)") @Named("ser2") ServiceC myReference2,
-                   @AnnotatedService ServiceA serviceAAnnotated) {
+                   @AnnotatedService ServiceA serviceAAnnotated,
+                   @Named("produced2") MyProduced myProduced) {
         this.serviceA1 = serviceA1;
         this.serviceA2 = serviceA2;
         this.serviceB = serviceB;

Added: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryNamedBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryNamedBean.java?rev=1736188&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryNamedBean.java
(added)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryNamedBean.java
Tue Mar 22 13:35:54 2016
@@ -0,0 +1,22 @@
+package org.apache.aries.blueprint.plugin.test;
+
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+@Singleton
+public class MyFactoryNamedBean {
+    
+    @Produces
+    @Named("produced1")
+    public MyProduced createBean1() {
+        return new MyProduced("My message");
+    }
+
+    @Produces
+    @Named("produced2")
+    @Singleton
+    public MyProduced createBean2() {
+        return new MyProduced("My message");
+    }
+}



Mime
View raw message