Author: alien11689 Date: Wed Feb 1 22:00:29 2017 New Revision: 1781323 URL: http://svn.apache.org/viewvc?rev=1781323&view=rev Log: [ARIES-1676] Handle bean annotation
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/BeanHandler.java aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BasicBean.java aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BeanWithCallbackMethods.java aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/NamedBean.java aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/SimpleProducedBean.java Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanFinder aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.FactoryMethodFinder aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.NamedLikeHandler aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/BlueprintFileWriterTest.java Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/BeanHandler.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/BeanHandler.java?rev=1781323&view=auto ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/BeanHandler.java (added) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/BeanHandler.java Wed Feb 1 22:00:29 2017 @@ -0,0 +1,75 @@ +package org.apache.aries.blueprint.plugin.handlers.bean; + +import org.apache.aries.blueprint.annotation.bean.Activation; +import org.apache.aries.blueprint.annotation.bean.Bean; +import org.apache.aries.blueprint.annotation.bean.Scope; +import org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler; +import org.apache.aries.blueprint.plugin.spi.BeanEnricher; +import org.apache.aries.blueprint.plugin.spi.BeanFinder; +import org.apache.aries.blueprint.plugin.spi.ContextEnricher; +import org.apache.aries.blueprint.plugin.spi.FactoryMethodFinder; +import org.apache.aries.blueprint.plugin.spi.NamedLikeHandler; + +import java.lang.reflect.AnnotatedElement; + +public class BeanHandler implements + BeanFinder<Bean>, + FactoryMethodFinder<Bean>, + NamedLikeHandler<Bean>, + BeanAnnotationHandler<Bean> { + @Override + public boolean isSingleton() { + return true; + } + + @Override + public Class<Bean> getAnnotation() { + return Bean.class; + } + + @Override + public String getName(Class clazz, AnnotatedElement annotatedElement) { + Bean bean = annotatedElement.getAnnotation(Bean.class); + if ("".equals(bean.id())) { + return null; + } + return bean.id(); + } + + @Override + public String getName(Object annotation) { + Bean bean = Bean.class.cast(annotation); + if ("".equals(bean.id())) { + return null; + } + return bean.id(); + } + + @Override + public void handleBeanAnnotation(AnnotatedElement annotatedElement, String id, + ContextEnricher contextEnricher, BeanEnricher beanEnricher) { + Bean annotation = annotatedElement.getAnnotation(Bean.class); + if (annotation.activation() != Activation.DEFAULT) { + beanEnricher.addAttribute("activation", annotation.activation().name().toLowerCase()); + } + if (annotation.scope() != Scope.SINGLETON) { + beanEnricher.addAttribute("scope", "prototype"); + } + if (annotation.dependsOn().length > 0) { + StringBuilder dependsOn = new StringBuilder(); + for (int i = 0; i < annotation.dependsOn().length; i++) { + if (i > 0) { + dependsOn.append(" "); + } + dependsOn.append(annotation.dependsOn()[i]); + } + beanEnricher.addAttribute("depends-on", dependsOn.toString()); + } + if(!annotation.initMethod().isEmpty()){ + beanEnricher.addAttribute("init-method", annotation.initMethod()); + } + if(!annotation.destroyMethod().isEmpty()){ + beanEnricher.addAttribute("destroy-method", annotation.destroyMethod()); + } + } +} Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java?rev=1781323&r1=1781322&r2=1781323&view=diff ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java (original) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java Wed Feb 1 22:00:29 2017 @@ -182,7 +182,7 @@ public class Blueprint implements Bluepr if (bean.needFieldInjection()) { writer.writeAttribute("ext", NS_EXT, "field-injection", "true"); } - if (bean.isPrototype) { + if (bean.isPrototype && !bean.attributes.containsKey("scope")) { writer.writeAttribute("scope", "prototype"); } Modified: aries/trunk/blueprint/plugin/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/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler?rev=1781323&r1=1781322&r2=1781323&view=diff ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler (original) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanAnnotationHandler Wed Feb 1 22:00:29 2017 @@ -23,3 +23,4 @@ org.apache.aries.blueprint.plugin.handle org.apache.aries.blueprint.plugin.handlers.javax.cdi.CdiTransactionFactory org.apache.aries.blueprint.plugin.handlers.config.ConfigAnnotationHandler org.apache.aries.blueprint.plugin.handlers.referencelistener.ReferenceListenerHandler +org.apache.aries.blueprint.plugin.handlers.bean.BeanHandler \ No newline at end of file Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanFinder URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanFinder?rev=1781323&r1=1781322&r2=1781323&view=diff ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanFinder (original) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.BeanFinder Wed Feb 1 22:00:29 2017 @@ -17,4 +17,5 @@ org.apache.aries.blueprint.plugin.handlers.spring.ComponentBeanFinder org.apache.aries.blueprint.plugin.handlers.javax.SingletonBeanFinder -org.apache.aries.blueprint.plugin.handlers.javax.NamedBeanFinder \ No newline at end of file +org.apache.aries.blueprint.plugin.handlers.javax.NamedBeanFinder +org.apache.aries.blueprint.plugin.handlers.bean.BeanHandler \ No newline at end of file Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.FactoryMethodFinder URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.FactoryMethodFinder?rev=1781323&r1=1781322&r2=1781323&view=diff ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.FactoryMethodFinder (original) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.FactoryMethodFinder Wed Feb 1 22:00:29 2017 @@ -16,3 +16,4 @@ # org.apache.aries.blueprint.plugin.handlers.javax.ProducesHandler +org.apache.aries.blueprint.plugin.handlers.bean.BeanHandler \ No newline at end of file Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.NamedLikeHandler URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.NamedLikeHandler?rev=1781323&r1=1781322&r2=1781323&view=diff ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.NamedLikeHandler (original) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.NamedLikeHandler Wed Feb 1 22:00:29 2017 @@ -17,4 +17,5 @@ org.apache.aries.blueprint.plugin.handlers.spring.ComponentAsNamed org.apache.aries.blueprint.plugin.handlers.spring.QualifierAsNamed -org.apache.aries.blueprint.plugin.handlers.javax.NamedHandler \ No newline at end of file +org.apache.aries.blueprint.plugin.handlers.javax.NamedHandler +org.apache.aries.blueprint.plugin.handlers.bean.BeanHandler \ No newline at end of file Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/BlueprintFileWriterTest.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/BlueprintFileWriterTest.java?rev=1781323&r1=1781322&r2=1781323&view=diff ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/BlueprintFileWriterTest.java (original) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/BlueprintFileWriterTest.java Wed Feb 1 22:00:29 2017 @@ -26,6 +26,10 @@ import org.apache.aries.blueprint.plugin import org.apache.aries.blueprint.plugin.test.ServiceA; import org.apache.aries.blueprint.plugin.test.ServiceB; import org.apache.aries.blueprint.plugin.test.ServiceD; +import org.apache.aries.blueprint.plugin.test.bean.BasicBean; +import org.apache.aries.blueprint.plugin.test.bean.BeanWithCallbackMethods; +import org.apache.aries.blueprint.plugin.test.bean.NamedBean; +import org.apache.aries.blueprint.plugin.test.bean.SimpleProducedBean; import org.apache.aries.blueprint.plugin.test.referencelistener.ReferenceListenerToProduceWithoutAnnotation; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.xbean.finder.ClassFinder; @@ -637,6 +641,84 @@ public class BlueprintFileWriterTest { validator.validate(xmlFile); } + @Test + public void beanAnnotationCreatesBasicBean() throws Exception { + Node bean = getBeanById("basicBean"); + assertXpathEquals(bean, "@class", BasicBean.class.getName()); + assertXpathDoesNotExist(bean, "@scope"); + assertXpathDoesNotExist(bean, "@activation"); + assertXpathDoesNotExist(bean, "@depends-on"); + assertXpathDoesNotExist(bean, "@init-method"); + assertXpathDoesNotExist(bean, "@destroy-method"); + assertXpathDoesNotExist(bean, "@factory-ref"); + assertXpathDoesNotExist(bean, "@factory-method"); + } + + @Test + public void beanAnnotationCreatesNamedBean() throws Exception { + Node bean = getBeanById("namedBean1"); + assertXpathEquals(bean, "@class", NamedBean.class.getName()); + assertXpathEquals(bean, "@activation", "eager"); + assertXpathEquals(bean, "@scope", "prototype"); + assertXpathDoesNotExist(bean, "@depends-on"); + assertXpathDoesNotExist(bean, "@init-method"); + assertXpathDoesNotExist(bean, "@destroy-method"); + assertXpathDoesNotExist(bean, "@factory-ref"); + assertXpathDoesNotExist(bean, "@factory-method"); + } + + @Test + public void beanAnnotationCreatesBeanWithCallbackMethods() throws Exception { + Node bean = getBeanById("beanWithCallbackMethods"); + assertXpathEquals(bean, "@class", BeanWithCallbackMethods.class.getName()); + assertXpathEquals(bean, "@scope", "prototype"); + assertXpathEquals(bean, "@activation", "lazy"); + assertXpathEquals(bean, "@depends-on", "basicBean namedBean1"); + assertXpathEquals(bean, "@init-method", "init"); + assertXpathEquals(bean, "@destroy-method", "destroy"); + assertXpathDoesNotExist(bean, "@factory-ref"); + assertXpathDoesNotExist(bean, "@factory-method"); + } + + @Test + public void beanAnnotationProducesSimpleBean() throws Exception { + Node bean = getBeanById("simpleProducedBean1"); + assertXpathEquals(bean, "@class", SimpleProducedBean.class.getName()); + assertXpathDoesNotExist(bean, "@scope"); + assertXpathDoesNotExist(bean, "@activation"); + assertXpathDoesNotExist(bean, "@depends-on"); + assertXpathDoesNotExist(bean, "@init-method"); + assertXpathDoesNotExist(bean, "@destroy-method"); + assertXpathEquals(bean, "@factory-ref", "basicBean"); + assertXpathEquals(bean, "@factory-method", "getBean1"); + } + + @Test + public void beanAnnotationProducesPrototypeBean() throws Exception { + Node bean = getBeanById("simpleProducedBean2"); + assertXpathEquals(bean, "@class", SimpleProducedBean.class.getName()); + assertXpathEquals(bean, "@activation", "eager"); + assertXpathEquals(bean, "@scope", "prototype"); + assertXpathDoesNotExist(bean, "@depends-on"); + assertXpathDoesNotExist(bean, "@init-method"); + assertXpathDoesNotExist(bean, "@destroy-method"); + assertXpathEquals(bean, "@factory-ref", "basicBean"); + assertXpathEquals(bean, "@factory-method", "getBean2"); + } + + @Test + public void beanAnnotationProducesMethodWithCallbacks() throws Exception { + Node bean = getBeanById("simpleProducedBean3"); + assertXpathEquals(bean, "@class", SimpleProducedBean.class.getName()); + assertXpathEquals(bean, "@scope", "prototype"); + assertXpathEquals(bean, "@activation", "lazy"); + assertXpathEquals(bean, "@depends-on", "simpleProducedBean1 simpleProducedBean2"); + assertXpathEquals(bean, "@init-method", "init1"); + assertXpathEquals(bean, "@destroy-method", "destroy1"); + assertXpathEquals(bean, "@factory-ref", "basicBean"); + assertXpathEquals(bean, "@factory-method", "getBean3"); + } + private void assertXpathDoesNotExist(Node node, String xpathExpression) throws XPathExpressionException { assertXpathEquals(node, "count(" + xpathExpression + ")", "0"); } Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BasicBean.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BasicBean.java?rev=1781323&view=auto ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BasicBean.java (added) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BasicBean.java Wed Feb 1 22:00:29 2017 @@ -0,0 +1,24 @@ +package org.apache.aries.blueprint.plugin.test.bean; + +import org.apache.aries.blueprint.annotation.bean.Activation; +import org.apache.aries.blueprint.annotation.bean.Bean; +import org.apache.aries.blueprint.annotation.bean.Scope; + +@Bean +public class BasicBean { + + @Bean(id = "simpleProducedBean1") + public SimpleProducedBean getBean1() { + return null; + } + + @Bean(id = "simpleProducedBean2", activation = Activation.EAGER, scope = Scope.PROTOTYPE) + public SimpleProducedBean getBean2() { + return null; + } + + @Bean(id = "simpleProducedBean3", activation = Activation.LAZY, scope = Scope.PROTOTYPE, dependsOn = {"simpleProducedBean1", "simpleProducedBean2"}, initMethod = "init1", destroyMethod = "destroy1") + public SimpleProducedBean getBean3() { + return null; + } +} Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BeanWithCallbackMethods.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BeanWithCallbackMethods.java?rev=1781323&view=auto ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BeanWithCallbackMethods.java (added) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/BeanWithCallbackMethods.java Wed Feb 1 22:00:29 2017 @@ -0,0 +1,14 @@ +package org.apache.aries.blueprint.plugin.test.bean; + +import org.apache.aries.blueprint.annotation.bean.Activation; +import org.apache.aries.blueprint.annotation.bean.Bean; +import org.apache.aries.blueprint.annotation.bean.Scope; + +@Bean(activation = Activation.LAZY, scope = Scope.PROTOTYPE, dependsOn = {"basicBean", "namedBean1"}, initMethod = "init", destroyMethod = "destroy") +public class BeanWithCallbackMethods { + public void init() { + } + + public void destroy() { + } +} Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/NamedBean.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/NamedBean.java?rev=1781323&view=auto ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/NamedBean.java (added) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/NamedBean.java Wed Feb 1 22:00:29 2017 @@ -0,0 +1,9 @@ +package org.apache.aries.blueprint.plugin.test.bean; + +import org.apache.aries.blueprint.annotation.bean.Activation; +import org.apache.aries.blueprint.annotation.bean.Bean; +import org.apache.aries.blueprint.annotation.bean.Scope; + +@Bean(id = "namedBean1", activation = Activation.EAGER, scope = Scope.PROTOTYPE) +public class NamedBean { +} Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/SimpleProducedBean.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/SimpleProducedBean.java?rev=1781323&view=auto ============================================================================== --- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/SimpleProducedBean.java (added) +++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/bean/SimpleProducedBean.java Wed Feb 1 22:00:29 2017 @@ -0,0 +1,9 @@ +package org.apache.aries.blueprint.plugin.test.bean; + +public class SimpleProducedBean { + public void init1() { + } + + public void destroy1() { + } +}