Author: alien11689
Date: Mon Jun  6 18:49:10 2016
New Revision: 1747053

URL: http://svn.apache.org/viewvc?rev=1747053&view=rev
Log:
[ARIES-1566] Support @Lazy annotation

Added:
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Activation.java
Modified:
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Generator.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.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/BeanWithSetters.java
    
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.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/MyProduced.java

Added: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Activation.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Activation.java?rev=1747053&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Activation.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Activation.java
 Mon Jun  6 18:49:10 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;
+
+public enum Activation {
+    EAGER,
+    LAZY;
+
+    @Override
+    public String toString() {
+        return name().toLowerCase();
+    }
+}

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java?rev=1747053&r1=1747052&r2=1747053&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/GenerateMojo.java
 Mon Jun  6 18:49:10 2016
@@ -60,7 +60,7 @@ public class GenerateMojo extends Abstra
      */
     @Parameter
     protected Set<String> namespaces;
-    
+
     @Component
     private BuildContext buildContext;
 
@@ -70,6 +70,15 @@ public class GenerateMojo extends Abstra
     @Parameter(defaultValue="autowire.xml")
     protected String generatedFileName;
 
+    /**
+     * Specifies the default activation setting that will be defined for 
components.
+     * Default is null, which indicates eager (blueprint default).
+     * If LAZY then default-activation will be set to lazy.
+     * If EAGER then default-activation will be explicitly set to eager.
+     */
+    @Parameter
+    protected Activation defaultActivation;
+
     public void execute() throws MojoExecutionException, MojoFailureException {
         if (scanPaths.size() == 0 || scanPaths.iterator().next() == null) {
             throw new MojoExecutionException("Configuration scanPaths must be 
set");
@@ -104,7 +113,7 @@ public class GenerateMojo extends Abstra
         System.out.println("Generating blueprint to " + file);
 
         OutputStream fos = buildContext.newFileOutputStream(file);
-        new Generator(context, fos, namespaces).generate();
+        new Generator(context, fos, namespaces, defaultActivation).generate();
         fos.close();
     }
 

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Generator.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Generator.java?rev=1747053&r1=1747052&r2=1747053&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Generator.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/Generator.java
 Mon Jun  6 18:49:10 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
@@ -48,16 +48,15 @@ public class Generator implements Proper
     public static final String NS_TX = 
"http://aries.apache.org/xmlns/transactions/v1.2.0";;
     public static final String NS_TX2 = 
"http://aries.apache.org/xmlns/transactions/v2.0.0";;
 
-    private Context context;
-    private XMLStreamWriter writer;
-    private Set<String> namespaces;
+    private final Context context;
+    private final XMLStreamWriter writer;
+    private final Set<String> namespaces;
+    private final Activation defaultActivation;
 
-    public Generator(Context context, OutputStream os, Set<String> namespaces) 
throws XMLStreamException {
+    public Generator(Context context, OutputStream os, Set<String> namespaces, 
Activation defaultActivation) throws XMLStreamException {
         this.context = context;
-        this.namespaces = namespaces;
-        if (this.namespaces == null) {
-            this.namespaces = new HashSet<String>(Arrays.asList(NS_TX2, 
NS_JPA2));
-        }
+        this.namespaces = namespaces != null ? namespaces :  new 
HashSet<>(Arrays.asList(NS_TX2, NS_JPA2));
+        this.defaultActivation = defaultActivation;
         XMLOutputFactory factory = XMLOutputFactory.newInstance();
         writer = factory.createXMLStreamWriter(os);
     }
@@ -129,6 +128,9 @@ public class Generator implements Proper
             String prefix = getPrefixForNamesapace(namespace);
             writer.writeNamespace(prefix, namespace);
         }
+        if (defaultActivation != null) {
+            writer.writeAttribute("default-activation", 
defaultActivation.name().toLowerCase());
+        }
     }
 
     private String getPrefixForNamesapace(String namespace) {
@@ -145,12 +147,15 @@ public class Generator implements Proper
         writer.writeStartElement("bean");
         writer.writeAttribute("id", bean.id);
         writer.writeAttribute("class", bean.clazz.getName());
-        if(bean.needFieldInjection()) {
+        if (bean.needFieldInjection()) {
             writer.writeAttribute("ext", NS_EXT, "field-injection", "true");
         }
         if (bean.isPrototype) {
             writer.writeAttribute("scope", "prototype");
         }
+        if (bean.activation != null) {
+            writer.writeAttribute("activation", bean.activation.toString());
+        }
         if (bean instanceof ProducedBean) {
             writeFactory((ProducedBean) bean);
         }
@@ -178,7 +183,7 @@ public class Generator implements Proper
     }
 
     private void writeTransactional(TransactionalDef transactionDef)
-            throws XMLStreamException {
+        throws XMLStreamException {
         if (transactionDef != null) {
             writer.writeCharacters("    ");
             writer.writeEmptyElement("tx", "transaction", NS_TX);

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java?rev=1747053&r1=1747052&r2=1747053&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
 Mon Jun  6 18:49:10 2016
@@ -18,10 +18,12 @@
  */
 package org.apache.aries.blueprint.plugin.model;
 
+import org.apache.aries.blueprint.plugin.Activation;
 import org.apache.aries.blueprint.plugin.model.service.ServiceProvider;
 import org.ops4j.pax.cdi.api.OsgiService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
@@ -32,6 +34,7 @@ import javax.inject.Singleton;
 import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceUnit;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -52,11 +55,13 @@ public class Bean extends BeanRef {
     public Set<TransactionalDef> transactionDefs = new HashSet<>();
     public boolean isPrototype;
     public List<ServiceProvider> serviceProviders = new ArrayList<>();
+    public Activation activation;
 
     public Bean(Class<?> clazz) {
         super(clazz, BeanRef.getBeanName(clazz));
         Introspector introspector = new Introspector(clazz);
 
+        activation = getActivation(clazz);
         initMethod = findMethodAnnotatedWith(introspector, 
PostConstruct.class);
         destroyMethod = findMethodAnnotatedWith(introspector, 
PreDestroy.class);
 
@@ -70,6 +75,14 @@ public class Bean extends BeanRef {
         interpretServiceProvider();
     }
 
+    protected Activation getActivation(AnnotatedElement annotatedElement) {
+        Lazy lazy = annotatedElement.getAnnotation(Lazy.class);
+        if (lazy == null) {
+            return null;
+        }
+        return lazy.value() ? Activation.LAZY : Activation.EAGER;
+    }
+
     private void interpretServiceProvider() {
         ServiceProvider serviceProvider = ServiceProvider.fromBean(this);
         if (serviceProvider != null) {

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java?rev=1747053&r1=1747052&r2=1747053&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
 Mon Jun  6 18:49:10 2016
@@ -19,6 +19,8 @@
 package org.apache.aries.blueprint.plugin.model;
 
 
+import org.apache.aries.blueprint.plugin.Activation;
+
 import java.lang.reflect.Method;
 
 public class ProducedBean extends Bean {
@@ -31,6 +33,7 @@ public class ProducedBean extends Bean {
         this.factoryBean = factoryBean;
         this.factoryMethod = factoryMethod.getName();
         this.producingMethod = factoryMethod;
+        overrideActivationIfNeeded(factoryMethod);
     }
 
     public ProducedBean(Class<?> clazz, String id, BeanRef factoryBean, Method 
factoryMethod) {
@@ -39,6 +42,14 @@ public class ProducedBean extends Bean {
         this.factoryBean = factoryBean;
         this.factoryMethod = factoryMethod.getName();
         this.producingMethod = factoryMethod;
+        overrideActivationIfNeeded(factoryMethod);
+    }
+
+    private void overrideActivationIfNeeded(Method factoryMethod) {
+        Activation methodActivation = getActivation(factoryMethod);
+        if (methodActivation != null) {
+            this.activation = methodActivation;
+        }
     }
 
     public void setSingleton() {

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=1747053&r1=1747052&r2=1747053&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
 Mon Jun  6 18:49:10 2016
@@ -66,7 +66,7 @@ public class GeneratorTest {
         context.resolve();
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         Set<String> namespaces = new 
HashSet<String>(Arrays.asList(Generator.NS_JPA, Generator.NS_TX));
-        new Generator(context, os, namespaces).generate();
+        new Generator(context, os, namespaces, null).generate();
         System.out.println(os.toString("UTF-8"));
 
         document = readToDocument(os);
@@ -297,6 +297,34 @@ public class GeneratorTest {
         assertXpathEquals(reference3, "@component-name", "B3Ref");
     }
 
+    @Test
+    public void testLazyWithTrueBeanHasActivationEager() throws Exception {
+        Node bean = getBeanById("beanWithSetters");
+
+        assertXpathEquals(bean, "@activation", "eager");
+    }
+
+    @Test
+    public void testLazyBeanHasActivationLazy() throws Exception {
+        Node bean = getBeanById("myBean1");
+
+        assertXpathEquals(bean, "@activation", "lazy");
+    }
+
+    @Test
+    public void testBeanWithoutLazyAnnotationHasNotActivationAttribute() 
throws Exception {
+        Node bean1 = getBeanById("myBean3");
+
+        assertXpathDoesNotExist(bean1, "@activation");
+    }
+
+    @Test
+    public void testLazyProducedBeanOverriddenByFactoryMethodAnnotation() 
throws Exception {
+        Node bean = getBeanById("producedEager");
+
+        assertXpathEquals(bean, "@activation", "eager");
+    }
+
     private void assertXpathDoesNotExist(Node node, String xpathExpression) 
throws XPathExpressionException {
         assertXpathEquals(node, "count(" + xpathExpression + ")", "0");
     }

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithSetters.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithSetters.java?rev=1747053&r1=1747052&r2=1747053&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithSetters.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/BeanWithSetters.java
 Mon Jun  6 18:49:10 2016
@@ -22,12 +22,14 @@ import org.ops4j.pax.cdi.api.OsgiService
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Lazy;
 
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
 @Singleton
+@Lazy(false)
 public class BeanWithSetters {
 
     @Inject

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java?rev=1747053&r1=1747052&r2=1747053&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java
 Mon Jun  6 18:49:10 2016
@@ -18,6 +18,8 @@
  */
 package org.apache.aries.blueprint.plugin.test;
 
+import org.springframework.context.annotation.Lazy;
+
 import javax.annotation.PostConstruct;
 import javax.inject.Singleton;
 import javax.transaction.Transactional;
@@ -25,6 +27,7 @@ import javax.transaction.Transactional.T
 
 @Singleton
 @Transactional(value=TxType.REQUIRES_NEW)
+@Lazy
 public class MyBean1 extends ParentBean {
 
     public void overridenInit() {

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=1747053&r1=1747052&r2=1747053&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
 Mon Jun  6 18:49:10 2016
@@ -18,6 +18,8 @@
  */
 package org.apache.aries.blueprint.plugin.test;
 
+import org.springframework.context.annotation.Lazy;
+
 import javax.enterprise.inject.Produces;
 import javax.inject.Named;
 import javax.inject.Singleton;
@@ -38,4 +40,12 @@ public class MyFactoryNamedBean {
         return new MyProduced("My message");
     }
 
+    @Produces
+    @Named("producedEager")
+    @Singleton
+    @Lazy(false)
+    public MyProduced createBean2AsEager() {
+        return new MyProduced("My message");
+    }
+
 }

Modified: 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProduced.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProduced.java?rev=1747053&r1=1747052&r2=1747053&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProduced.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProduced.java
 Mon Jun  6 18:49:10 2016
@@ -18,8 +18,11 @@
  */
 package org.apache.aries.blueprint.plugin.test;
 
+import org.springframework.context.annotation.Lazy;
+
 import javax.inject.Inject;
 
+@Lazy
 public class MyProduced {
     private String message;
     


Reply via email to