[jboss-svn-commits] JBL Code SVN: r33098 - labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed May 26 15:31:55 EDT 2010


Author: dward
Date: 2010-05-26 15:31:54 -0400 (Wed, 26 May 2010)
New Revision: 33098

Added:
   labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelMessageComposer.java
Modified:
   labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelGateway.java
   labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBComponent.java
   labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBProcessor.java
   labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/ServiceProcessor.java
Log:
Created CamelMessageComposer.


Modified: labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelGateway.java
===================================================================
--- labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelGateway.java	2010-05-26 19:15:51 UTC (rev 33097)
+++ labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelGateway.java	2010-05-26 19:31:54 UTC (rev 33098)
@@ -91,7 +91,7 @@
 			// JBossESB on AS4 will not work if we use this.
 			camelContext.setPackageScanClassResolver(new JBossPackageScanClassResolver());
 		}
-		camelContext.addComponent(JBossESBComponent.JBOSSESB, new JBossESBComponent());
+		camelContext.addComponent(JBossESBComponent.JBOSSESB, new JBossESBComponent(getConfig()));
 		if (logger.isInfoEnabled()) {
 			try {
 				StringWriter sw = new StringWriter();

Added: labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelMessageComposer.java
===================================================================
--- labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelMessageComposer.java	                        (rev 0)
+++ labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelMessageComposer.java	2010-05-26 19:31:54 UTC (rev 33098)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated 
+ * by the @authors tag. All rights reserved. 
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors. 
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A 
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+ * MA  02110-1301, USA.
+ * 
+ * (C) 2005-2010
+ */
+package org.jboss.soa.esb.listeners.gateway.camel;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map.Entry;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultMessage;
+import org.apache.camel.impl.MessageSupport;
+import org.jboss.soa.esb.addressing.Call;
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
+import org.jboss.soa.esb.listeners.message.AbstractMessageComposer;
+import org.jboss.soa.esb.listeners.message.MessageDeliverException;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.Properties;
+
+/**
+ * CamelMessageComposer.
+ * 
+ * @author dward at jboss.org
+ */
+public class CamelMessageComposer<T extends org.apache.camel.Message> extends AbstractMessageComposer<T> {
+
+	@Override
+	protected void populateMessage(Message esbMessageIn, T camelMessageIn) throws MessageDeliverException {
+		// maintain message id (both JBossESB and Camel use UUID.randomUUID(), so we can reuse)
+		String camelMessageId = camelMessageIn.getMessageId();
+		if (camelMessageId != null) {
+			try {
+				esbMessageIn.getHeader().getCall().setMessageID(new URI(camelMessageId));
+			} catch (URISyntaxException e) {
+				throw new MessageDeliverException("problem creating messageID", e);
+			}
+		}
+		
+		// maintain correlation
+		setRelatesTo(camelMessageIn.getExchange(), esbMessageIn);
+		
+		// update esb properties from camel headers
+		Properties properties = esbMessageIn.getProperties();
+		for (Entry<String, Object> entry : camelMessageIn.getHeaders().entrySet()) {
+			properties.setProperty(entry.getKey(), entry.getValue());
+		}
+		
+		// set esb body (payload) from camel body
+		getPayloadProxy().setPayload(esbMessageIn, camelMessageIn.getBody(String.class));
+	}
+	
+	@SuppressWarnings("unchecked")
+	@Override
+    public T decompose(Message esbMessageOut, T camelMessageIn) throws MessageDeliverException {
+		org.apache.camel.Message camelMessageOut;
+		if (camelMessageIn instanceof MessageSupport) {
+			camelMessageOut = ((MessageSupport)camelMessageIn).newInstance();
+		} else {
+			camelMessageOut = new DefaultMessage();
+		}
+		
+		// maintain message id (both JBossESB and Camel use UUID.randomUUID(), so we can reuse)
+		URI esbMessageID = esbMessageOut.getHeader().getCall().getMessageID();
+		if (esbMessageID != null) {
+			camelMessageOut.setMessageId(esbMessageID.toString());
+		}
+		
+		// maintain correlation
+		if (camelMessageIn != null) {
+			setRelatesTo(camelMessageIn.getExchange(), esbMessageOut);
+		}
+		
+		// set camel headers from esb properties
+		Properties properties = esbMessageOut.getProperties();
+		for (String name : properties.getNames()) {
+			camelMessageOut.setHeader(name, properties.getProperty(name));
+		}
+		
+		// set camel body from esb body (payload)
+		camelMessageOut.setBody(getPayloadProxy().getPayload(esbMessageOut));
+		
+		return (T)camelMessageOut;
+    }
+	
+	private void setRelatesTo(Exchange exchange, Message esbMessage) throws MessageDeliverException {
+		String exchangeId = (exchange != null ? exchange.getExchangeId() : null);
+		if (exchangeId != null) {
+			Call call = esbMessage.getHeader().getCall();
+			URI relatesTo = call.getRelatesTo();
+			if (relatesTo == null) {
+				try {
+					relatesTo = new URI(InVMEpr.INVM_PROTOCOL, "correlationID", exchangeId);
+				} catch (URISyntaxException e) {
+					throw new MessageDeliverException("problem creating relatesTo", e);
+				}
+				call.setRelatesTo(relatesTo);
+			}
+		}
+	}
+
+}


Property changes on: labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/CamelMessageComposer.java
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBComponent.java
===================================================================
--- labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBComponent.java	2010-05-26 19:15:51 UTC (rev 33097)
+++ labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBComponent.java	2010-05-26 19:31:54 UTC (rev 33098)
@@ -28,6 +28,7 @@
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.impl.ProcessorEndpoint;
 import org.jboss.soa.esb.Service;
+import org.jboss.soa.esb.helpers.ConfigTree;
 
 /**
  * JBossESBComponent.
@@ -44,8 +45,12 @@
 	public static final String ASYNC = "async";
 	public static final String TIMEOUT = "timeout";
 	
-	public JBossESBComponent() {}
+	private final ConfigTree config;
 	
+	public JBossESBComponent(ConfigTree config) {
+		this.config = config;
+	}
+	
 	@Override
 	protected Endpoint createEndpoint(String uri, String command, Map<String, Object> parameters) throws Exception
 	{
@@ -56,7 +61,7 @@
 			String name = getAndRemoveParameter(parameters, NAME, String.class);
 			boolean async = getAndRemoveParameter(parameters, ASYNC, Boolean.class, Boolean.FALSE);
 			long timeout = getAndRemoveParameter(parameters, TIMEOUT, Long.class, 30000L);
-			processor = new ServiceProcessor(new Service(category, name), async, timeout);
+			processor = new ServiceProcessor(config, new Service(category, name), async, timeout);
 		}
 		else
 		{

Modified: labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBProcessor.java
===================================================================
--- labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBProcessor.java	2010-05-26 19:15:51 UTC (rev 33097)
+++ labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/JBossESBProcessor.java	2010-05-26 19:31:54 UTC (rev 33098)
@@ -19,20 +19,9 @@
  */
 package org.jboss.soa.esb.listeners.gateway.camel;
 
-import java.io.Serializable;
-import java.net.URI;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.Processor;
-import org.jboss.soa.esb.addressing.Call;
-import org.jboss.soa.esb.addressing.eprs.InVMEpr;
-import org.jboss.soa.esb.message.Message;
-import org.jboss.soa.esb.message.Properties;
-import org.jboss.soa.esb.message.format.MessageFactory;
-import org.jboss.soa.esb.message.format.MessageType;
-import org.jboss.soa.esb.util.Util;
+import org.jboss.soa.esb.helpers.ConfigTree;
 
 /**
  * JBossESBProcessor.
@@ -42,84 +31,21 @@
 public abstract class JBossESBProcessor implements Processor
 {
 	
-	protected Message convertCamelToESB(Exchange exchange, org.apache.camel.Message camelMessage) throws Exception
-	{
-		Message esbMessage;
-		Serializable serial = camelMessage.getHeader(Message.class.getName(), Serializable.class);
-		if (serial != null)
-		{
-			esbMessage = Util.deserialize(serial);
-		}
-		else
-		{
-			esbMessage = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
-			// maintain message id (both JBossESB and Camel use UUID.randomUUID(), so we can just reuse)
-			esbMessage.getHeader().getCall().setMessageID(new URI(camelMessage.getMessageId()));
-		}
-		
-		// maintain correlation
-		setRelatesTo(exchange, esbMessage);
-		
-		// update esb properties from camel headers
-		Properties properties = esbMessage.getProperties();
-		Map<String,Object> headers = camelMessage.getHeaders();
-		for (Entry<String, Object> entry : headers.entrySet())
-		{
-			String key = entry.getKey();
-			if (!key.equals(Message.class.getName()))
-			{
-				properties.setProperty(key, entry.getValue());
-			}
-		}
-		
-		// update esb body from camel body
-		String body = camelMessage.getBody(String.class);
-		if (body != null)
-		{
-			esbMessage.getBody().add(body);
-		}
-		
-		return esbMessage;
+	private final ConfigTree config;
+	private final CamelMessageComposer<Message> composer;
+	
+	public JBossESBProcessor(ConfigTree config) {
+		this.config = config;
+		composer = new CamelMessageComposer<Message>();
+		composer.setConfiguration(config);
 	}
 	
-	protected org.apache.camel.Message convertESBToCamel(Exchange exchange, Message esbMessage) throws Exception
-	{
-		org.apache.camel.Message camelMessage = new org.apache.camel.impl.DefaultMessage();
-		
-		camelMessage.setHeader(Message.class.getName(), Util.serialize(esbMessage));
-		
-		// maintain message id (both JBossESB and Camel use UUID.randomUUID(), so we can just reuse)
-		camelMessage.setMessageId(esbMessage.getHeader().getCall().getMessageID().toString());
-		
-		// maintain correlation
-		setRelatesTo(exchange, esbMessage);
-		
-		// set camel headers from esb properties
-		Properties properties = esbMessage.getProperties();
-		for (String name : properties.getNames())
-		{
-			camelMessage.setHeader(name, properties.getProperty(name));
-		}
-		
-		// set camel body from esb body
-		Object body = esbMessage.getBody().get();
-		if (body != null)
-		{
-			camelMessage.setBody(body);
-		}
-		
-		return camelMessage;
+	public final ConfigTree getConfig() {
+		return config;
 	}
 	
-	private void setRelatesTo(Exchange exchange, Message esbMessage) throws Exception
-	{
-		Call call = esbMessage.getHeader().getCall();
-		URI relatesTo = call.getRelatesTo();
-		if (relatesTo == null)
-		{
-			relatesTo = new URI(InVMEpr.INVM_PROTOCOL, "correlationID", exchange.getExchangeId());
-			call.setRelatesTo(relatesTo);
-		}
+	public final CamelMessageComposer<Message> getComposer() {
+		return composer;
 	}
 	
 }

Modified: labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/ServiceProcessor.java
===================================================================
--- labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/ServiceProcessor.java	2010-05-26 19:15:51 UTC (rev 33097)
+++ labs/jbossesb/workspace/dward/JBESB_4_8_Camel_Gateway/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/camel/ServiceProcessor.java	2010-05-26 19:31:54 UTC (rev 33098)
@@ -22,6 +22,7 @@
 import org.apache.camel.Exchange;
 import org.jboss.soa.esb.Service;
 import org.jboss.soa.esb.client.ServiceInvoker;
+import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.message.Message;
 
 /**
@@ -29,33 +30,29 @@
  * 
  * @author dward at jboss.org
  */
-public class ServiceProcessor extends JBossESBProcessor
-{
+public class ServiceProcessor extends JBossESBProcessor {
 	
-	private Service service;
-	private boolean async;
-	private long timeout;
+	private final Service service;
+	private final boolean async;
+	private final long timeout;
 	
-	public ServiceProcessor(Service service, boolean async, long timeout)
-	{
+	public ServiceProcessor(ConfigTree config, Service service, boolean async, long timeout) {
+		super(config);
 		this.service = service;
 		this.async = async;
 		this.timeout = timeout;
 	}
 	
-	public void process(Exchange exchange) throws Exception
-	{
+	public void process(Exchange exchange) throws Exception {
 		org.apache.camel.Message camelMessageIn = exchange.getIn();
-		Message esbMessageIn = convertCamelToESB(exchange, camelMessageIn);
+		Message esbMessageIn = getComposer().compose(camelMessageIn);
 		ServiceInvoker invoker = new ServiceInvoker(service);
-		if (async)
-		{
+		if (async) {
 			invoker.deliverAsync(esbMessageIn);
 		}
-		else
-		{
+		else {
 			Message esbMessageOut = invoker.deliverSync(esbMessageIn, timeout);
-			org.apache.camel.Message camelMessageOut = convertESBToCamel(exchange, esbMessageOut);
+			org.apache.camel.Message camelMessageOut = getComposer().decompose(esbMessageOut, camelMessageIn);
 			exchange.setOut(camelMessageOut);
 		}
 	}



More information about the jboss-svn-commits mailing list