You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by al...@apache.org on 2016/09/10 19:16:59 UTC

svn commit: r1760205 - in /aries/trunk/blueprint/blueprint-maven-plugin: ./ src/main/java/org/apache/aries/blueprint/plugin/cdi/ src/main/resources/META-INF/services/ src/test/java/org/apache/aries/blueprint/plugin/ src/test/java/org/apache/aries/bluep...

Author: alien11689
Date: Sat Sep 10 19:16:59 2016
New Revision: 1760205

URL: http://svn.apache.org/viewvc?rev=1760205&view=rev
Log:
[ARIES-1611] Add handler for javax.transaction.cdi.Transactional annotation to BMP

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/cdi/
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/cdi/CdiTransactionFactory.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/CdiTransactionalAnnotatedBean.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.MethodAnnotationHandler
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java

Modified: aries/trunk/blueprint/blueprint-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/pom.xml?rev=1760205&r1=1760204&r2=1760205&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/pom.xml (original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/pom.xml Sat Sep 10 19:16:59 2016
@@ -244,6 +244,13 @@
             <version>3.1.4.RELEASE</version>
         </dependency>
 
+        <!-- For the CDI annotations -->
+        <dependency>
+            <groupId>javax.transaction.cdi</groupId>
+            <artifactId>javax.transaction.cdi-api</artifactId>
+            <version>1.2-b03</version>
+        </dependency>
+
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>

Added: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/cdi/CdiTransactionFactory.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/cdi/CdiTransactionFactory.java?rev=1760205&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/cdi/CdiTransactionFactory.java (added)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/cdi/CdiTransactionFactory.java Sat Sep 10 19:16:59 2016
@@ -0,0 +1,126 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <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
+ * 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.cdi;
+
+import com.google.common.base.CaseFormat;
+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.MethodAnnotationHandler;
+import org.apache.aries.blueprint.plugin.spi.XmlWriter;
+
+import javax.transaction.cdi.Transactional;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Set;
+
+public class CdiTransactionFactory implements BeanAnnotationHandler<Transactional>, MethodAnnotationHandler<Transactional> {
+    private static final String PATTERN_NS_TX1 = "http\\:\\/\\/aries\\.apache\\.org\\/xmlns\\/transactions\\/v1\\.(.)\\.(.)";
+    private static final String PATTERN_NS_TX2 = "http\\:\\/\\/aries\\.apache\\.org\\/xmlns\\/transactions\\/v2\\.(.)\\.(.)";
+
+    private String getTransactionTypeName(Transactional transactional) {
+        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, transactional.value().name());
+    }
+
+    @Override
+    public Class<Transactional> getAnnotation() {
+        return Transactional.class;
+    }
+
+    @Override
+    public void handleMethodAnnotation(Class<?> clazz, List<Method> methods, ContextEnricher contextEnricher, BeanEnricher beanEnricher) {
+        final String nsTx1 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX1);
+        if (nsTx1 != null) {
+            enableAnnotations(contextEnricher, nsTx1);
+            for (final Method method : methods) {
+                final Transactional transactional = method.getAnnotation(Transactional.class);
+                final String transactionTypeName = getTransactionTypeName(transactional);
+                final String name = method.getName();
+                beanEnricher.addBeanContentWriter("javax.transactional.method/" + clazz.getName() + "/" + name + "/" + transactionTypeName, new XmlWriter() {
+                    @Override
+                    public void write(XMLStreamWriter writer) throws XMLStreamException {
+                        writer.writeEmptyElement("transaction");
+                        writer.writeDefaultNamespace(nsTx1);
+                        writer.writeAttribute("method", name);
+                        writer.writeAttribute("value", transactionTypeName);
+                    }
+                });
+            }
+        }
+        final String nsTx2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX2);
+        if (nsTx2 != null) {
+            enableTransactionsTx2(contextEnricher, nsTx2);
+        }
+    }
+
+    private void enableAnnotations(ContextEnricher contextEnricher, final String namespace) {
+        contextEnricher.addBlueprintContentWriter("transaction/ennable-annotation", new XmlWriter() {
+            @Override
+            public void write(XMLStreamWriter writer) throws XMLStreamException {
+                writer.writeEmptyElement("enable-annotations");
+                writer.writeDefaultNamespace(namespace);
+            }
+        });
+    }
+
+    @Override
+    public void handleBeanAnnotation(AnnotatedElement annotatedElement, String id, ContextEnricher contextEnricher, BeanEnricher beanEnricher) {
+        final String nsTx1 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX1);
+        if (nsTx1 != null) {
+            enableAnnotations(contextEnricher, nsTx1);
+            final Transactional transactional = annotatedElement.getAnnotation(Transactional.class);
+            final String transactionTypeName = getTransactionTypeName(transactional);
+            beanEnricher.addBeanContentWriter("javax.transactional.method/" + annotatedElement + "/*/" + transactionTypeName, new XmlWriter() {
+                @Override
+                public void write(XMLStreamWriter writer) throws XMLStreamException {
+                    writer.writeEmptyElement("transaction");
+                    writer.writeDefaultNamespace(nsTx1);
+                    writer.writeAttribute("method", "*");
+                    writer.writeAttribute("value", transactionTypeName);
+                }
+            });
+        }
+        final String nsTx2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX2);
+        if (nsTx2 != null) {
+            enableTransactionsTx2(contextEnricher, nsTx2);
+        }
+    }
+
+    private void enableTransactionsTx2(ContextEnricher contextEnricher, final String namespace) {
+        contextEnricher.addBlueprintContentWriter("transaction/ennable-annotation", new XmlWriter() {
+            @Override
+            public void write(XMLStreamWriter writer) throws XMLStreamException {
+                writer.writeEmptyElement("enable");
+                writer.writeDefaultNamespace(namespace);
+            }
+        });
+    }
+
+    private String getNamespaceByPattern(Set<String> namespaces, String pattern) {
+        for (String namespace : namespaces) {
+            if (namespace.matches(pattern)) {
+                return namespace;
+            }
+        }
+        return null;
+    }
+}
\ No newline at end of file

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=1760205&r1=1760204&r2=1760205&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 Sat Sep 10 19:16:59 2016
@@ -19,4 +19,5 @@ org.apache.aries.blueprint.plugin.pax.Os
 org.apache.aries.blueprint.plugin.spring.LazyAttributeResolver
 org.apache.aries.blueprint.plugin.spring.DependsOnAttributeResolver
 org.apache.aries.blueprint.plugin.spring.SpringTransactionalFactory
-org.apache.aries.blueprint.plugin.javax.JavaxTransactionFactory
\ No newline at end of file
+org.apache.aries.blueprint.plugin.javax.JavaxTransactionFactory
+org.apache.aries.blueprint.plugin.cdi.CdiTransactionFactory
\ No newline at end of file

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler?rev=1760205&r1=1760204&r2=1760205&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler (original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/resources/META-INF/services/org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler Sat Sep 10 19:16:59 2016
@@ -18,4 +18,5 @@
 org.apache.aries.blueprint.plugin.javax.PostConstructHandler
 org.apache.aries.blueprint.plugin.javax.PreDestroyHandler
 org.apache.aries.blueprint.plugin.spring.SpringTransactionalFactory
-org.apache.aries.blueprint.plugin.javax.JavaxTransactionFactory
\ No newline at end of file
+org.apache.aries.blueprint.plugin.javax.JavaxTransactionFactory
+org.apache.aries.blueprint.plugin.cdi.CdiTransactionFactory
\ No newline at end of file

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=1760205&r1=1760204&r2=1760205&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 Sat Sep 10 19:16:59 2016
@@ -109,6 +109,26 @@ public class GeneratorTest {
     }
 
     @Test
+    public void testGenerateCDITransactional() throws Exception {
+        Node bean1 = getBeanById("cdiTransactionalAnnotatedBean");
+
+        NodeList txs = (NodeList) xpath.evaluate("transaction", bean1, XPathConstants.NODESET);
+        Set<TransactionalDef> defs = new HashSet<TransactionalDef>();
+        for (int i = 0; i < txs.getLength(); ++i) {
+            Node tx = txs.item(i);
+            defs.add(new TransactionalDef(xpath.evaluate("@method", tx), xpath.evaluate("@value", tx)));
+        }
+        Set<TransactionalDef> expectedDefs = 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"));
+        assertEquals(expectedDefs, defs);
+    }
+
+    @Test
     public void testGeneratePersistenceContext() throws Exception {
         Node bean1 = getBeanById("myBean1");
 

Added: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/CdiTransactionalAnnotatedBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/CdiTransactionalAnnotatedBean.java?rev=1760205&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/CdiTransactionalAnnotatedBean.java (added)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/CdiTransactionalAnnotatedBean.java Sat Sep 10 19:16:59 2016
@@ -0,0 +1,66 @@
+/**
+ * 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.test;
+
+import org.springframework.context.annotation.Lazy;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Singleton;
+import javax.transaction.cdi.Transactional;
+import javax.transaction.cdi.Transactional.TxType;
+
+@Singleton
+@Transactional(value=TxType.REQUIRES_NEW)
+@Lazy
+public class CdiTransactionalAnnotatedBean extends ParentBean {
+
+    public void overridenInit() {
+        // By overriding the method and removing the annotation, this method has lost its
+        // @PostConstruct method because it isn't @Inherited
+    }
+
+    @PostConstruct
+    public void init() {
+
+    }
+
+    @Transactional(TxType.NOT_SUPPORTED)
+    public void txNotSupported() {
+    }
+
+    @Transactional(TxType.MANDATORY)
+    public void txMandatory() {
+    }
+
+    @Transactional(TxType.NEVER)
+    public void txNever() {
+    }
+
+    @Transactional(TxType.REQUIRED)
+    public void txRequired() {
+    }
+
+    @Override
+    public void txOverridenWithoutTransactional() {
+    }
+
+    @Transactional(TxType.REQUIRES_NEW)
+    public void txOverridenWithRequiresNew() {
+    }
+}
\ No newline at end of file