You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by al...@apache.org on 2016/06/03 21:58:39 UTC

svn commit: r1746775 - in /aries/trunk/blueprint/blueprint-maven-plugin/src: main/java/org/apache/aries/blueprint/plugin/model/ main/java/org/apache/aries/blueprint/plugin/model/service/ test/java/org/apache/aries/blueprint/plugin/ test/java/org/apache...

Author: alien11689
Date: Fri Jun  3 21:58:39 2016
New Revision: 1746775

URL: http://svn.apache.org/viewvc?rev=1746775&view=rev
Log:
[ARIES-1561] Allow for exposing service from produced beans

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.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/service/ServiceProvider.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/test/MyFactoryNamedBean.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceReferences.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=1746775&r1=1746774&r2=1746775&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 Fri Jun  3 21:58:39 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
- * <p>
+ * <p/>
  * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ * <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
@@ -93,6 +93,10 @@ public class Context implements Matcher
                     producedBean.setSingleton();
                 }
                 reg.add(producedBean);
+                ServiceProvider serviceProvider = ServiceProvider.fromMethod(producedBean, method);
+                if (serviceProvider != null) {
+                    serviceProviders.add(serviceProvider);
+                }
             }
         }
     }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/service/ServiceProvider.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/service/ServiceProvider.java?rev=1746775&r1=1746774&r2=1746775&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/service/ServiceProvider.java (original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/service/ServiceProvider.java Fri Jun  3 21:58:39 2016
@@ -1,11 +1,32 @@
+/**
+ * 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
+ * <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
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.aries.blueprint.plugin.model.service;
 
 import com.google.common.collect.Lists;
 import org.apache.aries.blueprint.plugin.model.Bean;
+import org.apache.aries.blueprint.plugin.model.BeanRef;
 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
 import org.ops4j.pax.cdi.api.Properties;
 import org.ops4j.pax.cdi.api.Property;
 
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -22,25 +43,43 @@ public class ServiceProvider {
     }
 
     public static ServiceProvider fromBean(Bean bean) {
-        OsgiServiceProvider serviceProvider = bean.clazz.getAnnotation(OsgiServiceProvider.class);
+        return createServiceProvider(bean.clazz, bean.id);
+    }
+
+    public static ServiceProvider fromMethod(BeanRef beanRef, Method method) {
+        return createServiceProvider(method, beanRef.id);
+    }
+
+    private static ServiceProvider createServiceProvider(AnnotatedElement annotatedElement, String ref) {
+        OsgiServiceProvider serviceProvider = annotatedElement.getAnnotation(OsgiServiceProvider.class);
+        Properties properties = annotatedElement.getAnnotation(Properties.class);
+
         if (serviceProvider == null) {
             return null;
         }
 
-        List<String> interfaceNames = Lists.newArrayList();
-        for (Class<?> serviceIf : serviceProvider.classes()) {
-            interfaceNames.add(serviceIf.getName());
-        }
+        List<String> interfaceNames = extractServiceInterfaces(serviceProvider);
+
+        Map<String, String> propertiesAsMap = extractProperties(properties);
 
-        Properties properties = bean.clazz.getAnnotation(Properties.class);
+        return new ServiceProvider(interfaceNames, ref, propertiesAsMap);
+    }
 
+    private static Map<String, String> extractProperties(Properties properties) {
         Map<String, String> propertiesAsMap = new HashMap<>();
         if (properties != null) {
             for (Property property : properties.value()) {
                 propertiesAsMap.put(property.name(), property.value());
             }
         }
+        return propertiesAsMap;
+    }
 
-        return new ServiceProvider(interfaceNames, bean.id, propertiesAsMap);
+    private static List<String> extractServiceInterfaces(OsgiServiceProvider serviceProvider) {
+        List<String> interfaceNames = Lists.newArrayList();
+        for (Class<?> serviceIf : serviceProvider.classes()) {
+            interfaceNames.add(serviceIf.getName());
+        }
+        return interfaceNames;
     }
 }

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=1746775&r1=1746774&r2=1746775&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 Fri Jun  3 21:58:39 2016
@@ -22,6 +22,7 @@ import com.google.common.collect.Sets;
 import org.apache.aries.blueprint.plugin.model.Context;
 import org.apache.aries.blueprint.plugin.model.TransactionalDef;
 import org.apache.aries.blueprint.plugin.test.MyBean1;
+import org.apache.aries.blueprint.plugin.test.MyProduced;
 import org.apache.aries.blueprint.plugin.test.ServiceA;
 import org.apache.aries.blueprint.plugin.test.ServiceB;
 import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -221,6 +222,46 @@ public class GeneratorTest {
         assertEquals("ser1", xpath.evaluate("argument[3]/@ref", bean1));
     }
 
+    @Test
+    public void testExposeProducedBeanAsServiceWithAutoExport() throws Exception {
+        Node service = getServiceByRef("producedForService");
+        assertEquals("interfaces", xpath.evaluate("@auto-export", service));
+        assertEquals("", xpath.evaluate("@interface", service));
+        assertEquals("0", xpath.evaluate("count(interfaces)", service));
+        assertEquals("0", xpath.evaluate("count(service-properties)", service));
+    }
+
+    @Test
+    public void testExposeProducedBeanAsServiceWithOneInterface() throws Exception {
+        Node service = getServiceByRef("producedForServiceWithOneInterface");
+        assertEquals("", xpath.evaluate("@auto-export", service));
+        assertEquals(MyProduced.class.getName(), xpath.evaluate("@interface", service));
+        assertEquals("0", xpath.evaluate("count(interfaces)", service));
+        assertEquals("0", xpath.evaluate("count(service-properties)", service));
+    }
+
+    @Test
+    public void testExposeProducedBeanAsServiceWithTwoInterfaces() throws Exception {
+        Node service = getServiceByRef("producedForServiceWithTwoInterfaces");
+        assertEquals("", xpath.evaluate("@auto-export", service));
+        assertEquals("", xpath.evaluate("@interface", service));
+        assertEquals("2", xpath.evaluate("count(interfaces/value)", service));
+        assertEquals(MyProduced.class.getName(), xpath.evaluate("interfaces/value[1]", service));
+        assertEquals(ServiceA.class.getName(), xpath.evaluate("interfaces/value[2]", service));
+        assertEquals("0", xpath.evaluate("count(service-properties)", service));
+    }
+
+    @Test
+    public void testExposeProducedBeanAsServiceWithServiceProperties() throws Exception {
+        Node service = getServiceByRef("producedForServiceWithProperties");
+        assertEquals("interfaces", xpath.evaluate("@auto-export", service));
+        assertEquals("", xpath.evaluate("@interface", service));
+        assertEquals("0", xpath.evaluate("count(interfaces)", service));
+        assertEquals("2", xpath.evaluate("count(service-properties/entry)", service));
+        assertEquals("v1", xpath.evaluate("service-properties/entry[@key='n1']/@value", service));
+        assertEquals("v2", xpath.evaluate("service-properties/entry[@key='n2']/@value", service));
+    }
+
     private static Document readToDocument(ByteArrayOutputStream os) throws ParserConfigurationException,
             SAXException, IOException {
         InputStream is = new ByteArrayInputStream(os.toByteArray());

Added: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java?rev=1746775&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java (added)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java Fri Jun  3 21:58:39 2016
@@ -0,0 +1,62 @@
+/**
+ * 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
+ * <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
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.blueprint.plugin.test;
+
+import org.ops4j.pax.cdi.api.OsgiServiceProvider;
+import org.ops4j.pax.cdi.api.Properties;
+import org.ops4j.pax.cdi.api.Property;
+
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+@Singleton
+public class MyFactoryBeanAsService {
+    
+    @Produces
+    @Named("producedForService")
+    @OsgiServiceProvider
+    public MyProduced createBeanWithServiceExpose1() {
+        return new MyProduced("My message");
+    }
+
+    @Produces
+    @Named("producedForServiceWithOneInterface")
+    @OsgiServiceProvider(classes = MyProduced.class)
+    public MyProduced createBeanWithServiceExpose2() {
+        return new MyProduced("My message");
+    }
+    @Produces
+    @Named("producedForServiceWithTwoInterfaces")
+    @OsgiServiceProvider(classes = {MyProduced.class, ServiceA.class})
+    public MyProduced createBeanWithServiceExpose3() {
+        return new MyProduced("My message");
+    }
+
+    @Produces
+    @Named("producedForServiceWithProperties")
+    @OsgiServiceProvider
+    @Properties({
+        @Property(name = "n1", value = "v1"),
+        @Property(name = "n2", value = "v2")
+    })
+    public MyProduced createBeanWithServiceExpose4() {
+        return new MyProduced("My message");
+    }
+}

Modified: 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=1746775&r1=1746774&r2=1746775&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryNamedBean.java (original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryNamedBean.java Fri Jun  3 21:58:39 2016
@@ -37,4 +37,5 @@ public class MyFactoryNamedBean {
     public MyProduced createBean2() {
         return new MyProduced("My message");
     }
+
 }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceReferences.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceReferences.java?rev=1746775&r1=1746774&r2=1746775&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceReferences.java (original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceReferences.java Fri Jun  3 21:58:39 2016
@@ -1,4 +1,3 @@
-
 /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file