You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/04/15 07:55:12 UTC
svn commit: r934296 - in /camel/trunk/components/camel-jpa/src:
main/java/org/apache/camel/component/jpa/JpaComponent.java
test/java/org/apache/camel/processor/jpa/JpaRouteTest.java
Author: davsclaus
Date: Thu Apr 15 05:55:12 2010
New Revision: 934296
URL: http://svn.apache.org/viewvc?rev=934296&view=rev
Log:
CAMEL-2647: JpaComponent will not lookup in registry and use EMF and TM if found. Convention over configuration.
Modified:
camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaRouteTest.java
Modified: camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java?rev=934296&r1=934295&r2=934296&view=diff
==============================================================================
--- camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java (original)
+++ camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java Thu Apr 15 05:55:12 2010
@@ -21,7 +21,10 @@ import javax.persistence.EntityManagerFa
import org.apache.camel.Endpoint;
import org.apache.camel.impl.DefaultComponent;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.support.TransactionTemplate;
/**
* A JPA Component
@@ -29,6 +32,7 @@ import org.springframework.transaction.P
* @version $Revision$
*/
public class JpaComponent extends DefaultComponent {
+ private static final Log LOG = LogFactory.getLog(JpaComponent.class);
private EntityManagerFactory entityManagerFactory;
private PlatformTransactionManager transactionManager;
@@ -69,4 +73,63 @@ public class JpaComponent extends Defaul
return endpoint;
}
+
+ @Override
+ protected void doStart() throws Exception {
+ // lookup entity manager factory and use it if only one provided
+ if (entityManagerFactory == null) {
+ Map<String, EntityManagerFactory> map = getCamelContext().getRegistry().lookupByType(EntityManagerFactory.class);
+ if (map != null) {
+ if (map.size() == 1) {
+ entityManagerFactory = map.values().iterator().next();
+ LOG.info("Using EntityManagerFactory found in registry with id ["
+ + map.keySet().iterator().next() + "] " + entityManagerFactory);
+ } else {
+ LOG.debug("Could not find a single EntityManagerFactory in registry as there was " + map.size() + " instances.");
+ }
+ }
+ } else {
+ LOG.info("Using EntityManagerFactory configured: " + entityManagerFactory);
+ }
+
+ // lookup transaction manager and use it if only one provided
+ if (transactionManager == null) {
+ Map<String, PlatformTransactionManager> map = getCamelContext().getRegistry().lookupByType(PlatformTransactionManager.class);
+ if (map != null) {
+ if (map.size() == 1) {
+ transactionManager = map.values().iterator().next();
+ LOG.info("Using TransactionManager found in registry with id ["
+ + map.keySet().iterator().next() + "] " + transactionManager);
+ } else {
+ LOG.debug("Could not find a single TransactionManager in registry as there was " + map.size() + " instances.");
+ }
+ }
+ } else {
+ LOG.info("Using TransactionManager configured on this component: " + transactionManager);
+ }
+
+ // transaction manager could also be hidden in a template
+ if (transactionManager == null) {
+ Map<String, TransactionTemplate> map = getCamelContext().getRegistry().lookupByType(TransactionTemplate.class);
+ if (map != null) {
+ if (map.size() == 1) {
+ transactionManager = map.values().iterator().next().getTransactionManager();
+ LOG.info("Using TransactionManager found in registry with id ["
+ + map.keySet().iterator().next() + "] " + transactionManager);
+ } else {
+ LOG.debug("Could not find a single TransactionTemplate in registry as there was " + map.size() + " instances.");
+ }
+ }
+ }
+
+ // warn about missing configuration
+ if (entityManagerFactory == null) {
+ LOG.warn("No EntityManagerFactory has been configured on this JpaComponent. Each JpaEndpoint will auto create their own EntityManagerFactory.");
+ }
+ if (transactionManager == null) {
+ LOG.warn("No TransactionManager has been configured on this JpaComponent. Each JpaEndpoint will auto create their own JpaTransactionManager.");
+ }
+
+ super.doStart();
+ }
}
Modified: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaRouteTest.java?rev=934296&r1=934295&r2=934296&view=diff
==============================================================================
--- camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaRouteTest.java (original)
+++ camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaRouteTest.java Thu Apr 15 05:55:12 2010
@@ -20,6 +20,7 @@ import java.util.List;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jpa.JpaComponent;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.examples.SendEmail;
import org.apache.camel.spring.SpringCamelContext;
@@ -46,6 +47,11 @@ public class JpaRouteTest extends CamelT
@Test
public void testRouteJpa() throws Exception {
+ // should auto setup transaction manager and entity factory
+ JpaComponent jpa = context.getComponent("jpa", JpaComponent.class);
+ assertNotNull("Should have been auto assigned", jpa.getEntityManagerFactory());
+ assertNotNull("Should have been auto assigned", jpa.getTransactionManager());
+
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);