You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/04/03 19:47:19 UTC

svn commit: r525219 - in /activemq/camel/trunk/camel-jpa/src: main/java/org/apache/camel/component/jpa/ test/java/org/apache/camel/component/jpa/ test/java/org/apache/camel/examples/ test/resources/META-INF/

Author: jstrachan
Date: Tue Apr  3 10:47:18 2007
New Revision: 525219

URL: http://svn.apache.org/viewvc?view=rev&rev=525219
Log:
support URI configurations of consumers and added test example using a named query

Added:
    activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java   (with props)
    activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java   (with props)
Modified:
    activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
    activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
    activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
    activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java
    activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/SendEmail.java
    activemq/camel/trunk/camel-jpa/src/test/resources/META-INF/persistence.xml

Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java?view=diff&rev=525219&r1=525218&r2=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java (original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java Tue Apr  3 10:47:18 2007
@@ -26,6 +26,8 @@
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.URISupport;
+import org.apache.camel.util.IntrospectionSupport;
 import org.springframework.orm.jpa.JpaTemplate;
 import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
 import org.springframework.orm.jpa.JpaTransactionManager;
@@ -68,13 +70,22 @@
             return null;
         }
         URI u = new URI(uri);
-        String path = u.getSchemeSpecificPart();
-        String[] paths = ObjectHelper.splitOnCharacter(path, ":", 2);
+        String path = u.getHost();
+        if (path == null) {
+            path = u.getSchemeSpecificPart();
+        }
+        String[] paths = ObjectHelper.splitOnCharacter(path, ":", 4);
         // ignore a prefix
         if (paths[1] != null) {
             path = paths[1];
         }
         JpaEndpoint endpoint = new JpaEndpoint(uri, this);
+        Map options = URISupport.parseParamters(u);
+        Map consumerProperties = IntrospectionSupport.extractProperties(options, "consumer.");
+        if (consumerProperties != null) {
+            endpoint.setConsumerProperties(consumerProperties);
+        }
+        IntrospectionSupport.setProperties(endpoint, options);
 
         // lets interpret the next string as a class
         if (path != null) {

Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java?view=diff&rev=525219&r1=525218&r2=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java (original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java Tue Apr  3 10:47:18 2007
@@ -22,13 +22,12 @@
 import org.apache.camel.impl.PollingConsumer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.springframework.orm.jpa.JpaTemplate;
 import org.springframework.orm.jpa.JpaCallback;
 
 import javax.persistence.EntityManager;
 import javax.persistence.LockModeType;
-import javax.persistence.Query;
 import javax.persistence.PersistenceException;
+import javax.persistence.Query;
 import java.util.List;
 
 /**
@@ -40,6 +39,9 @@
     private final TransactionStrategy template;
     private QueryFactory queryFactory;
     private DeleteHandler<Object> deleteHandler;
+    private String query;
+    private String namedQuery;
+    private String nativeQuery;
 
     public JpaConsumer(JpaEndpoint endpoint, Processor<Exchange> processor) {
         super(endpoint, processor);
@@ -82,7 +84,6 @@
             if (queryFactory == null) {
                 throw new IllegalArgumentException("No queryType property configured on this consumer, nor an entityType configured on the endpoint so cannot consume");
             }
-
         }
         return queryFactory;
     }
@@ -102,13 +103,37 @@
         this.deleteHandler = deleteHandler;
     }
 
+    public String getNamedQuery() {
+        return namedQuery;
+    }
+
+    public void setNamedQuery(String namedQuery) {
+        this.namedQuery = namedQuery;
+    }
+
+    public String getNativeQuery() {
+        return nativeQuery;
+    }
+
+    public void setNativeQuery(String nativeQuery) {
+        this.nativeQuery = nativeQuery;
+    }
+
+    public String getQuery() {
+        return query;
+    }
+
+    public void setQuery(String query) {
+        this.query = query;
+    }
+
     // Implementation methods
     //-------------------------------------------------------------------------
 
     /**
      * A strategy method to lock an object with an exclusive lock so that it can be processed
      *
-     * @param entity the entity to be locked
+     * @param entity        the entity to be locked
      * @param entityManager
      * @return true if the entity was locked
      */
@@ -129,12 +154,23 @@
     }
 
     protected QueryFactory createQueryFactory() {
-        Class<?> entityType = endpoint.getEntityType();
-        if (entityType == null) {
-            return null;
+        if (query != null) {
+            return QueryBuilder.query(query);
+        }
+        else if (namedQuery != null) {
+            return QueryBuilder.namedQuery(namedQuery);
+        }
+        else if (nativeQuery != null) {
+            return QueryBuilder.nativeQuery(nativeQuery);
         }
         else {
-            return QueryBuilder.query("select x from " + entityType.getName() + " x");
+            Class<?> entityType = endpoint.getEntityType();
+            if (entityType == null) {
+                return null;
+            }
+            else {
+                return QueryBuilder.query("select x from " + entityType.getName() + " x");
+            }
         }
     }
 

Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java?view=diff&rev=525219&r1=525218&r2=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java (original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java Tue Apr  3 10:47:18 2007
@@ -23,6 +23,7 @@
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.impl.DefaultExchange;
@@ -30,6 +31,7 @@
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
+import java.util.Map;
 
 /**
  * @version $Revision$
@@ -40,6 +42,7 @@
     private Expression<Exchange> producerExpression;
     private int maximumResults = -1;
     private Class<?> entityType;
+    private Map consumerProperties;
 
     public JpaEndpoint(String uri, JpaComponent component) {
         super(uri, component);
@@ -56,7 +59,11 @@
     }
 
     public Consumer<Exchange> createConsumer(Processor<Exchange> processor) throws Exception {
-        return startService(new JpaConsumer(this, processor));
+        JpaConsumer consumer = new JpaConsumer(this, processor);
+        if (consumerProperties != null) {
+            IntrospectionSupport.setProperties(consumer, consumerProperties);
+        }
+        return startService(consumer);
     }
 
 
@@ -95,6 +102,14 @@
 
     public void setEntityType(Class<?> entityType) {
         this.entityType = entityType;
+    }
+
+    public Map getConsumerProperties() {
+        return consumerProperties;
+    }
+
+    public void setConsumerProperties(Map consumerProperties) {
+        this.consumerProperties = consumerProperties;
     }
 
     // Implementation methods

Modified: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java?view=diff&rev=525219&r1=525218&r2=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java (original)
+++ activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java Tue Apr  3 10:47:18 2007
@@ -76,7 +76,7 @@
 
         // now lets assert that there is a result
         results = template.find(queryText);
-        assertEquals("Should have no results: " + results, 1, results.size());
+        assertEquals("Should have results: " + results, 1, results.size());
         SendEmail mail = (SendEmail) results.get(0);
         assertEquals("address property", "foo@bar.com", mail.getAddress());
 

Added: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java?view=auto&rev=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java (added)
+++ activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java Tue Apr  3 10:47:18 2007
@@ -0,0 +1,132 @@
+/**
+ *
+ * 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.component.jpa;
+
+import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Endpoint;
+import org.apache.camel.examples.SendEmail;
+import org.apache.camel.examples.MultiSteps;
+import org.apache.camel.util.CamelClient;
+import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.JpaCallback;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.List;
+
+/**
+ * @version $Revision$
+ */
+public class JpaWithNamedQueryTest extends TestCase {
+    private static final transient Log log = LogFactory.getLog(JpaWithNamedQueryTest.class);
+    protected CamelContext camelContext = new DefaultCamelContext();
+    protected CamelClient client = new CamelClient(camelContext);
+    protected JpaEndpoint endpoint;
+    protected TransactionStrategy transactionStrategy;
+    protected JpaTemplate template;
+    protected Consumer<Exchange> consumer;
+    protected Exchange receivedExchange;
+    protected CountDownLatch latch = new CountDownLatch(1);
+    protected String entityName = MultiSteps.class.getName();
+    protected String queryText = "select o from " + entityName + " o where o.step = 1";
+
+    public void testProducerInsertsIntoDatabaseThenConsumerFiresMessageExchange() throws Exception {
+        transactionStrategy.execute(new JpaCallback() {
+            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
+                // lets delete any exiting records before the test
+                entityManager.createQuery("delete from " + entityName).executeUpdate();
+
+                // now lets create a dummy entry
+                MultiSteps dummy = new MultiSteps("cheese");
+                dummy.setStep(4);
+                entityManager.persist(dummy);
+                return null;
+            }
+        });
+
+        List results = template.find(queryText);
+        assertEquals("Should have no results: " + results, 0, results.size());
+
+        // lets produce some objects
+        client.send(endpoint, new Processor<Exchange>() {
+            public void onExchange(Exchange exchange) {
+                exchange.getIn().setBody(new MultiSteps("foo@bar.com"));
+            }
+        });
+
+        // now lets assert that there is a result
+        results = template.find(queryText);
+        assertEquals("Should have results: " + results, 1, results.size());
+        MultiSteps mail = (MultiSteps) results.get(0);
+        assertEquals("address property", "foo@bar.com", mail.getAddress());
+
+        // now lets create a consumer to consume it
+        consumer = endpoint.createConsumer(new Processor<Exchange>() {
+            public void onExchange(Exchange e) {
+                log.info("Received exchange: " + e.getIn());
+                receivedExchange = e;
+                latch.countDown();
+            }
+        });
+
+        boolean received = latch.await(50, TimeUnit.SECONDS);
+        assertTrue("Did not receive the message!", received);
+
+        assertNotNull(receivedExchange);
+        MultiSteps result = receivedExchange.getIn().getBody(MultiSteps.class);
+        assertNotNull("Received a POJO", result);
+        assertEquals("address property", "foo@bar.com", result.getAddress());
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        ServiceHelper.startServices(client, camelContext);
+
+        Endpoint value = camelContext.resolveEndpoint(getEndpointUri());
+        assertNotNull("Could not find endpoint!", value);
+        assertTrue("Should be a JPA endpoint but was: " + value, value instanceof JpaEndpoint);
+        endpoint = (JpaEndpoint) value;
+
+        transactionStrategy = endpoint.createTransactionStrategy();
+        template = endpoint.getTemplate();
+    }
+
+    protected String getEndpointUri() {
+        return "jpa://" + MultiSteps.class.getName() + "?consumer.namedQuery=step1";
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+
+        ServiceHelper.stopServices(consumer, client, camelContext);
+
+        super.tearDown();
+    }
+}

Propchange: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java?view=auto&rev=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java (added)
+++ activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java Tue Apr  3 10:47:18 2007
@@ -0,0 +1,75 @@
+/**
+ *
+ * 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.examples;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.NamedQuery;
+
+/**
+ * Represents a task which has multiple steps so that it can move from stage to stage
+ *
+ * @version $Revision$
+ */
+@Entity
+@NamedQuery(name = "step1", query="select x from MultiSteps x where x.step = 1")
+public class MultiSteps {
+    private Long id;
+    private String address;
+    private int step;
+
+    public MultiSteps() {
+    }
+
+    public MultiSteps(String address) {
+        setAddress(address);
+        setStep(1);
+    }
+
+    @Override
+    public String toString() {
+        return "MultiSteps[id: " + getId() + " step: " + getStep() + " address: " + getAddress() + "]";
+    }
+
+    @Id
+    @GeneratedValue
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getAddress() {
+        return address;
+    }
+
+    public void setAddress(String address) {
+        this.address = address;
+    }
+
+    public int getStep() {
+        return step;
+    }
+
+    public void setStep(int step) {
+        this.step = step;
+    }
+}

Propchange: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/SendEmail.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/SendEmail.java?view=diff&rev=525219&r1=525218&r2=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/SendEmail.java (original)
+++ activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/examples/SendEmail.java Tue Apr  3 10:47:18 2007
@@ -22,7 +22,7 @@
 import javax.persistence.Id;
 
 /**
- * Represents a task which is added to the database
+ * Represents a task which is added to the database, then removed from the database when it is consumed
  *
  * @version $Revision$
  */

Modified: activemq/camel/trunk/camel-jpa/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/test/resources/META-INF/persistence.xml?view=diff&rev=525219&r1=525218&r2=525219
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/test/resources/META-INF/persistence.xml (original)
+++ activemq/camel/trunk/camel-jpa/src/test/resources/META-INF/persistence.xml Tue Apr  3 10:47:18 2007
@@ -26,9 +26,10 @@
         in cases where there are multiple JPA implementations available.
     -->
     <provider>
-        org.apache.openjpa.persistence.PersistenceProviderImpl
+      org.apache.openjpa.persistence.PersistenceProviderImpl
     </provider>
 
+    <class>org.apache.camel.examples.MultiSteps</class>
     <class>org.apache.camel.examples.SendEmail</class>
 
     <properties>
@@ -44,7 +45,8 @@
     </properties>
   </persistence-unit>
 
-    <persistence-unit name="custom" transaction-type="RESOURCE_LOCAL">
+  <persistence-unit name="custom" transaction-type="RESOURCE_LOCAL">
+    <class>org.apache.camel.examples.MultiSteps</class>
     <class>org.apache.camel.examples.SendEmail</class>
 
     <properties>