Author: jstrachan
Date: Thu Sep  6 04:43:59 2007
New Revision: 573224

URL: http://svn.apache.org/viewvc?rev=573224&view=rev
Log:
fixed the classloader issues so that Camel service units can find Java 
RouteBuilders on the classpath and load them. Also we now by default expose a 
simple CamelControlBus to JBI; then any JBI consumers (or Camel endpoints) can 
be exposed to the NMR dynamically. We also use the Camel TypeConverter stuff to 
go from POJO -> Source (such as if Camel messages contain a JAXB POJO etc)

Added:
    
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelControlBus.java
      - copied, changed from r572929, 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
Modified:
    
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelJbiComponent.java
    
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
    
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
    
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java
    
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java

Copied: 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelControlBus.java
 (from r572929, 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java)
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelControlBus.java?p2=incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelControlBus.java&p1=incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java&r1=572929&r2=573224&rev=573224&view=diff
==============================================================================
--- 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelControlBus.java
 Thu Sep  6 04:43:59 2007
@@ -16,130 +16,25 @@
  */
 package org.apache.servicemix.camel;
 
-import java.io.StringReader;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.Map;
-import java.util.Set;
+import java.util.Collection;
 
-import javax.jbi.messaging.MessageExchange;
-import javax.jbi.messaging.MessageExchangeFactory;
-import javax.jbi.messaging.MessagingException;
-import javax.jbi.messaging.NormalizedMessage;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.ExchangePattern;
-import org.apache.camel.Message;
+import org.apache.camel.Body;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
 
 /**
- * The binding of how Camel messages get mapped to JBI and back again
- * 
+ * A simple control bus for accessing Camel features over the JBI NMR
+ *
  * @version $Revision: 563665 $
  */
-public class JbiBinding {
-    private String messageExchangePattern;
-
-    /**
-     * Extracts the body from the given normalized message
-     */
-    public Object extractBodyFromJbi(JbiExchange exchange, NormalizedMessage 
normalizedMessage) {
-        // TODO we may wish to turn this into a POJO such as a JAXB/DOM
-        return normalizedMessage.getContent();
-    }
-
-    public Source convertBodyToJbi(Object body) {
-        // TODO: conversion strategy?
-        return null;
-    }
-
-    public MessageExchange makeJbiMessageExchange(Exchange camelExchange,
-                                                  MessageExchangeFactory 
exchangeFactory,
-                                                  String defaultMep)
-        throws MessagingException, URISyntaxException {
-        
-        MessageExchange jbiExchange = createJbiMessageExchange(camelExchange, 
exchangeFactory, defaultMep);
-        NormalizedMessage normalizedMessage = jbiExchange.getMessage("in");
-        if (normalizedMessage == null) {
-            normalizedMessage = jbiExchange.createMessage();
-            jbiExchange.setMessage(normalizedMessage, "in");
-        }
-        normalizedMessage.setContent(getJbiInContent(camelExchange));
-        addJbiHeaders(jbiExchange, normalizedMessage, camelExchange);
-        return jbiExchange;
-    }
-
-    // Properties
-    //-------------------------------------------------------------------------
+public class CamelControlBus {
+    private CamelContext camelContext;
 
-    public String getMessageExchangePattern() {
-        return messageExchangePattern;
+    public CamelControlBus(CamelContext camelContext) {
+        this.camelContext = camelContext;
     }
 
-    /**
-     * Sets the message exchange pattern to use for communicating with JBI
-     *
-     * @param messageExchangePattern
-     */
-    public void setMessageExchangePattern(String messageExchangePattern) {
-        this.messageExchangePattern = messageExchangePattern;
+    public Collection<Endpoint> getEndpoints(@Body String filter) {
+        return camelContext.getSingletonEndpoints();
     }
-
-    protected MessageExchange createJbiMessageExchange(Exchange camelExchange,
-                                                       MessageExchangeFactory 
exchangeFactory,
-                                                       String defaultMep)
-        throws MessagingException, URISyntaxException {
-
-        ExchangePattern mep = camelExchange.getPattern();
-        if (mep == null) {
-            mep = ExchangePattern.fromWsdlUri(defaultMep);
-        }
-        if (mep == null) {
-            mep = ExchangePattern.fromWsdlUri(getMessageExchangePattern());
-        }
-        MessageExchange answer = null;
-        if (mep != null) {
-            if (mep == ExchangePattern.InOnly) {
-                answer = exchangeFactory.createInOnlyExchange();
-            } else if (mep == ExchangePattern.InOptionalOut) {
-                answer = exchangeFactory.createInOptionalOutExchange();
-            } else if (mep == ExchangePattern.InOut) {
-                answer = exchangeFactory.createInOutExchange();
-            } else if (mep == ExchangePattern.RobustInOnly) {
-                answer = exchangeFactory.createRobustInOnlyExchange();
-            } else {
-                answer = exchangeFactory.createExchange(new 
URI(mep.toString()));
-            }
-        }
-        // TODO: this is not really usefull as the out will not be
-        // TODO: populated at that time
-        if (answer == null) {
-            // lets try choose the best MEP based on the camel message
-            Message out = camelExchange.getOut(false);
-            if (out == null || out.getBody() == null) {
-                answer = exchangeFactory.createInOnlyExchange();
-            } else {
-                answer = exchangeFactory.createInOutExchange();
-            }
-        }
-        return answer;
-    }
-
-    protected Source getJbiInContent(Exchange camelExchange) {
-        // TODO this should be more smart
-        Object value = camelExchange.getIn().getBody();
-        if (value instanceof String) {
-            return new StreamSource(new StringReader(value.toString()));
-        }
-        return camelExchange.getIn().getBody(Source.class);
-    }
-
-    protected void addJbiHeaders(MessageExchange jbiExchange, 
NormalizedMessage normalizedMessage, Exchange camelExchange) {
-        Set<Map.Entry<String, Object>> entries = 
camelExchange.getIn().getHeaders().entrySet();
-        for (Map.Entry<String, Object> entry : entries) {
-            normalizedMessage.setProperty(entry.getKey(), entry.getValue());
-        }
-    }
-
-}
+}
\ No newline at end of file

Modified: 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelJbiComponent.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelJbiComponent.java?rev=573224&r1=573223&r2=573224&view=diff
==============================================================================
--- 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelJbiComponent.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelJbiComponent.java
 Thu Sep  6 04:43:59 2007
@@ -267,6 +267,7 @@
      */
     public boolean isEndpointExposedOnNmr(Endpoint endpoint) {
         // TODO we should only expose consuming endpoints
-        return !(endpoint instanceof JbiEndpoint);
+        return false;
+        //return !(endpoint instanceof JbiEndpoint);
     }
 }

Modified: 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java?rev=573224&r1=573223&r2=573224&view=diff
==============================================================================
--- 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
 Thu Sep  6 04:43:59 2007
@@ -23,15 +23,16 @@
 import javax.jbi.management.DeploymentException;
 
 import org.apache.camel.Endpoint;
+import org.apache.camel.component.bean.BeanComponent;
 import org.apache.camel.spring.SpringCamelContext;
 import org.apache.servicemix.common.ServiceUnit;
 import org.apache.servicemix.common.xbean.AbstractXBeanDeployer;
 import org.apache.xbean.kernel.Kernel;
 import org.apache.xbean.server.spring.loader.PureSpringLoader;
 import org.apache.xbean.server.spring.loader.SpringLoader;
+import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.AbstractXmlApplicationContext;
-import org.springframework.context.support.FileSystemXmlApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
 
 /**
@@ -45,7 +46,8 @@
     private PureSpringLoader springLoader = new PureSpringLoader() {
         @Override
         protected AbstractXmlApplicationContext 
createXmlApplicationContext(String configLocation) {
-            return new FileSystemXmlApplicationContext(new String[] 
{configLocation}, false, createParentApplicationContext());
+            ApplicationContext parentAppContext = 
createParentApplicationContext(getXmlPreprocessors());
+            return new FileSystemXmlApplicationContext(new 
String[]{configLocation}, false, parentAppContext, getXmlPreprocessors());
         }
     };
 
@@ -72,7 +74,11 @@
         // lets register the deployer so that any endpoints activated are added
         // to this SU
         component.deployer = this;
-        return super.deploy(serviceUnitName, serviceUnitRootPath);
+
+        // lets install the context class loader
+        ServiceUnit serviceUnit = super.deploy(serviceUnitName, 
serviceUnitRootPath);
+        
Thread.currentThread().setContextClassLoader(serviceUnit.getConfigurationClassLoader());
+        return serviceUnit;
     }
 
     public void addService(CamelJbiEndpoint endpoint) {
@@ -90,11 +96,18 @@
 
             // now lets iterate through all the endpoints
             Collection<Endpoint> endpoints = 
camelContext.getSingletonEndpoints();
+
             for (Endpoint endpoint : endpoints) {
                 if (component.isEndpointExposedOnNmr(endpoint)) {
                     
services.add(component.createJbiEndpointFromCamel(endpoint));
                 }
             }
+
+            // lets add a control bus endpoint to ensure we have at least one 
endpoint to deploy
+            BeanComponent beanComponent = camelContext.getComponent("bean", 
BeanComponent.class);
+            Endpoint endpoint = beanComponent.createEndpoint(new 
CamelControlBus(camelContext), "camel:controlBus");
+            services.add(component.createJbiEndpointFromCamel(endpoint));
+            
             return services;
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -110,9 +123,10 @@
      * Returns the parent application context which can be used to auto-wire 
any
      * JBI based components using the jbi prefix
      */
-    protected ApplicationContext createParentApplicationContext() {
+    protected ApplicationContext createParentApplicationContext(List 
xmlProcessors) {
         GenericApplicationContext answer = new GenericApplicationContext();
         answer.getBeanFactory().registerSingleton("jbi", component);
+        answer.setClassLoader(Thread.currentThread().getContextClassLoader());
         answer.start();
         answer.refresh();
         return answer;

Modified: 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java?rev=573224&r1=573223&r2=573224&view=diff
==============================================================================
--- 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiBinding.java
 Thu Sep  6 04:43:59 2007
@@ -21,7 +21,6 @@
 import java.net.URISyntaxException;
 import java.util.Map;
 import java.util.Set;
-
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessageExchangeFactory;
 import javax.jbi.messaging.MessagingException;
@@ -32,6 +31,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.Message;
+import org.apache.camel.util.ExchangeHelper;
 
 /**
  * The binding of how Camel messages get mapped to JBI and back again
@@ -49,9 +49,8 @@
         return normalizedMessage.getContent();
     }
 
-    public Source convertBodyToJbi(Object body) {
-        // TODO: conversion strategy?
-        return null;
+    public Source convertBodyToJbi(Exchange exchange, Object body) {
+        return ExchangeHelper.convertToType(exchange, Source.class, body);
     }
 
     public MessageExchange makeJbiMessageExchange(Exchange camelExchange,

Modified: 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java?rev=573224&r1=573223&r2=573224&view=diff
==============================================================================
--- 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/JbiMessage.java
 Thu Sep  6 04:43:59 2007
@@ -109,7 +109,7 @@
     public void setBody(Object body) {
         if (normalizedMessage != null) {
             if (!(body instanceof Source)) {
-                body = getExchange().getBinding().convertBodyToJbi(body);
+                body = 
getExchange().getBinding().convertBodyToJbi(getExchange(), body);
             }
             try {
                 normalizedMessage.setContent((Source) body);

Modified: 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java?rev=573224&r1=573223&r2=573224&view=diff
==============================================================================
--- 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java
 Thu Sep  6 04:43:59 2007
@@ -148,7 +148,7 @@
     protected void configureExchange(ServiceMixClient client,
             MessageExchange exchange) {
         ServiceEndpoint endpoint = client.getContext().getEndpoint(
-                CamelJbiEndpoint.SERVICE_NAME, "seda:a");
+                CamelJbiEndpoint.SERVICE_NAME, "camel:controlBus");
         assertNotNull("Should have a Camel endpoint exposed in JBI!", 
endpoint);
         exchange.setEndpoint(endpoint);
     }


Reply via email to