Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6D18E10742 for ; Tue, 14 Jan 2014 12:25:43 +0000 (UTC) Received: (qmail 15388 invoked by uid 500); 14 Jan 2014 12:25:42 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 15303 invoked by uid 500); 14 Jan 2014 12:25:42 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 15296 invoked by uid 99); 14 Jan 2014 12:25:42 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Jan 2014 12:25:42 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 810B78BC944; Tue, 14 Jan 2014 12:25:41 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hekonsek@apache.org To: commits@camel.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: git commit: [CAMEL-7126] Partially removed dependencies on JpaTemplate. Date: Tue, 14 Jan 2014 12:25:41 +0000 (UTC) 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 Authored: Tue Jan 14 13:17:36 2014 +0100 Committer: Henryk Konsek 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 extends BamProcessorSupport { private static final Logger LOG = LoggerFactory.getLogger(JpaBamProcessorSupport.class); @@ -53,14 +56,14 @@ public class JpaBamProcessorSupport extends BamProcessorSupport { 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 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 extends BamProcessorSupport { @SuppressWarnings("unchecked") protected T findEntityByCorrelationKey(Object key) { - if (isCorrelationKeyIsPrimary()) { - return template.find(getEntityType(), key); - } else { - Map params = new HashMap(1); - params.put("key", key); - List 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 list = entityManager.createQuery(getFindByKeyQuery()).setParameter("key", key).getResultList(); + if (list.isEmpty()) { + return null; + } else { + return list.get(0); + } } + } finally { + closeNonTransactionalEntityManager(entityManager); } }