You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2016/01/12 14:42:42 UTC

svn commit: r1724231 - in /aries/trunk/blueprint/blueprint-maven-plugin/src: main/java/org/apache/aries/blueprint/plugin/ test/java/org/apache/aries/blueprint/plugin/ test/java/org/apache/aries/blueprint/plugin/test/

Author: cschneider
Date: Tue Jan 12 13:42:42 2016
New Revision: 1724231

URL: http://svn.apache.org/viewvc?rev=1724231&view=rev
Log:
[ARIES-1476] Support 0 or multiple OsgiServiceProvider#classes.

- 0 classes adds auto-export="interfaces" to the service element
- multiple classes adds the interfaces (e.g. A and B) to the interfaces
  element:
<service ref="ref">
    <interfaces>
        <value>A</value>
        <value>B</value>
    </interfaces>
<service/>

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceABImpl.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceAImpl3.java
Modified:
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/OsgiServiceProviderWriter.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/OsgiServiceProviderWriter.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/OsgiServiceProviderWriter.java?rev=1724231&r1=1724230&r2=1724231&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/OsgiServiceProviderWriter.java (original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/OsgiServiceProviderWriter.java Tue Jan 12 13:42:42 2016
@@ -19,6 +19,7 @@
 package org.apache.aries.blueprint.plugin;
 
 import java.util.Collection;
+import java.util.List;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
@@ -28,6 +29,9 @@ import org.ops4j.pax.cdi.api.OsgiService
 import org.ops4j.pax.cdi.api.Properties;
 import org.ops4j.pax.cdi.api.Property;
 
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+
 public class OsgiServiceProviderWriter {
     private XMLStreamWriter writer;
 
@@ -40,30 +44,64 @@ public class OsgiServiceProviderWriter {
             write(bean);
         }
     }
-    
+
     public void write(Bean bean) throws XMLStreamException {
         OsgiServiceProvider serviceProvider = bean.clazz.getAnnotation(OsgiServiceProvider.class);
         if (serviceProvider == null) {
             return;
         }
-        if (serviceProvider.classes().length == 0) {
-            throw new IllegalArgumentException("Need to provide the interface class in the @OsgiServiceProvider(classes={...}) annotation on " + bean.clazz);
-        }
+
         Properties properties = bean.clazz.getAnnotation(Properties.class);
-        if (properties == null) {
+        List<String> interfaceNames = Lists.newArrayList();
+        for (Class<?> serviceIf : serviceProvider.classes()) {
+            interfaceNames.add(serviceIf.getName());
+        }
+
+        // If there are no properties to write and only one service attribute (either
+        // interface="MyServiceInterface" or auto-export="interfaces") then create an
+        // empty element
+        boolean writeEmptyElement = properties == null && interfaceNames.size() < 2;
+        if (writeEmptyElement) {
             writer.writeEmptyElement("service");
         } else {
             writer.writeStartElement("service");
         }
         writer.writeAttribute("ref", bean.id);
-        Class<?> serviceIf = serviceProvider.classes()[0];
-        writer.writeAttribute("interface", serviceIf.getName());
+
+        if (interfaceNames.size() == 0) {
+            writer.writeAttribute("auto-export", "interfaces");
+        } else if (interfaceNames.size() == 1) {
+            writer.writeAttribute("interface", Iterables.getOnlyElement(interfaceNames));
+        } else {
+            writeInterfacesElement(interfaceNames);
+        }
+
         writer.writeCharacters("\n");
         if (properties != null) {
             writeProperties(properties);
+        }
+
+        if (!writeEmptyElement) {
+            writer.writeEndElement();
+            writer.writeCharacters("\n");
+        }
+    }
+
+    private void writeInterfacesElement(Iterable<String> interfaceNames) throws XMLStreamException
+    {
+        writer.writeCharacters("\n");
+        writer.writeCharacters("    ");
+        writer.writeStartElement("interfaces");
+        writer.writeCharacters("\n");
+        for (String interfaceName : interfaceNames) {
+            writer.writeCharacters("        ");
+            writer.writeStartElement("value");
+            writer.writeCharacters(interfaceName);
             writer.writeEndElement();
             writer.writeCharacters("\n");
         }
+        writer.writeCharacters("    ");
+        writer.writeEndElement();
     }
 
     private void writeProperties(Properties properties) throws XMLStreamException {

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=1724231&r1=1724230&r2=1724231&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 Jan 12 13:42:42 2016
@@ -38,6 +38,8 @@ import javax.xml.xpath.XPathFactory;
 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.ServiceA;
+import org.apache.aries.blueprint.plugin.test.ServiceB;
 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.xbean.finder.ClassFinder;
 import org.junit.Assert;
@@ -105,6 +107,31 @@ public class GeneratorTest {
         // @Autowired
         Assert.assertEquals("my1", xpath.evaluate("property[@name='bean2']/@ref", bean1));
 
+        // Service with 1 interface
+        Node serviceAImpl2 = (Node) xpath.evaluate("/blueprint/service[@ref='my2']", document, XPathConstants.NODE);
+        Assert.assertEquals(ServiceA.class.getName(), xpath.evaluate("@interface", serviceAImpl2));
+        Assert.assertEquals("", xpath.evaluate("@auto-export", serviceAImpl2));
+        Assert.assertEquals("", xpath.evaluate("interfaces", serviceAImpl2));
+
+        // Service with 0 interfaces (using auto-export=interfaces instead)
+        Node serviceAImpl3 = (Node) xpath.evaluate("/blueprint/service[@ref='serviceAImpl3']", document, XPathConstants.NODE);
+        Assert.assertEquals("", xpath.evaluate("@interface", serviceAImpl3));
+        Assert.assertEquals("interfaces", xpath.evaluate("@auto-export", serviceAImpl3));
+        Assert.assertEquals("", xpath.evaluate("interfaces", serviceAImpl3));
+
+        // Service with 2 interfaces (using <interfaces><value>ServiceA</value><value>ServiceB</value></interfaces>
+        Node serviceABImpl = (Node) xpath.evaluate("/blueprint/service[@ref='serviceABImpl']", document, XPathConstants.NODE);
+        Assert.assertEquals("", xpath.evaluate("@interface", serviceABImpl));
+        Assert.assertEquals("", xpath.evaluate("@auto-export", serviceABImpl));
+
+        NodeList interfaceValues = (NodeList) xpath.evaluate("interfaces/value", serviceABImpl, XPathConstants.NODESET);
+        Set<String> interfaceNames = new HashSet<String>();
+        for (int i = 0; i < interfaceValues.getLength(); ++i) {
+            Node interfaceValue = interfaceValues.item(i);
+            interfaceNames.add(interfaceValue.getTextContent());
+        }
+        Assert.assertEquals(Sets.newHashSet(ServiceA.class.getName(), ServiceB.class.getName()),
+                            interfaceNames);
     }
 
     private Document readToDocument(ByteArrayOutputStream os) throws ParserConfigurationException,

Added: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceABImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceABImpl.java?rev=1724231&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceABImpl.java (added)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceABImpl.java Tue Jan 12 13:42:42 2016
@@ -0,0 +1,29 @@
+/**
+ * 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.aries.blueprint.plugin.test;
+
+import javax.inject.Singleton;
+
+import org.ops4j.pax.cdi.api.OsgiServiceProvider;
+
+@Singleton
+@OsgiServiceProvider(classes = {ServiceA.class, ServiceB.class})
+public class ServiceABImpl implements ServiceA, ServiceB
+{
+}

Added: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceAImpl3.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceAImpl3.java?rev=1724231&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceAImpl3.java (added)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceAImpl3.java Tue Jan 12 13:42:42 2016
@@ -0,0 +1,29 @@
+/**
+ * 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.aries.blueprint.plugin.test;
+
+import javax.inject.Singleton;
+
+import org.ops4j.pax.cdi.api.OsgiServiceProvider;
+
+@Singleton
+@OsgiServiceProvider
+public class ServiceAImpl3 implements ServiceA
+{
+}