Author: cschneider
Date: Mon Oct 17 19:22:21 2016
New Revision: 1765356

URL: http://svn.apache.org/viewvc?rev=1765356&view=rev
Log:
[ARIES-1628] Allow to configure beans with pure annotations

Added:
    aries/trunk/blueprint/blueprint-maven-plugin-annotation/
    aries/trunk/blueprint/blueprint-maven-plugin-annotation/pom.xml
    aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/
    aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/
    aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/
    aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/
    
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/
    
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/
    
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/
    
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/
    
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/
    
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/Config.java
    
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/ConfigProperty.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigAnnotationHandler.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigPropertyInjectionHandler.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigWriter.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithConfig.java
Modified:
    aries/trunk/blueprint/blueprint-maven-plugin/pom.xml
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.ValueInjectionHandler
    
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
    aries/trunk/blueprint/pom.xml

Added: aries/trunk/blueprint/blueprint-maven-plugin-annotation/pom.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin-annotation/pom.xml?rev=1765356&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin-annotation/pom.xml (added)
+++ aries/trunk/blueprint/blueprint-maven-plugin-annotation/pom.xml Mon Oct 17 
19:22:21 2016
@@ -0,0 +1,10 @@
+<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/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.aries.blueprint</groupId>
+    <artifactId>blueprint</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>blueprint-maven-plugin-annotation</artifactId>
+  <name>blueprint-config-api</name>
+</project>
\ No newline at end of file

Added: 
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/Config.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/Config.java?rev=1765356&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/Config.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/Config.java
 Mon Oct 17 19:22:21 2016
@@ -0,0 +1,17 @@
+package org.apache.aries.blueprint.api.config;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotating any class with this will create a 
+ * cm:property-placeholder element in blueprint
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Config {
+    String pid();
+    String updatePolicy() default "reload"; 
+}

Added: 
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/ConfigProperty.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/ConfigProperty.java?rev=1765356&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/ConfigProperty.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin-annotation/src/main/java/org/apache/aries/blueprint/api/config/ConfigProperty.java
 Mon Oct 17 19:22:21 2016
@@ -0,0 +1,16 @@
+package org.apache.aries.blueprint.api.config;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotate fields with this to inject configuration like:
+ * @ConfigProperty("${mykey}")
+ */
+@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ConfigProperty {
+    String value();
+}

Modified: aries/trunk/blueprint/blueprint-maven-plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/pom.xml?rev=1765356&r1=1765355&r2=1765356&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/pom.xml (original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/pom.xml Mon Oct 17 19:22:21 
2016
@@ -154,6 +154,11 @@
             <version>0.8.0</version>
         </dependency>
         <dependency>
+               <groupId>org.apache.aries.blueprint</groupId>
+               <artifactId>blueprint-maven-plugin-annotation</artifactId>
+               <version>1.0.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
             <groupId>org.apache.maven</groupId>
             <artifactId>maven-plugin-api</artifactId>
             <version>${maven.version}</version>

Added: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigAnnotationHandler.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigAnnotationHandler.java?rev=1765356&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigAnnotationHandler.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigAnnotationHandler.java
 Mon Oct 17 19:22:21 2016
@@ -0,0 +1,46 @@
+/**
+ * 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.javax;
+
+import java.lang.reflect.AnnotatedElement;
+
+import org.apache.aries.blueprint.api.config.Config;
+import org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler;
+import org.apache.aries.blueprint.plugin.spi.BeanEnricher;
+import org.apache.aries.blueprint.plugin.spi.ContextEnricher;
+import org.apache.aries.blueprint.plugin.spi.XmlWriter;
+
+public class ConfigAnnotationHandler implements BeanAnnotationHandler<Config>{
+
+    @Override
+    public Class<Config> getAnnotation() {
+        return Config.class;
+    }
+
+    @Override
+    public void handleBeanAnnotation(AnnotatedElement annotatedElement, String 
id,
+                                     ContextEnricher contextEnricher, 
BeanEnricher beanEnricher) {
+        Config config = annotatedElement.getAnnotation(Config.class);
+        XmlWriter writer = new ConfigWriter(config);
+        contextEnricher.addBlueprintContentWriter("config", writer);
+        
contextEnricher.getBlueprintConfiguration().getNamespaces().add(ConfigWriter.CONFIG_NS);
+        
+    }
+
+}

Added: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigPropertyInjectionHandler.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigPropertyInjectionHandler.java?rev=1765356&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigPropertyInjectionHandler.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigPropertyInjectionHandler.java
 Mon Oct 17 19:22:21 2016
@@ -0,0 +1,44 @@
+/**
+ * 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.javax;
+
+import java.lang.reflect.AnnotatedElement;
+
+import org.apache.aries.blueprint.api.config.ConfigProperty;
+
+public class ConfigPropertyInjectionHandler implements 
org.apache.aries.blueprint.plugin.spi.ValueInjectionHandler<ConfigProperty> {
+
+    @Override
+    public Class<ConfigProperty> getAnnotation() {
+        return ConfigProperty.class;
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    public String getValue(Class clazz, AnnotatedElement annotatedElement) {
+        ConfigProperty annotation = 
annotatedElement.getAnnotation(ConfigProperty.class);
+        return annotation.value();
+    }
+
+    @Override
+    public String getValue(Object annotation) {
+        return ((ConfigProperty)annotation).value();
+    }
+
+}

Added: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigWriter.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigWriter.java?rev=1765356&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigWriter.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/javax/ConfigWriter.java
 Mon Oct 17 19:22:21 2016
@@ -0,0 +1,46 @@
+/**
+ * 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.javax;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.aries.blueprint.api.config.Config;
+import org.apache.aries.blueprint.plugin.spi.XmlWriter;
+
+public class ConfigWriter implements XmlWriter {
+    
+    static final String CONFIG_NS = 
"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0";;
+    private Config config;
+
+    public ConfigWriter(Config config) {
+        this.config = config;
+    }
+
+    @Override
+    public void write(XMLStreamWriter writer) throws XMLStreamException {
+        writer.writeStartElement("property-placeholder");
+        writer.writeDefaultNamespace(CONFIG_NS);
+        writer.writeAttribute("persistent-id", config.pid());
+        writer.writeAttribute("update-strategy", config.updatePolicy());
+        writer.writeCharacters("\n");
+        writer.writeEndElement();
+    }
+
+}

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler?rev=1765356&r1=1765355&r2=1765356&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler
 Mon Oct 17 19:22:21 2016
@@ -20,4 +20,5 @@ org.apache.aries.blueprint.plugin.spring
 org.apache.aries.blueprint.plugin.spring.DependsOnAttributeResolver
 org.apache.aries.blueprint.plugin.spring.SpringTransactionalFactory
 org.apache.aries.blueprint.plugin.javax.JavaxTransactionFactory
-org.apache.aries.blueprint.plugin.javax.cdi.CdiTransactionFactory
\ No newline at end of file
+org.apache.aries.blueprint.plugin.javax.cdi.CdiTransactionFactory
+org.apache.aries.blueprint.plugin.javax.ConfigAnnotationHandler

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.ValueInjectionHandler
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.ValueInjectionHandler?rev=1765356&r1=1765355&r2=1765356&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.ValueInjectionHandler
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.ValueInjectionHandler
 Mon Oct 17 19:22:21 2016
@@ -15,4 +15,5 @@
 # limitations under the License.
 #
 
-org.apache.aries.blueprint.plugin.spring.ValueInjectionHandler
\ No newline at end of file
+org.apache.aries.blueprint.plugin.spring.ValueInjectionHandler
+org.apache.aries.blueprint.plugin.javax.ConfigPropertyInjectionHandler

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=1765356&r1=1765355&r2=1765356&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
 Mon Oct 17 19:22:21 2016
@@ -18,7 +18,16 @@
  */
 package org.apache.aries.blueprint.plugin.model;
 
-import com.google.common.collect.Sets;
+import static junit.framework.Assert.assertFalse;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.inject.Named;
+
 import org.apache.aries.blueprint.plugin.BlueprintConfigurationImpl;
 import org.apache.aries.blueprint.plugin.bad.BadBean1;
 import org.apache.aries.blueprint.plugin.bad.BadBean2;
@@ -27,6 +36,7 @@ import org.apache.aries.blueprint.plugin
 import org.apache.aries.blueprint.plugin.bad.BadFieldBean2;
 import org.apache.aries.blueprint.plugin.bad.BadFieldBean3;
 import org.apache.aries.blueprint.plugin.bad.FieldBean4;
+import org.apache.aries.blueprint.plugin.test.BeanWithConfig;
 import org.apache.aries.blueprint.plugin.test.MyBean1;
 import org.apache.aries.blueprint.plugin.test.MyBean3;
 import org.apache.aries.blueprint.plugin.test.MyBean4;
@@ -34,14 +44,7 @@ import org.apache.aries.blueprint.plugin
 import org.apache.aries.blueprint.plugin.test.ServiceAImpl1;
 import org.junit.Test;
 
-import javax.inject.Named;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-
-import static junit.framework.Assert.assertFalse;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import com.google.common.collect.Sets;
 
 public class BeanTest {
     private static final String NS_JPA1 = 
"http://aries.apache.org/xmlns/jpa/v1.1.0";;
@@ -197,4 +200,14 @@ public class BeanTest {
         assertEquals("serviceA", bean.constructorArguments.get(6).getRef());
         assertEquals("produced2", bean.constructorArguments.get(7).getRef());
     }
+    
+    @Test
+    public void testParseBeanWithConfig() {
+        Bean bean = new Bean(BeanWithConfig.class, context);
+        bean.resolve(context);
+        assertEquals("There should be a property", 1, bean.properties.size());
+        Property prop = bean.properties.iterator().next();
+        assertEquals("title", prop.name);
+        assertEquals("${title}", prop.value);
+    }
 }

Added: 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithConfig.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithConfig.java?rev=1765356&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithConfig.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithConfig.java
 Mon Oct 17 19:22:21 2016
@@ -0,0 +1,13 @@
+package org.apache.aries.blueprint.plugin.test;
+
+import javax.inject.Singleton;
+
+import org.apache.aries.blueprint.api.config.Config;
+import org.apache.aries.blueprint.api.config.ConfigProperty;
+
+@Config(pid="org.apache.aries.my")
+@Singleton
+public class BeanWithConfig {
+    @ConfigProperty("${title}")
+    String title;
+}

Modified: aries/trunk/blueprint/pom.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/pom.xml?rev=1765356&r1=1765355&r2=1765356&view=diff
==============================================================================
--- aries/trunk/blueprint/pom.xml (original)
+++ aries/trunk/blueprint/pom.xml Mon Oct 17 19:22:21 2016
@@ -46,6 +46,7 @@
         <module>blueprint-compatibility</module>
         <module>blueprint-core-compatibility</module>
         <module>blueprint-maven-plugin-spi</module>
+        <module>blueprint-maven-plugin-annotation</module>
         <module>blueprint-maven-plugin</module>
         <module>blueprint-noosgi</module>
         <module>blueprint-web</module>


Reply via email to