[jboss-svn-commits] JBL Code SVN: r13391 - in labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta: src/org/jboss/soa/esb/listeners/gateway and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jul 12 02:29:32 EDT 2007


Author: beve
Date: 2007-07-12 02:29:32 -0400 (Thu, 12 Jul 2007)
New Revision: 13391

Added:
   labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContentsUnitTest.java
Modified:
   labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java
   labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContents.java
Log:
Added JMSHeader fields to the ESB Message object.


Modified: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java	2007-07-12 06:24:43 UTC (rev 13390)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java	2007-07-12 06:29:32 UTC (rev 13391)
@@ -132,8 +132,10 @@
         	
             // Send the message to the queue...
             if (message instanceof org.jboss.soa.esb.message.Message) {
-            	message = Util.serialize((org.jboss.soa.esb.message.Message)message);
+            	org.jboss.soa.esb.message.Message esbMsg = (org.jboss.soa.esb.message.Message)message;
+            	message = Util.serialize(esbMsg);
             	jmsMessage = createObjectMessage(message);
+                setJMSProperties ( jmsMessage, esbMsg );
             } else if(message instanceof String) {
             	jmsMessage = queueSetup.jmsSession.createTextMessage();
     
@@ -166,6 +168,10 @@
         }
     }
 
+	protected void setJMSProperties( Message jmsMessage, org.jboss.soa.esb.message.Message esbMsg ) throws JMSException { 	
+        jmsMessage.setJMSCorrelationID( esbMsg.getHeader().getCall().getRelatesTo().toString() );
+	}
+
 	private Message createObjectMessage(Object message) throws JMSException {
 		Message jmsMessage;
 		jmsMessage = queueSetup.jmsSession.createObjectMessage();

Modified: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContents.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContents.java	2007-07-12 06:24:43 UTC (rev 13390)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContents.java	2007-07-12 06:29:32 UTC (rev 13391)
@@ -23,14 +23,22 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Enumeration;
 
 import javax.jms.BytesMessage;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
 import javax.jms.JMSException;
 import javax.jms.ObjectMessage;
+import javax.jms.Queue;
 import javax.jms.TextMessage;
+import javax.jms.Topic;
 
-import org.jboss.soa.esb.common.Environment;
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.addressing.eprs.JMSEpr;
+import org.jboss.soa.esb.common.ModulePropertyManager;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.message.format.MessageFactory;
 
@@ -45,62 +53,97 @@
 */
 public class PackageJmsMessageContents
 {
-  @SuppressWarnings("unchecked")
-  public Message process (Object obj) throws JMSException, IOException
-  {
-     if (!(obj instanceof javax.jms.Message))
-        throw new IllegalArgumentException(
-              "Object must be instance of javax.jms.Message");
-     byte[] bytes = getMessageContent((javax.jms.Message) obj);
-     if (null == bytes) return null;
+	private Logger log = Logger.getLogger( PackageJmsMessageContents.class );
+	
+	@SuppressWarnings("unchecked")
+	public Message process (Object obj) throws JMSException, IOException
+	{
+		if (!(obj instanceof javax.jms.Message))
+			throw new IllegalArgumentException( "Object must be instance of javax.jms.Message");
+		
+		byte[] bytes = getMessageContent((javax.jms.Message) obj);
+		if (null == bytes) return null;
+		
+		javax.jms.Message jmsMsg = (javax.jms.Message) obj;
+		Message message = MessageFactory.getInstance().getMessage();
+		message.getBody().setByteArray(getMessageContent(jmsMsg));
+		     
+		if (jmsMsg instanceof ObjectMessage) {
+			ObjectMessage msg = (ObjectMessage) jmsMsg;
+			message.getBody().add(msg.getObject());
+		}
+		if (jmsMsg.getJMSMessageID()!=null) {
+			try
+			{
+				message.getHeader().getCall().setMessageID( new URI( jmsMsg.getJMSMessageID() ));
+			} catch (URISyntaxException e) {
+				log.error( "Could not setMessageID to JMSMessageID [" + jmsMsg.getJMSMessageID() + "]", e );
+			}
+		}
+		if (jmsMsg.getJMSCorrelationID()!=null) {
+			try
+			{
+				message.getHeader().getCall().setRelatesTo( new URI ( jmsMsg.getJMSCorrelationID()));
+			} catch (URISyntaxException e) {
+				log.error( "Could not setRelatesTo to JMSCorrelationID [ " + jmsMsg.getJMSCorrelationID() + "]", e );
+			}
+		}
+		if (jmsMsg.getJMSReplyTo()!=null) {
+			Destination replyToDestination = jmsMsg.getJMSReplyTo();
+			String destType = null;
+			String destName = null;
+			String connection = ModulePropertyManager.getPropertyManager("connection").getProperty(ConnectionFactory.class.getName(), "ConnectionFactory");
+			if ( replyToDestination instanceof Queue )
+			{
+				Queue queue = ( Queue ) replyToDestination;
+				destName = queue.getQueueName();
+				destType = JMSEpr.QUEUE_TYPE;
+			}
+			else
+			{
+				Topic topic = ( Topic ) replyToDestination;
+				destName = topic.getTopicName();
+				destType = JMSEpr.TOPIC_TYPE;
+			}
+			message.getHeader().getCall().setReplyTo( new JMSEpr( destType , destName, connection ) );
+		}
+		Enumeration<String> EE = jmsMsg.getPropertyNames();
+		if (null != EE)
+		{
+			while (EE.hasMoreElements())
+			{
+				String name = EE.nextElement();
+				Object value = jmsMsg.getObjectProperty(name);
+				if (null != value)
+					message.getProperties().setProperty(name, value);
+			}
+		}
+		return message;
+	}
 
-     javax.jms.Message jmsMsg = (javax.jms.Message) obj;
-     Message message = MessageFactory.getInstance().getMessage();
-     message.getBody().setByteArray(getMessageContent(jmsMsg));
-     
-     if (jmsMsg instanceof ObjectMessage) {
-         ObjectMessage msg = (ObjectMessage) jmsMsg;
-         message.getBody().add(msg.getObject());
-     }
-     if (jmsMsg.getJMSMessageID()!=null) {
-         message.getBody().add(Environment.JMS_MESSAGE_ID, jmsMsg.getJMSMessageID());
-     }
-     Enumeration<String> EE = jmsMsg.getPropertyNames();
-     if (null != EE)
-     {
-        while (EE.hasMoreElements())
-        {
-           String name = EE.nextElement();
-           Object value = jmsMsg.getObjectProperty(name);
-           if (null != value)
-              message.getProperties().setProperty(name, value);
-        }
-     }
-     return message;
-  }
+private byte[] getMessageContent (javax.jms.Message jMess) throws JMSException, IOException
+{
+if (jMess instanceof TextMessage)
+return ((TextMessage) jMess).getText().getBytes();
 
-  private byte[] getMessageContent (javax.jms.Message jMess) throws JMSException, IOException
-  {
-     if (jMess instanceof TextMessage)
-        return ((TextMessage) jMess).getText().getBytes();
+if (jMess instanceof BytesMessage)
+{
+BytesMessage jBytes = (BytesMessage) jMess;
+ByteArrayOutputStream out = new ByteArrayOutputStream();
+byte[] ba = new byte[1000];
+int iQread;
+while (-1 != (iQread = jBytes.readBytes(ba)))
+if (iQread > 0) out.write(ba, 0, iQread);
+out.close();
+return out.toByteArray();
+}
 
-     if (jMess instanceof BytesMessage)
-     {
-        BytesMessage jBytes = (BytesMessage) jMess;
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        byte[] ba = new byte[1000];
-        int iQread;
-        while (-1 != (iQread = jBytes.readBytes(ba)))
-           if (iQread > 0) out.write(ba, 0, iQread);
-        out.close();
-        return out.toByteArray();
-     }
-
-     if (jMess instanceof ObjectMessage)
-        return ((ObjectMessage) jMess).getObject().toString()
-              .getBytes();
-     JmsGatewayListener._logger
-             .warn("Message type " + jMess.getClass().getSimpleName() + " not supported - Message is ignored");
-     return null;
-  }
+if (jMess instanceof ObjectMessage)
+return ((ObjectMessage) jMess).getObject().toString()
+.getBytes();
+JmsGatewayListener._logger
+.warn("Message type " + jMess.getClass().getSimpleName() + " not supported - Message is ignored");
+return null;
+}
 } // ____________________________________________________
+	
\ No newline at end of file

Added: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContentsUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContentsUnitTest.java	                        (rev 0)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/PackageJmsMessageContentsUnitTest.java	2007-07-12 06:29:32 UTC (rev 13391)
@@ -0,0 +1,124 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc., and
+ * individual contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of individual
+ * contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY 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
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.listeners.gateway;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.jms.JMSException;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.addressing.eprs.JMSEpr;
+import org.jboss.soa.esb.message.Message;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockejb.jms.MockQueue;
+import org.mockejb.jms.ObjectMessageImpl;
+
+/**
+ * Unit test for PackageJmsMessageContens
+ * 
+ * @author <a href="daniel.bevenius at redpill.se">Daniel Bevenius</a>				
+ *
+ */
+public class PackageJmsMessageContentsUnitTest
+{
+	@SuppressWarnings ( "unused" )
+	private Logger log = Logger.getLogger( PackageJmsMessageContentsUnitTest.class );
+	
+	private final static String messageContent = "Test Message Content";
+	private final static String jmsMessageID = "123456780";
+	private final static String jmsCorrelationID = "YYXX-123456780-GG";
+	
+	private PackageJmsMessageContents packer;
+	private ObjectMessageImpl objectMsg;
+	
+	@Before
+	public void setup()
+	{
+		packer = new PackageJmsMessageContents();
+		objectMsg = new ObjectMessageImpl();
+	}
+	
+	@Test
+	public void process_with_content() throws JMSException, IOException, URISyntaxException
+	{
+		objectMsg.setObject( messageContent );
+		Message message = packer.process( objectMsg );
+		final String actualContent = new String ( message.getBody().getByteArray() );
+		assertEquals ( messageContent, actualContent );
+	}
+	
+	@Test
+	public void process_with_JMSMessageID() throws JMSException, IOException, URISyntaxException
+	{
+		objectMsg.setObject( messageContent );
+		objectMsg.setJMSMessageID( jmsMessageID );
+		
+		Message message = packer.process( objectMsg );
+		
+		final String actualContent = new String ( message.getBody().getByteArray() );
+		assertEquals ( messageContent, actualContent );
+		assertEquals ( new URI( jmsMessageID ), message.getHeader().getCall().getMessageID()  );
+	}
+	
+	@Test
+	public void process_with_JMSCorrelationID() throws JMSException, IOException, URISyntaxException
+	{
+		objectMsg.setObject( messageContent );
+		objectMsg.setJMSCorrelationID( jmsCorrelationID );
+		
+		Message message = packer.process( objectMsg );
+		
+		assertEquals ( new URI( jmsCorrelationID ), message.getHeader().getCall().getRelatesTo()  );
+	}
+	
+	@Test
+	public void process_with_JMSReplyTo() throws JMSException, IOException, URISyntaxException
+	{
+		objectMsg.setObject( messageContent );
+		MockQueue jmsReplyToQueue = new MockQueue( "mockReplyToQueueName");
+		objectMsg.setJMSReplyTo( jmsReplyToQueue );
+		
+		Message message = packer.process( objectMsg );
+		
+		EPR replyTo = message.getHeader().getCall().getReplyTo();
+		assertTrue(  replyTo instanceof JMSEpr );
+		JMSEpr jmsEpr = (JMSEpr) replyTo;
+		assertEquals ( jmsReplyToQueue.getQueueName() , jmsEpr.getDestinationName() );
+	}
+	
+	/**
+	 * Just here to get Ant to find annotated test.
+	 */
+	public static junit.framework.Test suite()
+	{
+		return new JUnit4TestAdapter( PackageJmsMessageContentsUnitTest.class );
+	}
+	
+}




More information about the jboss-svn-commits mailing list