From ae92f181eeb66ac5eb2db4f395f5ea3ecd27331b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20D=C4=85browski?= Date: Tue, 20 Dec 2016 13:15:05 +0100 Subject: [PATCH] [ARIES-1641] Read "service.ranking" property as ranking attribute in service element --- .../pax/OsgiServiceProviderHandler.java | 28 +++++++++++++++---- .../aries/blueprint/plugin/GeneratorTest.java | 14 ++++++++++ .../plugin/test/MyFactoryBeanAsService.java | 3 +- .../plugin/test/ServiceWithRanking.java | 15 ++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceWithRanking.java diff --git a/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/pax/OsgiServiceProviderHandler.java b/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/pax/OsgiServiceProviderHandler.java index ef685f3ddb..e302ea0bf6 100644 --- a/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/pax/OsgiServiceProviderHandler.java +++ b/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/pax/OsgiServiceProviderHandler.java @@ -31,11 +31,15 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.lang.reflect.AnnotatedElement; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class OsgiServiceProviderHandler implements BeanAnnotationHandler { + + private static final List SPECIAL_PROPERTIES = Collections.singletonList("service.ranking"); + @Override public Class getAnnotation() { return OsgiServiceProvider.class; @@ -83,6 +87,7 @@ private void writeService(XMLStreamWriter writer, Map properties } if (!propertiesAsMap.isEmpty()) { + writeRanking(writer, propertiesAsMap); writeProperties(writer, propertiesAsMap); } @@ -126,16 +131,29 @@ private void writeInterfacesElement(XMLStreamWriter writer, Iterable int writer.writeCharacters("\n"); } + private void writeRanking(XMLStreamWriter writer, Map propertiesAsMap) throws XMLStreamException { + if (propertiesAsMap.containsKey("service.ranking")) { + try { + Integer ranking = Integer.parseInt(propertiesAsMap.get("service.ranking")); + writer.writeAttribute("ranking", ranking.toString()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("service.ranking property must be an integer!"); + } + } + } + private void writeProperties(XMLStreamWriter writer, Map properties) throws XMLStreamException { writer.writeCharacters(" "); writer.writeStartElement("service-properties"); writer.writeCharacters("\n"); for (Map.Entry property : properties.entrySet()) { - writer.writeCharacters(" "); - writer.writeEmptyElement("entry"); - writer.writeAttribute("key", property.getKey()); - writer.writeAttribute("value", property.getValue()); - writer.writeCharacters("\n"); + if (!SPECIAL_PROPERTIES.contains(property.getKey())) { + writer.writeCharacters(" "); + writer.writeEmptyElement("entry"); + writer.writeAttribute("key", property.getKey()); + writer.writeAttribute("value", property.getValue()); + writer.writeCharacters("\n"); + } } writer.writeCharacters(" "); writer.writeEndElement(); diff --git a/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java b/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java index b5db69e5cb..8ce4701fa2 100644 --- a/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java +++ b/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java @@ -190,6 +190,18 @@ public void testGenerateServiceWith2Interfaces() throws Exception { interfaceNames); } + @Test + public void testGenerateServiceWithRanking() throws Exception { + Node serviceWithRanking = getServiceByRef("serviceWithRanking"); + + assertXpathDoesNotExist(serviceWithRanking, "@interface"); + assertXpathEquals(serviceWithRanking, "@auto-export", "interfaces"); + assertXpathDoesNotExist(serviceWithRanking, "interfaces"); + assertXpathEquals(serviceWithRanking, "@ranking", "100"); + assertXpathEquals(serviceWithRanking, "count(service-properties/entry)", "0"); + assertXpathDoesNotExist(serviceWithRanking, "service-properties/entry[@key='service.ranking']"); + } + @Test public void testGenerateBeanWithConstructorInjection() throws Exception { // Bean with constructor injection @@ -289,9 +301,11 @@ public void testExposeProducedBeanAsServiceWithServiceProperties() throws Except assertXpathEquals(service, "@auto-export", "interfaces"); assertXpathDoesNotExist(service, "@interface"); assertXpathDoesNotExist(service, "interfaces"); + assertXpathEquals(service, "@ranking", "100"); assertXpathEquals(service, "count(service-properties/entry)", "2"); assertXpathEquals(service, "service-properties/entry[@key='n1']/@value", "v1"); assertXpathEquals(service, "service-properties/entry[@key='n2']/@value", "v2"); + assertXpathDoesNotExist(service, "service-properties/entry[@key='service.ranking']"); } @Test diff --git a/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java b/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java index 11fed76f8c..b1a5cab45c 100644 --- a/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java +++ b/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBeanAsService.java @@ -54,7 +54,8 @@ public MyProduced createBeanWithServiceExpose3() { @OsgiServiceProvider @Properties({ @Property(name = "n1", value = "v1"), - @Property(name = "n2", value = "v2") + @Property(name = "n2", value = "v2"), + @Property(name = "service.ranking", value = "100") }) public MyProduced createBeanWithServiceExpose4() { return new MyProduced("My message"); diff --git a/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceWithRanking.java b/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceWithRanking.java new file mode 100644 index 0000000000..7f379478c6 --- /dev/null +++ b/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ServiceWithRanking.java @@ -0,0 +1,15 @@ +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.inject.Singleton; + +@Singleton +@OsgiServiceProvider +@Properties({ + @Property(name = "service.ranking", value = "100") +}) +public class ServiceWithRanking implements ServiceA { +} \ No newline at end of file