aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alien11...@apache.org
Subject svn commit: r1781668 - in /aries/trunk/blueprint/plugin/blueprint-maven-plugin/src: main/java/org/apache/aries/blueprint/plugin/handlers/bean/ main/java/org/apache/aries/blueprint/plugin/model/ test/java/org/apache/aries/blueprint/plugin/model/
Date Sat, 04 Feb 2017 13:30:43 GMT
Author: alien11689
Date: Sat Feb  4 13:30:43 2017
New Revision: 1781668

URL: http://svn.apache.org/viewvc?rev=1781668&view=rev
Log:
[MAINTENANCE] Simplify bean model in BMP

Modified:
    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/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
    aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java
    aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanRef.java
    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/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
    aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BlueprintTest.java

Modified: 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=1781668&r1=1781667&r2=1781668&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/BeanHandler.java
(original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/bean/BeanHandler.java
Sat Feb  4 13:30:43 2017
@@ -19,7 +19,7 @@ public class BeanHandler implements
         BeanAnnotationHandler<Bean> {
     @Override
     public boolean isSingleton() {
-        return true;
+        return false;
     }
 
     @Override
@@ -52,9 +52,7 @@ public class BeanHandler implements
         if (annotation.activation() != Activation.DEFAULT) {
             beanEnricher.addAttribute("activation", annotation.activation().name().toLowerCase());
         }
-        if (annotation.scope() != Scope.SINGLETON) {
-            beanEnricher.addAttribute("scope", "prototype");
-        }
+        beanEnricher.addAttribute("scope", annotation.scope() == Scope.SINGLETON ? "singleton"
: "prototype");
         if (annotation.dependsOn().length > 0) {
             StringBuilder dependsOn = new StringBuilder();
             for (int i = 0; i < annotation.dependsOn().length; i++) {
@@ -65,10 +63,10 @@ public class BeanHandler implements
             }
             beanEnricher.addAttribute("depends-on", dependsOn.toString());
         }
-        if(!annotation.initMethod().isEmpty()){
+        if (!annotation.initMethod().isEmpty()) {
             beanEnricher.addAttribute("init-method", annotation.initMethod());
         }
-        if(!annotation.destroyMethod().isEmpty()){
+        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/Bean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java?rev=1781668&r1=1781667&r2=1781668&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
(original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
Sat Feb  4 13:30:43 2017
@@ -45,11 +45,13 @@ import java.util.TreeSet;
 import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findName;
 import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findValue;
 
-class Bean extends BeanRef implements BeanEnricher {
-    public SortedSet<Property> properties = new TreeSet<>();
+class Bean extends BeanRef implements BeanEnricher, XmlWriter {
+
+    static final String NS_EXT = "http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0";
+
+    SortedSet<Property> properties = new TreeSet<>();
     List<Argument> constructorArguments = new ArrayList<>();
-    public boolean isPrototype;
-    public final Map<String, String> attributes = new HashMap<>();
+    final Map<String, String> attributes = new HashMap<>();
     final Map<String, XmlWriter> beanContentWriters = new HashMap<>();
     protected final ContextEnricher contextEnricher;
 
@@ -58,7 +60,7 @@ class Bean extends BeanRef implements Be
         this.contextEnricher = contextEnricher;
         Introspector introspector = new Introspector(clazz);
 
-        this.isPrototype = isPrototype(clazz);
+        setScope(clazz);
 
         setQualifiersFromAnnotations(clazz.getAnnotations());
 
@@ -69,6 +71,10 @@ class Bean extends BeanRef implements Be
         handleMethodsAnnotation(introspector);
     }
 
+    private void setScope(Class<?> clazz) {
+        attributes.put("scope", findSingleton(clazz) ? "singleton" : "prototype");
+    }
+
     void resolve(BlueprintRegistry blueprintRegistry) {
         resolveArguments(blueprintRegistry);
         resolveFields(blueprintRegistry);
@@ -102,10 +108,6 @@ class Bean extends BeanRef implements Be
         }
     }
 
-    private boolean isPrototype(Class<?> clazz) {
-        return !findSingleton(clazz);
-    }
-
     private boolean findSingleton(Class clazz) {
         for (Class<?> singletonAnnotation : Handlers.SINGLETONS) {
             if (clazz.getAnnotation(singletonAnnotation) != null) {
@@ -153,7 +155,7 @@ class Bean extends BeanRef implements Be
         return false;
     }
 
-    protected void resolveArguments(BlueprintRegistry blueprintRegistry, Class[] parameterTypes,
Annotation[][] parameterAnnotations) {
+    void resolveArguments(BlueprintRegistry blueprintRegistry, Class[] parameterTypes, Annotation[][]
parameterAnnotations) {
         for (int i = 0; i < parameterTypes.length; ++i) {
             Annotation[] annotations = parameterAnnotations[i];
             String value = findValue(annotations);
@@ -195,19 +197,19 @@ class Bean extends BeanRef implements Be
         return clazz.getName();
     }
 
-    void writeProperties(XMLStreamWriter writer) throws XMLStreamException {
+    private void writeProperties(XMLStreamWriter writer) throws XMLStreamException {
         for (Property property : properties) {
             property.write(writer);
         }
     }
 
-    void writeArguments(XMLStreamWriter writer) throws XMLStreamException {
+    private void writeArguments(XMLStreamWriter writer) throws XMLStreamException {
         for (Argument argument : constructorArguments) {
             argument.write(writer);
         }
     }
 
-    boolean needFieldInjection() {
+    private boolean needFieldInjection() {
         for (Property property : properties) {
             if (property.isField) {
                 return true;
@@ -226,9 +228,37 @@ class Bean extends BeanRef implements Be
         beanContentWriters.put(id, blueprintWriter);
     }
 
-    void writeCustomContent(XMLStreamWriter writer) throws XMLStreamException {
+    private void writeCustomContent(XMLStreamWriter writer) throws XMLStreamException {
         for (XmlWriter xmlWriter : beanContentWriters.values()) {
             xmlWriter.write(writer);
         }
     }
+
+    private void writeAttributes(XMLStreamWriter writer) throws XMLStreamException {
+        for (Map.Entry<String, String> entry : attributes.entrySet()) {
+            if ("scope".equals(entry.getKey()) && "singleton".equals(entry.getValue()))
{
+                continue;
+            }
+            writer.writeAttribute(entry.getKey(), entry.getValue());
+        }
+    }
+
+    @Override
+    public void write(XMLStreamWriter writer) throws XMLStreamException {
+        writeBeanStart(writer);
+        writeCustomContent(writer);
+        writeArguments(writer);
+        writeProperties(writer);
+        writer.writeEndElement();
+    }
+
+    private void writeBeanStart(XMLStreamWriter writer) throws XMLStreamException {
+        writer.writeStartElement("bean");
+        writer.writeAttribute("id", id);
+        writer.writeAttribute("class", clazz.getName());
+        if (needFieldInjection()) {
+            writer.writeAttribute("ext", NS_EXT, "field-injection", "true");
+        }
+        writeAttributes(writer);
+    }
 }

Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java?rev=1781668&r1=1781667&r2=1781668&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java
(original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java
Sat Feb  4 13:30:43 2017
@@ -26,9 +26,7 @@ import org.apache.aries.blueprint.plugin
 import java.lang.reflect.Method;
 
 class BeanFromFactory extends Bean {
-    String factoryMethod;
-     BeanRef factoryBean;
-    private Method producingMethod;
+    private final Method producingMethod;
 
     BeanFromFactory(Class<?> clazz, BeanRef factoryBean, Method factoryMethod, ContextEnricher
contextEnricher) {
         this(clazz, null, factoryBean, factoryMethod, contextEnricher);
@@ -39,10 +37,10 @@ class BeanFromFactory extends Bean {
         if (id != null) {
             this.id = id;
         }
-        this.factoryBean = factoryBean;
-        this.factoryMethod = factoryMethod.getName();
         this.producingMethod = factoryMethod;
         handleCustomBeanAnnotations();
+        attributes.put("factory-ref", factoryBean.id);
+        attributes.put("factory-method", producingMethod.getName());
     }
 
     private void handleCustomBeanAnnotations() {
@@ -55,7 +53,7 @@ class BeanFromFactory extends Bean {
     }
 
     void setSingleton() {
-        this.isPrototype = false;
+        attributes.put("scope", "singleton");
     }
 
     @Override

Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanRef.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanRef.java?rev=1781668&r1=1781667&r2=1781668&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanRef.java
(original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanRef.java
Sat Feb  4 13:30:43 2017
@@ -31,7 +31,7 @@ import java.util.Map;
 class BeanRef implements Comparable<BeanRef> {
     public String id;
     public Class<?> clazz;
-    Map<Class<? extends Annotation>, Annotation> qualifiers = new HashMap<>();
+    private Map<Class<? extends Annotation>, Annotation> qualifiers = new HashMap<>();
 
     /**
      * @param clazz interface or implementation class
@@ -60,7 +60,7 @@ class BeanRef implements Comparable<Bean
         setQualifiersFromAnnotations(annotations);
     }
 
-    protected void setQualifiersFromAnnotations(Annotation[] annotations) {
+    void setQualifiersFromAnnotations(Annotation[] annotations) {
         for (Annotation ann : annotations) {
             if (isQualifier(ann) != null) {
                 this.qualifiers.put(ann.annotationType(), ann);

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=1781668&r1=1781667&r2=1781668&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
Sat Feb  4 13:30:43 2017
@@ -35,9 +35,10 @@ import java.util.Map;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
+import static org.apache.aries.blueprint.plugin.model.Bean.NS_EXT;
+
 public class Blueprint implements BlueprintRegistry, ContextEnricher, XmlWriter {
     private static final String NS_BLUEPRINT = "http://www.osgi.org/xmlns/blueprint/v1.0.0";
-    private static final String NS_EXT = "http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0";
 
     private Registry registry = new Registry();
     private final Map<String, XmlWriter> blueprintWriters = new HashMap<>();
@@ -153,11 +154,7 @@ public class Blueprint implements Bluepr
         writeBlueprint(writer);
 
         for (Bean bean : getBeans()) {
-            writeBeanStart(writer, bean);
-            bean.writeCustomContent(writer);
-            bean.writeArguments(writer);
-            bean.writeProperties(writer);
-            writer.writeEndElement();
+            bean.write(writer);
         }
 
         for (XmlWriter bw : getBlueprintWriters().values()) {
@@ -176,31 +173,6 @@ public class Blueprint implements Bluepr
         }
     }
 
-    private void writeBeanStart(XMLStreamWriter writer, Bean bean) throws XMLStreamException
{
-        writer.writeStartElement("bean");
-        writer.writeAttribute("id", bean.id);
-        writer.writeAttribute("class", bean.clazz.getName());
-        if (bean.needFieldInjection()) {
-            writer.writeAttribute("ext", NS_EXT, "field-injection", "true");
-        }
-        if (bean.isPrototype && !bean.attributes.containsKey("scope")) {
-            writer.writeAttribute("scope", "prototype");
-        }
-
-        Map<String, String> attributes = bean.attributes;
-        for (Map.Entry<String, String> entry : attributes.entrySet()) {
-            writer.writeAttribute(entry.getKey(), entry.getValue());
-        }
-        if (bean instanceof BeanFromFactory) {
-            writeFactory(writer, (BeanFromFactory) bean);
-        }
-    }
-
-    private void writeFactory(XMLStreamWriter writer, BeanFromFactory bean) throws XMLStreamException
{
-        writer.writeAttribute("factory-ref", bean.factoryBean.id);
-        writer.writeAttribute("factory-method", bean.factoryMethod);
-    }
-
     public boolean shouldBeGenerated() {
         return !getBeans().isEmpty();
     }

Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java?rev=1781668&r1=1781667&r2=1781668&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
(original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
Sat Feb  4 13:30:43 2017
@@ -18,16 +18,7 @@
  */
 package org.apache.aries.blueprint.plugin.model;
 
-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 com.google.common.collect.Sets;
 import org.apache.aries.blueprint.plugin.BlueprintConfigurationImpl;
 import org.apache.aries.blueprint.plugin.bad.BadBean1;
 import org.apache.aries.blueprint.plugin.bad.BadBean2;
@@ -44,12 +35,19 @@ import org.apache.aries.blueprint.plugin
 import org.apache.aries.blueprint.plugin.test.ServiceAImpl1;
 import org.junit.Test;
 
-import com.google.common.collect.Sets;
+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;
 
 public class BeanTest {
     private static final String NS_JPA1 = "http://aries.apache.org/xmlns/jpa/v1.1.0";
     private static final String NS_TX1 = "http://aries.apache.org/xmlns/transactions/v1.1.0";
-    
+
     private final Set<String> namespaces = new HashSet<String>(Arrays.asList(NS_JPA1,
NS_TX1));
     private final BlueprintConfigurationImpl blueprintConfiguration = new BlueprintConfigurationImpl(namespaces,
null, null);
     private final Blueprint blueprint = new Blueprint(blueprintConfiguration);
@@ -63,18 +61,18 @@ public class BeanTest {
         assertEquals(2, getPersistenceFields(bean).size());
         assertEquals(Sets.newHashSet("em", "emf"), getPersistenceFields(bean));
         assertEquals(1, bean.properties.size());
-        assertFalse(bean.isPrototype);
+        assertEquals("singleton", bean.attributes.get("scope"));
         Property prop = bean.properties.iterator().next();
         assertEquals("bean2", prop.name);
         assertEquals("serviceA", prop.ref);
 
         Set<TransactionalDef> expectedTxs = Sets.newHashSet(new TransactionalDef("*",
"RequiresNew"),
-            new TransactionalDef("txNotSupported", "NotSupported"),
-            new TransactionalDef("txMandatory", "Mandatory"),
-            new TransactionalDef("txNever", "Never"),
-            new TransactionalDef("txRequired", "Required"),
-            new TransactionalDef("txOverridenWithRequiresNew", "RequiresNew"),
-            new TransactionalDef("txSupports", "Supports"));
+                new TransactionalDef("txNotSupported", "NotSupported"),
+                new TransactionalDef("txMandatory", "Mandatory"),
+                new TransactionalDef("txNever", "Never"),
+                new TransactionalDef("txRequired", "Required"),
+                new TransactionalDef("txOverridenWithRequiresNew", "RequiresNew"),
+                new TransactionalDef("txSupports", "Supports"));
         assertEquals(expectedTxs, getTransactionalDefs(bean));
     }
 
@@ -86,15 +84,15 @@ public class BeanTest {
         assertEquals("myBean3", bean.id); // Name derived from class name
         assertEquals("There should be no persistence fields", 0, getPersistenceFields(bean).size());
         assertEquals(5, bean.properties.size());
-        assertTrue(bean.isPrototype);
+        assertEquals("prototype", bean.attributes.get("scope"));
 
         Set<TransactionalDef> expectedTxs = Sets.newHashSet(new TransactionalDef("*",
"RequiresNew"),
-            new TransactionalDef("txNotSupported", "NotSupported"),
-            new TransactionalDef("txMandatory", "Mandatory"),
-            new TransactionalDef("txNever", "Never"),
-            new TransactionalDef("txRequired", "Required"),
-            new TransactionalDef("txRequiresNew", "RequiresNew"),
-            new TransactionalDef("txSupports", "Supports"));
+                new TransactionalDef("txNotSupported", "NotSupported"),
+                new TransactionalDef("txMandatory", "Mandatory"),
+                new TransactionalDef("txNever", "Never"),
+                new TransactionalDef("txRequired", "Required"),
+                new TransactionalDef("txRequiresNew", "RequiresNew"),
+                new TransactionalDef("txSupports", "Supports"));
         assertEquals(expectedTxs, getTransactionalDefs(bean));
     }
 
@@ -105,10 +103,10 @@ public class BeanTest {
         String definedName = ServiceAImpl1.class.getAnnotation(Named.class).value();
         assertEquals("my1", definedName);
         assertEquals("Name should be defined using @Named", definedName, bean.id);
-        assertEquals("There should be no persistence fields", 0,getPersistenceFields(bean).size());
+        assertEquals("There should be no persistence fields", 0, getPersistenceFields(bean).size());
         assertTrue("There should be no transaction definition", getTransactionalDefs(bean).isEmpty());
         assertEquals("There should be no properties", 0, bean.properties.size());
-        assertTrue(bean.isPrototype);
+        assertEquals("prototype", bean.attributes.get("scope"));
     }
 
     @Test
@@ -118,7 +116,7 @@ public class BeanTest {
         Property bcProp = bean.properties.iterator().next();
         assertEquals("bundleContext", bcProp.name);
         assertEquals("blueprintBundleContext", bcProp.ref);
-        assertFalse(bean.isPrototype);
+        assertEquals("singleton", bean.attributes.get("scope"));
 
         Set<TransactionalDef> expectedTxs = Sets.newHashSet(new TransactionalDef("txWithoutClassAnnotation",
"Supports"));
         assertEquals(expectedTxs, getTransactionalDefs(bean));
@@ -200,7 +198,7 @@ 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, blueprint);

Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BlueprintTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BlueprintTest.java?rev=1781668&r1=1781667&r2=1781668&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BlueprintTest.java
(original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BlueprintTest.java
Sat Feb  4 13:30:43 2017
@@ -87,8 +87,8 @@ public class BlueprintTest {
         Blueprint blueprint = new Blueprint(blueprintConfiguration, MyFactoryBean.class);
         BeanFromFactory matching = (BeanFromFactory) blueprint.getMatching(new BeanRef(MyProduced.class));
         Assert.assertEquals(MyProduced.class, matching.clazz);
-        Assert.assertEquals("myFactoryBean", matching.factoryBean.id);
-        Assert.assertEquals("create", matching.factoryMethod);
+        Assert.assertEquals("myFactoryBean", matching.attributes.get("factory-ref"));
+        Assert.assertEquals("create", matching.attributes.get("factory-method"));
     }
 
 }



Mime
View raw message