You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2015/09/03 18:36:49 UTC

deltaspike git commit: DELTASPIKE-986 support for jta outside of ee-servers

Repository: deltaspike
Updated Branches:
  refs/heads/master 249f12de0 -> b61c4e4e1


DELTASPIKE-986 support for jta outside of ee-servers


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/b61c4e4e
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/b61c4e4e
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/b61c4e4e

Branch: refs/heads/master
Commit: b61c4e4e1da1bb38bc047865426a380deefe8e0b
Parents: 249f12d
Author: gpetracek <gp...@apache.org>
Authored: Thu Sep 3 18:03:08 2015 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Thu Sep 3 18:36:19 2015 +0200

----------------------------------------------------------------------
 .../jpa/api/config/base/JpaBaseConfig.java      | 38 +++++++++++++
 .../BeanManagedUserTransactionStrategy.java     |  3 +-
 .../ManagedUserTransactionResolver.java         | 35 ++++++++++++
 .../transaction/UserTransactionResolver.java    | 56 +++++++++++++++-----
 .../META-INF/apache-deltaspike.properties       | 19 +++++++
 5 files changed, 138 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b61c4e4e/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/config/base/JpaBaseConfig.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/config/base/JpaBaseConfig.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/config/base/JpaBaseConfig.java
new file mode 100644
index 0000000..7ffaaaf
--- /dev/null
+++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/config/base/JpaBaseConfig.java
@@ -0,0 +1,38 @@
+/*
+ * 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.deltaspike.jpa.api.config.base;
+
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.config.base.DeltaSpikeBaseConfig;
+
+public interface JpaBaseConfig extends DeltaSpikeBaseConfig
+{
+    interface UserTransaction
+    {
+        String JNDI_NAME = ConfigResolver.resolve("deltaspike.jpa.user-transaction.jndi-name")
+                .withCurrentProjectStage(true)
+                .withDefault("java:comp/UserTransaction")
+                .getValue();
+
+        Integer TIMEOUT_IN_SECONDS = ConfigResolver.resolve("deltaspike.jpa.user-transaction.timeout_in_seconds")
+                .as(Integer.class)
+                .withCurrentProjectStage(true)
+                .getValue();
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b61c4e4e/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
index e5af662..baad16b 100644
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
@@ -22,6 +22,7 @@ import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.apache.deltaspike.core.api.provider.DependentProvider;
 import org.apache.deltaspike.core.impl.util.JndiUtils;
 import org.apache.deltaspike.core.util.ExceptionUtils;
+import org.apache.deltaspike.jpa.api.config.base.JpaBaseConfig;
 import org.apache.deltaspike.jpa.api.transaction.TransactionConfig;
 import org.apache.deltaspike.jpa.impl.transaction.context.EntityManagerEntry;
 
@@ -130,7 +131,7 @@ public class BeanManagedUserTransactionStrategy extends ResourceLocalTransaction
             @Override
             public Integer getUserTransactionTimeoutInSeconds()
             {
-                return null;
+                return JpaBaseConfig.UserTransaction.TIMEOUT_IN_SECONDS;
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b61c4e4e/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ManagedUserTransactionResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ManagedUserTransactionResolver.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ManagedUserTransactionResolver.java
new file mode 100644
index 0000000..d6657c0
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ManagedUserTransactionResolver.java
@@ -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.
+ */
+package org.apache.deltaspike.jpa.impl.transaction;
+
+import javax.annotation.Resource;
+import javax.transaction.UserTransaction;
+
+//we need to keep it separated,
+//because the injection of UserTransaction might fail with an exception (see DELTASPIKE-986)
+public class ManagedUserTransactionResolver
+{
+    @Resource
+    private UserTransaction userTransaction;
+
+    public UserTransaction resolveUserTransaction()
+    {
+        return userTransaction;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b61c4e4e/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
index 92a734a..3760054 100644
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
@@ -18,10 +18,14 @@
  */
 package org.apache.deltaspike.jpa.impl.transaction;
 
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.api.provider.DependentProvider;
 import org.apache.deltaspike.core.impl.util.JndiUtils;
+import org.apache.deltaspike.jpa.api.config.base.JpaBaseConfig;
 
-import javax.annotation.Resource;
 import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
 import javax.transaction.UserTransaction;
 import java.io.Serializable;
 
@@ -30,28 +34,56 @@ import java.io.Serializable;
 @Dependent
 public class UserTransactionResolver implements Serializable
 {
-    protected static final String USER_TRANSACTION_JNDI_NAME = "java:comp/UserTransaction";
-
     private static final long serialVersionUID = -1432802805095533499L;
 
-    @Resource
-    private UserTransaction userTransaction;
+    @Inject
+    private BeanManager beanManager;
 
     public UserTransaction resolveUserTransaction()
     {
-        if (userTransaction != null)
+        UserTransaction userTransaction;
+
+        try
         {
-            return userTransaction;
+            DependentProvider<ManagedUserTransactionResolver> provider =
+                BeanProvider.getDependent(this.beanManager, ManagedUserTransactionResolver.class);
+
+            userTransaction = provider.get().resolveUserTransaction();
+
+            provider.destroy();
+        }
+        catch (Throwable t)
+        {
+            //it was just a try
+            userTransaction = null;
         }
 
-        try
+        if (userTransaction != null)
         {
-            return JndiUtils.lookup(USER_TRANSACTION_JNDI_NAME, UserTransaction.class);
+            return userTransaction;
         }
-        catch (Exception e)
+
+        String jndiName = JpaBaseConfig.UserTransaction.JNDI_NAME;
+
+        String[] jndiNames = jndiName.split(",");
+
+        for (String currentJndiName : jndiNames)
         {
-            // do nothing it was just a try
-            return null;
+            try
+            {
+                userTransaction = JndiUtils.lookup(currentJndiName, UserTransaction.class);
+
+                if (userTransaction != null)
+                {
+                    break;
+                }
+            }
+            catch (Exception e)
+            {
+                userTransaction = null;
+            }
         }
+
+        return userTransaction;
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b61c4e4e/deltaspike/modules/jpa/impl/src/main/resources/META-INF/apache-deltaspike.properties
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/resources/META-INF/apache-deltaspike.properties b/deltaspike/modules/jpa/impl/src/main/resources/META-INF/apache-deltaspike.properties
new file mode 100644
index 0000000..e9d3afe
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/main/resources/META-INF/apache-deltaspike.properties
@@ -0,0 +1,19 @@
+# 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.
+
+deltaspike_ordinal=50
+deltaspike.jpa.user-transaction.jndi-name=java:comp/UserTransaction,java:/jboss/UserTransaction