camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hekon...@apache.org
Subject git commit: [CAMEL-7126] Partially removed dependencies on JpaTemplate.
Date Tue, 14 Jan 2014 12:25:41 GMT
Updated Branches:
  refs/heads/master ff1426026 -> 039463cd7


[CAMEL-7126] Partially removed dependencies on JpaTemplate.


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

Branch: refs/heads/master
Commit: 039463cd744e0b6077aeebb8be0defd0e3dc9519
Parents: ff14260
Author: Henryk Konsek <hekonsek@gmail.com>
Authored: Tue Jan 14 13:17:36 2014 +0100
Committer: Henryk Konsek <hekonsek@gmail.com>
Committed: Tue Jan 14 13:25:23 2014 +0100

----------------------------------------------------------------------
 .../org/apache/camel/bam/EntityManagers.java    | 46 ++++++++++++++++++++
 .../bam/processor/JpaBamProcessorSupport.java   | 35 +++++++++------
 2 files changed, 67 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/039463cd/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java
----------------------------------------------------------------------
diff --git a/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java
new file mode 100644
index 0000000..6043a77
--- /dev/null
+++ b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java
@@ -0,0 +1,46 @@
+/**
+ * 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.camel.bam;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.springframework.orm.jpa.EntityManagerHolder;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
+
+public final class EntityManagers {
+
+    private EntityManagers() {
+    }
+
+    public static EntityManager resolveEntityManager(EntityManagerFactory entityManagerFactory)
{
+        EntityManagerHolder entityManagerHolder =
+                (EntityManagerHolder) TransactionSynchronizationManager.getResource(entityManagerFactory);
+        if (entityManagerHolder != null) {
+            return entityManagerHolder.getEntityManager();
+        }
+        return entityManagerFactory.createEntityManager();
+    }
+
+    public static void closeNonTransactionalEntityManager(EntityManager entityManager) {
+        boolean isTransactional = TransactionSynchronizationManager.hasResource(entityManager.getEntityManagerFactory());
+        if (entityManager != null && isTransactional) {
+            entityManager.close();
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/039463cd/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
b/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
index aa2ea6e..f9626c8 100644
--- a/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
+++ b/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
@@ -17,12 +17,12 @@
 package org.apache.camel.bam.processor;
 
 import java.lang.reflect.Method;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
+import javax.persistence.EntityManager;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Processor;
@@ -35,12 +35,15 @@ import org.slf4j.LoggerFactory;
 import org.springframework.orm.jpa.JpaTemplate;
 import org.springframework.transaction.support.TransactionTemplate;
 
+import static org.apache.camel.bam.EntityManagers.closeNonTransactionalEntityManager;
+import static org.apache.camel.bam.EntityManagers.resolveEntityManager;
+
 /**
  * A base class for JPA based BAM which can use any entity to store the process
  * instance information which allows derived classes to specialise the process
  * instance entity.
  *
- * @version 
+ * @version
  */
 public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T> {
     private static final Logger LOG = LoggerFactory.getLogger(JpaBamProcessorSupport.class);
@@ -53,14 +56,14 @@ public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T>
{
     private String keyPropertyName = "correlationKey";
     private boolean correlationKeyIsPrimary = true;
 
-    public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template,

+    public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template,
             Expression correlationKeyExpression, ActivityRules activityRules, Class<T>
entitytype) {
         super(transactionTemplate, correlationKeyExpression, entitytype);
         this.activityRules = activityRules;
         this.template = template;
     }
 
-    public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template,

+    public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template,
             Expression correlationKeyExpression, ActivityRules activityRules) {
         super(transactionTemplate, correlationKeyExpression);
         this.activityRules = activityRules;
@@ -138,17 +141,21 @@ public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T>
{
 
     @SuppressWarnings("unchecked")
     protected T findEntityByCorrelationKey(Object key) {
-        if (isCorrelationKeyIsPrimary()) {
-            return template.find(getEntityType(), key);
-        } else {
-            Map<String, Object> params = new HashMap<String, Object>(1);
-            params.put("key", key);
-            List<T> list = template.findByNamedParams(getFindByKeyQuery(), params);
-            if (list.isEmpty()) {
-                return null;
+        EntityManager entityManager = null;
+        try {
+            entityManager = resolveEntityManager(template.getEntityManagerFactory());
+            if (isCorrelationKeyIsPrimary()) {
+                return entityManager.find(getEntityType(), key);
             } else {
-                return list.get(0);
+                List<T> list = entityManager.createQuery(getFindByKeyQuery()).setParameter("key",
key).getResultList();
+                if (list.isEmpty()) {
+                    return null;
+                } else {
+                    return list.get(0);
+                }
             }
+        } finally {
+            closeNonTransactionalEntityManager(entityManager);
         }
     }
 


Mime
View raw message