camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r769677 - in /camel/branches/camel-1.x/components/camel-jpa/src: main/java/org/apache/camel/component/jpa/ test/resources/META-INF/
Date Wed, 29 Apr 2009 06:25:02 GMT
Author: davsclaus
Date: Wed Apr 29 06:25:01 2009
New Revision: 769677

URL: http://svn.apache.org/viewvc?rev=769677&view=rev
Log:
CAMEL-1552: Added options to set transaction maanger declarative for managed environments.

Modified:
    camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
    camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
    camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
    camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
    camel/branches/camel-1.x/components/camel-jpa/src/test/resources/META-INF/persistence.xml

Modified: camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java?rev=769677&r1=769676&r2=769677&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
(original)
+++ camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
Wed Apr 29 06:25:01 2009
@@ -17,15 +17,13 @@
 package org.apache.camel.component.jpa;
 
 import java.util.Map;
-
 import javax.persistence.EntityManagerFactory;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.Component;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.util.ObjectHelper;
+import org.springframework.transaction.PlatformTransactionManager;
 
 /**
  * A JPA Component
@@ -34,10 +32,7 @@
  */
 public class JpaComponent extends DefaultComponent<Exchange> {
     private EntityManagerFactory entityManagerFactory;
-
-    public Component resolveComponent(CamelContext container, String uri) throws Exception
{
-        return null;
-    }
+    private PlatformTransactionManager transactionManager;
 
     // Properties
     //-------------------------------------------------------------------------
@@ -49,6 +44,14 @@
         this.entityManagerFactory = entityManagerFactory;
     }
 
+    public PlatformTransactionManager getTransactionManager() {
+        return transactionManager;
+    }
+
+    public void setTransactionManager(PlatformTransactionManager transactionManager) {
+        this.transactionManager = transactionManager;
+    }
+
     // Implementation methods
     //-------------------------------------------------------------------------
 

Modified: camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java?rev=769677&r1=769676&r2=769677&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
(original)
+++ camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
Wed Apr 29 06:25:01 2009
@@ -34,12 +34,15 @@
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ObjectHelper;
 import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.transaction.PlatformTransactionManager;
 
 /**
  * @version $Revision$
  */
 public class JpaEndpoint extends ScheduledPollEndpoint<Exchange> {
     private EntityManagerFactory entityManagerFactory;
+    private PlatformTransactionManager transactionManager;
     private String persistenceUnit = "camel";
     private JpaTemplate template;
     private Expression<Exchange> producerExpression;
@@ -50,9 +53,14 @@
     private boolean consumeLockEntity = true;
     private boolean flushOnSend = true;
 
+    public JpaEndpoint(String endpointUri) {
+        super(endpointUri);
+    }
+
     public JpaEndpoint(String uri, JpaComponent component) {
         super(uri, component);
         entityManagerFactory = component.getEntityManagerFactory();
+        transactionManager = component.getTransactionManager();
     }
 
     public JpaEndpoint(String endpointUri, EntityManagerFactory entityManagerFactory) {
@@ -60,8 +68,10 @@
         this.entityManagerFactory = entityManagerFactory;
     }
 
-    public JpaEndpoint(String endpointUri) {
+    public JpaEndpoint(String endpointUri, EntityManagerFactory entityManagerFactory, PlatformTransactionManager
transactionManager) {
         super(endpointUri);
+        this.entityManagerFactory = entityManagerFactory;
+        this.transactionManager = transactionManager;
     }
 
     public Producer<Exchange> createProducer() throws Exception {
@@ -140,6 +150,17 @@
         this.entityManagerFactory = entityManagerFactory;
     }
 
+    public PlatformTransactionManager getTransactionManager() {
+        if (transactionManager == null) {
+            transactionManager = createTransactionManager();
+        }
+        return transactionManager;
+    }
+
+    public void setTransactionManager(PlatformTransactionManager transactionManager) {
+        this.transactionManager = transactionManager;
+    }
+
     public Map getEntityManagerProperties() {
         if (entityManagerProperties == null) {
             entityManagerProperties = System.getProperties();
@@ -185,6 +206,7 @@
 
     // Implementation methods
     // -------------------------------------------------------------------------
+
     protected void validate() {
         ObjectHelper.notNull(getEntityManagerFactory(), "entityManagerFactory property");
     }
@@ -197,14 +219,18 @@
         return Persistence.createEntityManagerFactory(persistenceUnit, getEntityManagerProperties());
     }
 
+    protected PlatformTransactionManager createTransactionManager() {
+        JpaTransactionManager tm = new JpaTransactionManager(getEntityManagerFactory());
+        tm.afterPropertiesSet();
+        return tm;
+    }
+
     protected EntityManager createEntityManager() {
         return getEntityManagerFactory().createEntityManager();
     }
 
     protected TransactionStrategy createTransactionStrategy() {
-        EntityManagerFactory emf = getEntityManagerFactory();
-        return JpaTemplateTransactionStrategy.newInstance(emf, getTemplate());
-        // return new DefaultTransactionStrategy(emf);
+        return JpaTemplateTransactionStrategy.newInstance(getTransactionManager(), getTemplate());
     }
 
     protected Expression<Exchange> createProducerExpression() {

Modified: camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java?rev=769677&r1=769676&r2=769677&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
(original)
+++ camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
Wed Apr 29 06:25:01 2009
@@ -17,15 +17,13 @@
 package org.apache.camel.component.jpa;
 
 import java.util.Iterator;
-
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceException;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
-import org.apache.camel.converter.ObjectConverter;
 import org.apache.camel.impl.DefaultProducer;
-
+import org.apache.camel.util.ObjectHelper;
 import org.springframework.orm.jpa.JpaCallback;
 
 /**
@@ -48,10 +46,10 @@
         if (values != null) {
             template.execute(new JpaCallback() {
                 public Object doInJpa(EntityManager entityManager) throws PersistenceException
{
-                    Iterator iter = ObjectConverter.iterator(values);
+                    Iterator iter = ObjectHelper.createIterator(values);
                     while (iter.hasNext()) {
                         Object value = iter.next();
-                        entityManager.persist(value);
+                        entityManager.merge(value);
                     }
                     if (endpoint.isFlushOnSend()) {
                         entityManager.flush();

Modified: camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java?rev=769677&r1=769676&r2=769677&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
(original)
+++ camel/branches/camel-1.x/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
Wed Apr 29 06:25:01 2009
@@ -25,6 +25,7 @@
 import org.springframework.orm.jpa.JpaCallback;
 import org.springframework.orm.jpa.JpaTemplate;
 import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.transaction.PlatformTransactionManager;
 import org.springframework.transaction.TransactionStatus;
 import org.springframework.transaction.support.TransactionCallback;
 import org.springframework.transaction.support.TransactionTemplate;
@@ -51,6 +52,9 @@
         return newInstance(emf, template);
     }
 
+    /**
+     * Creates a new implementation from the given JPA factory and JPA template
+     */
     public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf, JpaTemplate
template) {
         JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
         transactionManager.afterPropertiesSet();
@@ -61,6 +65,16 @@
         return new JpaTemplateTransactionStrategy(template, tranasctionTemplate);
     }
 
+    /**
+     * Creates a new implementation from the given Transaction Manager and JPA template
+     */
+    public static JpaTemplateTransactionStrategy newInstance(PlatformTransactionManager transactionManager,
JpaTemplate template) {
+        TransactionTemplate tranasctionTemplate = new TransactionTemplate(transactionManager);
+        tranasctionTemplate.afterPropertiesSet();
+
+        return new JpaTemplateTransactionStrategy(template, tranasctionTemplate);
+    }
+
     public Object execute(final JpaCallback callback) {
         return transactionTemplate.execute(new TransactionCallback() {
             public Object doInTransaction(TransactionStatus status) {

Modified: camel/branches/camel-1.x/components/camel-jpa/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jpa/src/test/resources/META-INF/persistence.xml?rev=769677&r1=769676&r2=769677&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-jpa/src/test/resources/META-INF/persistence.xml
(original)
+++ camel/branches/camel-1.x/components/camel-jpa/src/test/resources/META-INF/persistence.xml
Wed Apr 29 06:25:01 2009
@@ -16,9 +16,10 @@
 -->
 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-             version="1.0">
+             version="1.0"
+             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 
-  <persistence-unit name="camel" transaction-type="RESOURCE_LOCAL">
+    <persistence-unit name="camel" transaction-type="RESOURCE_LOCAL">
     <!--
         The default provider can be OpenJPA, or some other product.
         This element is optional if OpenJPA is the only JPA provider
@@ -35,11 +36,6 @@
     <properties>
       <property name="openjpa.ConnectionURL" value="jdbc:derby:target/derby;create=true"/>
       <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
-      <!--
-            <property name="openjpa.ConnectionUserName" value="sa"/>
-            <property name="openjpa.ConnectionPassword" value=""/>
-      -->
-
       <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
       <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO, SQL=TRACE"/>
     </properties>



Mime
View raw message