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 2017/04/07 14:14:11 UTC
svn commit: r1790559 - in /aries/trunk/blueprint/plugin:
blueprint-maven-plugin-itest/
blueprint-maven-plugin-itest/src/it/transaction-annotation/
blueprint-maven-plugin-itest/src/it/transaction-annotation/src/
blueprint-maven-plugin-itest/src/it/trans...
Author: alien11689
Date: Fri Apr 7 14:14:11 2017
New Revision: 1790559
URL: http://svn.apache.org/viewvc?rev=1790559&view=rev
Log:
[ARIES-1710] Add enable-annotations only for transaction schema v1.2.0
Added:
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/pom.xml
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p1/
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p1/T1.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p2/
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p2/T1.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p3/
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p3/T1.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/verify.groovy
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/EnableAnnotationTest.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/transactionenable/
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/transactionenable/TxBean.java
Modified:
aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/pom.xml
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/JavaxTransactionFactory.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/Namespaces.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/cdi/CdiTransactionFactory.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/spring/SpringTransactionalFactory.java
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/pom.xml?rev=1790559&r1=1790558&r2=1790559&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/pom.xml (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/pom.xml Fri Apr 7 14:14:11 2017
@@ -58,7 +58,7 @@
</plugin>
</plugins>
</build>
-
+
<profiles>
<profile>
<id>integration-test</id>
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/pom.xml?rev=1790559&view=auto
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/pom.xml (added)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/pom.xml Fri Apr 7 14:14:11 2017
@@ -0,0 +1,107 @@
+<!--
+ 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.
+-->
+<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>
+
+ <groupId>org.apache.aries.blueprint.plugin.itest</groupId>
+ <artifactId>simple-project</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ <version>1</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.transaction</groupId>
+ <artifactId>javax.transaction-api</artifactId>
+ <version>1.2</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.aries.blueprint</groupId>
+ <artifactId>blueprint-maven-plugin</artifactId>
+ <version>@blueprint-maven-plugin.version@</version>
+ <executions>
+ <execution>
+ <id>p1</id>
+ <goals>
+ <goal>blueprint-generate</goal>
+ </goals>
+ <configuration>
+ <scanPaths>
+ <scanPath>p1</scanPath>
+ </scanPaths>
+ <generatedFileName>p1.xml</generatedFileName>
+ <namespaces>
+ <namespace>http://aries.apache.org/xmlns/transactions/v1.2.0</namespace>
+ </namespaces>
+ <customParameters>
+ <transaction.enableAnnotation>false</transaction.enableAnnotation>
+ </customParameters>
+ </configuration>
+ </execution>
+ <execution>
+ <id>p2</id>
+ <goals>
+ <goal>blueprint-generate</goal>
+ </goals>
+ <configuration>
+ <scanPaths>
+ <scanPath>p2</scanPath>
+ </scanPaths>
+ <generatedFileName>p2.xml</generatedFileName>
+ <namespaces>
+ <namespace>http://aries.apache.org/xmlns/transactions/v1.2.0</namespace>
+ </namespaces>
+ <customParameters>
+ <transaction.enableAnnotation>true</transaction.enableAnnotation>
+ </customParameters>
+ </configuration>
+ </execution>
+ <execution>
+ <id>p3</id>
+ <goals>
+ <goal>blueprint-generate</goal>
+ </goals>
+ <configuration>
+ <scanPaths>
+ <scanPath>p3</scanPath>
+ </scanPaths>
+ <generatedFileName>p3.xml</generatedFileName>
+ <namespaces>
+ <namespace>http://aries.apache.org/xmlns/transactions/v1.1.0</namespace>
+ </namespaces>
+ <customParameters>
+ <transaction.enableAnnotation>true</transaction.enableAnnotation>
+ </customParameters>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
\ No newline at end of file
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p1/T1.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p1/T1.java?rev=1790559&view=auto
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p1/T1.java (added)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p1/T1.java Fri Apr 7 14:14:11 2017
@@ -0,0 +1,28 @@
+/**
+ * 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 p1;
+
+import javax.inject.Singleton;
+import javax.transaction.Transactional;
+
+@Singleton
+@Transactional
+public class T1 {
+
+}
\ No newline at end of file
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p2/T1.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p2/T1.java?rev=1790559&view=auto
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p2/T1.java (added)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p2/T1.java Fri Apr 7 14:14:11 2017
@@ -0,0 +1,28 @@
+/**
+ * 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 p2;
+
+import javax.inject.Singleton;
+import javax.transaction.Transactional;
+
+@Singleton
+@Transactional
+public class T1 {
+
+}
\ No newline at end of file
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p3/T1.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p3/T1.java?rev=1790559&view=auto
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p3/T1.java (added)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/src/main/java/p3/T1.java Fri Apr 7 14:14:11 2017
@@ -0,0 +1,28 @@
+/**
+ * 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 p3;
+
+import javax.inject.Singleton;
+import javax.transaction.Transactional;
+
+@Singleton
+@Transactional
+public class T1 {
+
+}
\ No newline at end of file
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/verify.groovy
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/verify.groovy?rev=1790559&view=auto
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/verify.groovy (added)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin-itest/src/it/transaction-annotation/verify.groovy Fri Apr 7 14:14:11 2017
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+def fileP1 = new File(basedir, 'target/generated-sources/blueprint/OSGI-INF/blueprint/p1.xml')
+assert fileP1.exists()
+def xml1 = new groovy.util.XmlSlurper().parse(fileP1)
+assert xml1."enable-annotations".size() == 0
+assert xml1.bean.find{ it.@class == 'p1.T1'}.@id == 't1'
+assert xml1.bean.transaction.find{ it.name() == 'transaction' }.size() == 1
+
+def fileP2 = new File(basedir, 'target/generated-sources/blueprint/OSGI-INF/blueprint/p2.xml')
+assert fileP2.exists()
+def xml2 = new groovy.util.XmlSlurper().parse(fileP2)
+assert xml2."enable-annotations".size() == 1
+
+def fileP3 = new File(basedir, 'target/generated-sources/blueprint/OSGI-INF/blueprint/p3.xml')
+assert fileP3.exists()
+def xml3 = new groovy.util.XmlSlurper().parse(fileP3)
+assert xml3."enable-annotations".size() == 0
\ No newline at end of file
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/JavaxTransactionFactory.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/JavaxTransactionFactory.java?rev=1790559&r1=1790558&r2=1790559&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/JavaxTransactionFactory.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/JavaxTransactionFactory.java Fri Apr 7 14:14:11 2017
@@ -19,8 +19,10 @@
package org.apache.aries.blueprint.plugin.handlers.javax;
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.BlueprintConfiguration;
import org.apache.aries.blueprint.plugin.spi.ContextEnricher;
import org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler;
import org.apache.aries.blueprint.plugin.spi.XmlWriter;
@@ -28,11 +30,13 @@ import org.apache.aries.blueprint.plugin
import javax.transaction.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;
public class JavaxTransactionFactory implements BeanAnnotationHandler<Transactional>, MethodAnnotationHandler<Transactional> {
+ private static final String ENABLE_ANNOTATION = "transaction.enableAnnotation";
private String getTransactionTypeName(Transactional transactional) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, transactional.value().name());
@@ -47,7 +51,7 @@ public class JavaxTransactionFactory imp
public void handleMethodAnnotation(Class<?> clazz, List<Method> methods, ContextEnricher contextEnricher, BeanEnricher beanEnricher) {
final String nsTx1 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_TX1);
if (nsTx1 != null) {
- enableAnnotations(contextEnricher, nsTx1);
+ enableAnnotationTx1(contextEnricher, nsTx1);
for (final Method method : methods) {
final Transactional transactional = method.getAnnotation(Transactional.class);
final String transactionTypeName = getTransactionTypeName(transactional);
@@ -65,25 +69,15 @@ public class JavaxTransactionFactory imp
}
final String nsTx2 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_TX2);
if (nsTx2 != null) {
- enableTransactionsTx2(contextEnricher, nsTx2);
+ insertEnableAnnotationTx2(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 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_TX1);
if (nsTx1 != null) {
- enableAnnotations(contextEnricher, nsTx1);
+ enableAnnotationTx1(contextEnricher, nsTx1);
final Transactional transactional = annotatedElement.getAnnotation(Transactional.class);
final String transactionTypeName = getTransactionTypeName(transactional);
beanEnricher.addBeanContentWriter("javax.transactional.method/" + annotatedElement + "/*/" + transactionTypeName, new XmlWriter() {
@@ -98,11 +92,34 @@ public class JavaxTransactionFactory imp
}
final String nsTx2 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_TX2);
if (nsTx2 != null) {
- enableTransactionsTx2(contextEnricher, nsTx2);
+ insertEnableAnnotationTx2(contextEnricher, nsTx2);
}
}
- private void enableTransactionsTx2(ContextEnricher contextEnricher, final String namespace) {
+
+ private void enableAnnotationTx1(ContextEnricher contextEnricher, final String nsTx1) {
+ // TX1 enable-annotation are valid only in 1.2.0 schema
+ if (Namespaces.isTX12(nsTx1) && getEnableAnnotationConfig(contextEnricher.getBlueprintConfiguration())) {
+ insertEnableAnnotationTx1(contextEnricher, nsTx1);
+ }
+ }
+
+ private boolean getEnableAnnotationConfig(BlueprintConfiguration blueprintConfig) {
+ String enableAnnotation = blueprintConfig.getCustomParameters().get(ENABLE_ANNOTATION);
+ return enableAnnotation == null || Boolean.parseBoolean(enableAnnotation);
+ }
+
+ private void insertEnableAnnotationTx1(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);
+ }
+ });
+ }
+
+ private void insertEnableAnnotationTx2(ContextEnricher contextEnricher, final String namespace) {
contextEnricher.addBlueprintContentWriter("transaction/ennable-annotation", new XmlWriter() {
@Override
public void write(XMLStreamWriter writer) throws XMLStreamException {
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/Namespaces.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/Namespaces.java?rev=1790559&r1=1790558&r2=1790559&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/Namespaces.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/Namespaces.java Fri Apr 7 14:14:11 2017
@@ -25,6 +25,7 @@ class Namespaces {
static final String PATTERN_NS_JPA2 = "http\\:\\/\\/aries\\.apache\\.org\\/xmlns\\/jpa\\/v2\\.(.)\\.(.)";
static final String PATTERN_NS_TX1 = "http\\:\\/\\/aries\\.apache\\.org\\/xmlns\\/transactions\\/v1\\.(.)\\.(.)";
static final String PATTERN_NS_TX2 = "http\\:\\/\\/aries\\.apache\\.org\\/xmlns\\/transactions\\/v2\\.(.)\\.(.)";
+ private static final String NS_TX_1_2_0 = "http://aries.apache.org/xmlns/transactions/v1.2.0";
static String getNamespaceByPattern(Set<String> namespaces, String pattern) {
for (String namespace : namespaces) {
@@ -34,4 +35,8 @@ class Namespaces {
}
return null;
}
+
+ static boolean isTX12(String namespace) {
+ return NS_TX_1_2_0.equals(namespace);
+ }
}
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/cdi/CdiTransactionFactory.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/cdi/CdiTransactionFactory.java?rev=1790559&r1=1790558&r2=1790559&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/cdi/CdiTransactionFactory.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/javax/cdi/CdiTransactionFactory.java Fri Apr 7 14:14:11 2017
@@ -19,8 +19,10 @@
package org.apache.aries.blueprint.plugin.handlers.javax.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.BlueprintConfiguration;
import org.apache.aries.blueprint.plugin.spi.ContextEnricher;
import org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler;
import org.apache.aries.blueprint.plugin.spi.XmlWriter;
@@ -28,6 +30,7 @@ import org.apache.aries.blueprint.plugin
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;
@@ -36,6 +39,8 @@ 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 static final String NS_TX_1_2_0 = "http://aries.apache.org/xmlns/transactions/v1.2.0";
+ private static final String ENABLE_ANNOTATION = "transaction.enableAnnotation";
private String getTransactionTypeName(Transactional transactional) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, transactional.value().name());
@@ -50,7 +55,7 @@ public class CdiTransactionFactory imple
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);
+ enableAnnotationTx1(contextEnricher, nsTx1);
for (final Method method : methods) {
final Transactional transactional = method.getAnnotation(Transactional.class);
final String transactionTypeName = getTransactionTypeName(transactional);
@@ -67,26 +72,16 @@ public class CdiTransactionFactory imple
}
}
final String nsTx2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX2);
- if (nsTx2 != null) {
- enableTransactionsTx2(contextEnricher, nsTx2);
+ if ((nsTx2 != null) && getEnableAnnotationConfig(contextEnricher.getBlueprintConfiguration())) {
+ insertEnableAnnotationTx2(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);
+ enableAnnotationTx1(contextEnricher, nsTx1);
final Transactional transactional = annotatedElement.getAnnotation(Transactional.class);
final String transactionTypeName = getTransactionTypeName(transactional);
beanEnricher.addBeanContentWriter("javax.transactional.method/" + annotatedElement + "/*/" + transactionTypeName, new XmlWriter() {
@@ -100,27 +95,49 @@ public class CdiTransactionFactory imple
});
}
final String nsTx2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX2);
- if (nsTx2 != null) {
- enableTransactionsTx2(contextEnricher, nsTx2);
+ if (nsTx2 != null && getEnableAnnotationConfig(contextEnricher.getBlueprintConfiguration())) {
+ insertEnableAnnotationTx2(contextEnricher, nsTx2);
+ }
+ }
+
+ private String getNamespaceByPattern(Set<String> namespaces, String pattern) {
+ for (String namespace : namespaces) {
+ if (namespace.matches(pattern)) {
+ return namespace;
+ }
}
+ return null;
+ }
+
+ private void enableAnnotationTx1(ContextEnricher contextEnricher, final String nsTx1) {
+ // TX1 enable-annotation are valid only in 1.2.0 schema
+ if (NS_TX_1_2_0.equals(nsTx1) && getEnableAnnotationConfig(contextEnricher.getBlueprintConfiguration())) {
+ insertEnableAnnotationTx1(contextEnricher, nsTx1);
+ }
+ }
+
+ private boolean getEnableAnnotationConfig(BlueprintConfiguration blueprintConfig) {
+ String enableAnnotation = blueprintConfig.getCustomParameters().get(ENABLE_ANNOTATION);
+ return enableAnnotation == null || Boolean.parseBoolean(enableAnnotation);
}
- private void enableTransactionsTx2(ContextEnricher contextEnricher, final String namespace) {
+ private void insertEnableAnnotationTx1(ContextEnricher contextEnricher, final String namespace) {
contextEnricher.addBlueprintContentWriter("transaction/ennable-annotation", new XmlWriter() {
@Override
public void write(XMLStreamWriter writer) throws XMLStreamException {
- writer.writeEmptyElement("enable");
+ writer.writeEmptyElement("enable-annotations");
writer.writeDefaultNamespace(namespace);
}
});
}
- private String getNamespaceByPattern(Set<String> namespaces, String pattern) {
- for (String namespace : namespaces) {
- if (namespace.matches(pattern)) {
- return namespace;
+ private void insertEnableAnnotationTx2(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);
}
- }
- return null;
+ });
}
}
\ No newline at end of file
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/spring/SpringTransactionalFactory.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/spring/SpringTransactionalFactory.java?rev=1790559&r1=1790558&r2=1790559&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/spring/SpringTransactionalFactory.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/handlers/spring/SpringTransactionalFactory.java Fri Apr 7 14:14:11 2017
@@ -19,8 +19,10 @@
package org.apache.aries.blueprint.plugin.handlers.spring;
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.BlueprintConfiguration;
import org.apache.aries.blueprint.plugin.spi.ContextEnricher;
import org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler;
import org.apache.aries.blueprint.plugin.spi.XmlWriter;
@@ -29,6 +31,7 @@ import org.springframework.transaction.a
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
+
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
@@ -37,6 +40,8 @@ import java.util.Set;
public class SpringTransactionalFactory 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 static final String NS_TX_1_2_0 = "http://aries.apache.org/xmlns/transactions/v1.2.0";
+ private static final String ENABLE_ANNOTATION = "transaction.enableAnnotation";
private String getTransactionTypeName(Transactional transactional) {
Propagation propagation = transactional.propagation();
@@ -53,9 +58,9 @@ public class SpringTransactionalFactory
@Override
public void handleMethodAnnotation(Class<?> clazz, List<Method> methods, ContextEnricher contextEnricher, BeanEnricher beanEnricher) {
- final String nsTx1 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX1);
+ final String nsTx1 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX1);
if (nsTx1 != null) {
- enableAnnotations(contextEnricher, nsTx1);
+ enableAnnotationTx1(contextEnricher, nsTx1);
for (final Method method : methods) {
final Transactional transactional = method.getAnnotation(Transactional.class);
final String transactionTypeName = getTransactionTypeName(transactional);
@@ -73,25 +78,15 @@ public class SpringTransactionalFactory
}
final String nsTx2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX2);
if (nsTx2 != null) {
- enableTransactionsTx2(contextEnricher, nsTx2);
+ insertEnableAnnotationTx2(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);
+ enableAnnotationTx1(contextEnricher, nsTx1);
final Transactional transactional = annotatedElement.getAnnotation(Transactional.class);
final String transactionTypeName = getTransactionTypeName(transactional);
beanEnricher.addBeanContentWriter("javax.transactional.method/" + annotatedElement + "/*/" + transactionTypeName, new XmlWriter() {
@@ -106,26 +101,48 @@ public class SpringTransactionalFactory
}
final String nsTx2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_TX1);
if (nsTx2 != null) {
- enableTransactionsTx2(contextEnricher, nsTx2);
+ insertEnableAnnotationTx2(contextEnricher, nsTx2);
+ }
+ }
+
+ private String getNamespaceByPattern(Set<String> namespaces, String pattern) {
+ for (String namespace : namespaces) {
+ if (namespace.matches(pattern)) {
+ return namespace;
+ }
}
+ return null;
+ }
+
+ private void enableAnnotationTx1(ContextEnricher contextEnricher, final String nsTx1) {
+ // TX1 enable-annotation are valid only in 1.2.0 schema
+ if (NS_TX_1_2_0.equals(nsTx1) && getEnableAnnotationConfig(contextEnricher.getBlueprintConfiguration())) {
+ insertEnableAnnotationTx1(contextEnricher, nsTx1);
+ }
+ }
+
+ private boolean getEnableAnnotationConfig(BlueprintConfiguration blueprintConfig) {
+ String enableAnnotation = blueprintConfig.getCustomParameters().get(ENABLE_ANNOTATION);
+ return enableAnnotation == null || Boolean.parseBoolean(enableAnnotation);
}
- private void enableTransactionsTx2(ContextEnricher contextEnricher, final String namespace) {
+ private void insertEnableAnnotationTx1(ContextEnricher contextEnricher, final String namespace) {
contextEnricher.addBlueprintContentWriter("transaction/ennable-annotation", new XmlWriter() {
@Override
public void write(XMLStreamWriter writer) throws XMLStreamException {
- writer.writeEmptyElement("enable");
+ writer.writeEmptyElement("enable-annotations");
writer.writeDefaultNamespace(namespace);
}
});
}
-
- private String getNamespaceByPattern(Set<String> namespaces, String pattern) {
- for (String namespace : namespaces) {
- if (namespace.matches(pattern)) {
- return namespace;
+
+ private void insertEnableAnnotationTx2(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);
}
- }
- return null;
+ });
}
}
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/EnableAnnotationTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/EnableAnnotationTest.java?rev=1790559&view=auto
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/EnableAnnotationTest.java (added)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/EnableAnnotationTest.java Fri Apr 7 14:14:11 2017
@@ -0,0 +1,135 @@
+/**
+ * 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;
+
+import static org.apache.aries.blueprint.plugin.FilteredClassFinder.findClasses;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.apache.aries.blueprint.plugin.model.Blueprint;
+import org.apache.aries.blueprint.plugin.test.transactionenable.TxBean;
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.xbean.finder.ClassFinder;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+public class EnableAnnotationTest {
+
+ private static final String NS_JPA = "http://aries.apache.org/xmlns/jpa/v1.1.0";
+ private static final String NS_TX1_0 = "http://aries.apache.org/xmlns/transactions/v1.0.0";
+ private static final String NS_TX1_1 = "http://aries.apache.org/xmlns/transactions/v1.1.0";
+ private static final String NS_TX1_2 = "http://aries.apache.org/xmlns/transactions/v1.2.0";
+
+ private static Set<Class<?>> beanClasses;
+
+ private XPath xpath;
+ private Document document;
+ private byte[] xmlAsBytes;
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ ClassFinder classFinder = new ClassFinder(EnableAnnotationTest.class.getClassLoader());
+ beanClasses = findClasses(classFinder, Arrays.asList(
+ TxBean.class.getPackage().getName()));
+ }
+
+ private void writeXML(String namespace, String enableAnnotations) throws XMLStreamException,
+ UnsupportedEncodingException, ParserConfigurationException, SAXException, IOException {
+ Set<String> namespaces = new HashSet<>(Arrays.asList(NS_JPA, namespace));
+ Map<String, String> customParameters = new HashMap<>();
+ customParameters.put("transaction.enableAnnotation", enableAnnotations);
+ BlueprintConfigurationImpl blueprintConfiguration = new BlueprintConfigurationImpl(namespaces, null, customParameters);
+ Blueprint blueprint = new Blueprint(blueprintConfiguration, beanClasses);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ new BlueprintFileWriter(os).write(blueprint);
+
+ xmlAsBytes = os.toByteArray();
+ System.out.println(new String(xmlAsBytes, "UTF-8"));
+
+ document = readToDocument(xmlAsBytes, false);
+ xpath = XPathFactory.newInstance().newXPath();
+ }
+
+ @Test
+ public void testNS1_0() throws Exception {
+ writeXML(NS_TX1_0, "true");
+ assertNull(getEnableAnnotationTx1());
+ }
+
+ @Test
+ public void testNS1_1() throws Exception {
+ writeXML(NS_TX1_1, "true");
+ assertNull(getEnableAnnotationTx1());
+ }
+
+ @Test
+ public void testNS1_2_enabled() throws Exception {
+ writeXML(NS_TX1_2, "true");
+ assertNotNull(getEnableAnnotationTx1());
+ }
+
+ @Test
+ public void testNS1_2_disabled() throws Exception {
+ writeXML(NS_TX1_2, "false");
+ assertNull(getEnableAnnotationTx1());
+ }
+
+ @Test
+ public void testNS1_2_default() throws Exception {
+ writeXML(NS_TX1_2, null);
+ assertNotNull(getEnableAnnotationTx1());
+ }
+
+ private Document readToDocument(byte[] xmlAsBytes, boolean nameSpaceAware)
+ throws ParserConfigurationException,
+ SAXException, IOException {
+
+ InputStream is = new ByteArrayInputStream(xmlAsBytes);
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ builderFactory.setNamespaceAware(nameSpaceAware);
+ DocumentBuilder builder = builderFactory.newDocumentBuilder();
+ return builder.parse(is);
+ }
+
+ private Node getEnableAnnotationTx1() throws XPathExpressionException {
+ return (Node) xpath.evaluate("/blueprint/enable-annotations", document, XPathConstants.NODE);
+ }
+}
Added: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/transactionenable/TxBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/transactionenable/TxBean.java?rev=1790559&view=auto
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/transactionenable/TxBean.java (added)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/transactionenable/TxBean.java Fri Apr 7 14:14:11 2017
@@ -0,0 +1,30 @@
+/**
+ * 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.transactionenable;
+
+import javax.inject.Singleton;
+import javax.transaction.Transactional;
+
+@Singleton
+@Transactional
+public class TxBean {
+
+ public void txTest() {
+ }
+}