Coverage Report - org.apache.camel.component.jpa.JpaTemplateTransactionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
JpaTemplateTransactionStrategy
79% 
N/A 
1
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.jpa;
 19  
 
 20  
 import org.apache.camel.impl.ServiceSupport;
 21  
 import org.springframework.orm.jpa.JpaCallback;
 22  
 import org.springframework.orm.jpa.JpaTemplate;
 23  
 import org.springframework.orm.jpa.JpaTransactionManager;
 24  
 import org.springframework.transaction.TransactionStatus;
 25  
 import org.springframework.transaction.support.TransactionCallback;
 26  
 import org.springframework.transaction.support.TransactionTemplate;
 27  
 
 28  
 import javax.persistence.EntityManager;
 29  
 import javax.persistence.EntityManagerFactory;
 30  
 import javax.persistence.PersistenceException;
 31  
 
 32  
 /**
 33  
  * Delegates the strategy to the {@link JpaTemplate} and {@link TransactionTemplate} for transaction handling
 34  
  *
 35  
  * @version $Revision: 525537 $
 36  
  */
 37  12
 public class JpaTemplateTransactionStrategy extends ServiceSupport implements TransactionStrategy {
 38  
     private final JpaTemplate jpaTemplate;
 39  
     private final TransactionTemplate transactionTemplate;
 40  
 
 41  
     /**
 42  
      * Creates a new implementation from the given JPA factory
 43  
      */
 44  
     public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf) {
 45  0
         JpaTemplate template = new JpaTemplate(emf);
 46  0
         return newInstance(emf, template);
 47  
     }
 48  
 
 49  
     public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf, JpaTemplate template) {
 50  9
         JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
 51  9
         transactionManager.afterPropertiesSet();
 52  
 
 53  9
         TransactionTemplate tranasctionTemplate = new TransactionTemplate(transactionManager);
 54  9
         tranasctionTemplate.afterPropertiesSet();
 55  
 
 56  9
         return new JpaTemplateTransactionStrategy(template, tranasctionTemplate);
 57  
     }
 58  
 
 59  9
     public JpaTemplateTransactionStrategy(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate) {
 60  9
         this.jpaTemplate = jpaTemplate;
 61  9
         this.transactionTemplate = transactionTemplate;
 62  9
     }
 63  
 
 64  
     public Object execute(final JpaCallback callback) {
 65  12
         return transactionTemplate.execute(new TransactionCallback() {
 66  12
             public Object doInTransaction(TransactionStatus status) {
 67  12
                 return jpaTemplate.execute(new JpaCallback() {
 68  12
                     public Object doInJpa(EntityManager entityManager) throws PersistenceException {
 69  12
                         return callback.doInJpa(entityManager);
 70  
                     }
 71  
                 });
 72  
             }
 73  
         });
 74  
     }
 75  
 
 76  
     protected void doStart() throws Exception {
 77  0
     }
 78  
 
 79  
     protected void doStop() throws Exception {
 80  0
     }
 81  
 }