activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r548562 - in /activemq/trunk/activemq-optional/src: main/java/org/apache/activemq/util/xstream/XStreamMessageTransformer.java test/java/org/apache/activemq/util/xstream/XStreamTransformTest.java
Date Tue, 19 Jun 2007 02:33:24 GMT
Author: aco
Date: Mon Jun 18 19:33:23 2007
New Revision: 548562

URL: http://svn.apache.org/viewvc?view=rev&rev=548562
Log:
Added an adaptive message transformation. More details here: https://issues.apache.org/activemq/browse/AMQ-1208

Modified:
    activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/xstream/XStreamMessageTransformer.java
    activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/xstream/XStreamTransformTest.java

Modified: activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/xstream/XStreamMessageTransformer.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/xstream/XStreamMessageTransformer.java?view=diff&rev=548562&r1=548561&r2=548562
==============================================================================
--- activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/xstream/XStreamMessageTransformer.java
(original)
+++ activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/xstream/XStreamMessageTransformer.java
Mon Jun 18 19:33:23 2007
@@ -44,38 +44,69 @@
     private XStream xStream;
 
     /**
-     * Defines the direction in which transformation goes.
-     * If 'false' (default),
+     * Defines the type of transformation.
+     * If XML (default),
      *  - producer transformation transforms from Object to XML.
      *  - consumer transformation transforms from XML to Object.
-     * If 'true' transformation directions changes,
+     * If OBJECT,
      *  - producer transformation transforms from XML to Object.
      *  - consumer transformation transforms from Object to XML.
+     * If ADAPTIVE,
+     *  - producer transformation transforms from Object to XML, or XML to Object
+     *    depending on the type of the original message
+     *  - consumer transformation transforms from XML to Object, or Object to XML
+     *    depending on the type of the original message
 	 */
-	private boolean reverse;
+	public enum MessageTransform {XML, OBJECT, ADAPTIVE};
 
-	public XStreamMessageTransformer() {
-		new XStreamMessageTransformer(false);
+    protected MessageTransform transformType;
+
+    public XStreamMessageTransformer() {
+		this(MessageTransform.XML);
 	}
 
-	public XStreamMessageTransformer(boolean direction) {
-		this.reverse = direction;
+	public XStreamMessageTransformer(MessageTransform transformType) {
+		this.transformType = transformType;
 	}
 
     public Message consumerTransform(Session session, MessageConsumer consumer, Message message)
throws JMSException {
-		if (reverse) {
-			return objectToText(session, message);
-		} else {
-			return textToObject(session, message);
-		}
-	}
+		switch (transformType) {
+            case XML:
+                return (message instanceof TextMessage) ?
+                        textToObject(session, (TextMessage)message) :
+                        message;
+            case OBJECT:
+                return (message instanceof ObjectMessage) ?
+                        objectToText(session, (ObjectMessage)message) :
+                        message;
+            case ADAPTIVE:
+                return (message instanceof TextMessage) ?
+                        textToObject(session, (TextMessage)message) :
+                       (message instanceof ObjectMessage) ?
+                        objectToText(session, (ObjectMessage)message) :
+                        message;
+        }
+        return message;
+    }
 
 	public Message producerTransform(Session session, MessageProducer producer, Message message)
throws JMSException {
-		if (reverse) {
-			return textToObject(session, message);
-		} else {
-			return objectToText(session, message);
-		}
+		switch (transformType) {
+            case XML:
+                return (message instanceof ObjectMessage) ?
+                        objectToText(session, (ObjectMessage)message) :
+                        message;
+            case OBJECT:
+                return (message instanceof TextMessage) ?
+                        textToObject(session, (TextMessage)message) :
+                        message;
+            case ADAPTIVE:
+                return (message instanceof TextMessage) ?
+                        textToObject(session, (TextMessage)message) :
+                       (message instanceof ObjectMessage) ?
+                        objectToText(session, (ObjectMessage)message) :
+                        message;
+        }
+        return message;
 	}
 
     // Properties
@@ -97,52 +128,44 @@
         return new XStream();
     }
 
-
-    public boolean isReverse() {
-        return reverse;
+    public MessageTransform getTransformType() {
+        return transformType;
     }
 
-    public void setReverse(boolean reverse) {
-        this.reverse = reverse;
+    public void setTransformType(MessageTransform transformType) {
+        this.transformType = transformType;
     }
 
     /**
      * Transforms an incoming XML encoded {@link TextMessage} to an {@link ObjectMessage}
      * @param session - JMS session currently being used
-     * @param message - if this is a TextMessage, it will be transformed to an ObjectMessage
-     * @return ObjectMessage, if the incoming message is a TextMessage, the original message
otherwise
+     * @param textMessage - text message to transform to object message
+     * @return ObjectMessage
      * @throws JMSException
      */
-    protected Message textToObject(Session session, Message message) throws JMSException
{
-        if (message instanceof TextMessage) {
-            TextMessage textMessage = (TextMessage) message;
-            Object object = unmarshall(session, textMessage);
-            if (object instanceof Serializable) {
-                ObjectMessage answer = session.createObjectMessage((Serializable) object);
-                copyProperties(message, answer);
-                return answer;
-            }
-            else {
-                throw new JMSException("Object is not serializable: " + object);
-            }
+    protected ObjectMessage textToObject(Session session, TextMessage textMessage) throws
JMSException {
+        Object object = unmarshall(session, textMessage);
+        if (object instanceof Serializable) {
+            ObjectMessage answer = session.createObjectMessage((Serializable) object);
+            copyProperties(textMessage, answer);
+            return answer;
+        }
+        else {
+            throw new JMSException("Object is not serializable: " + object);
         }
-        return message;
 	}
 
     /**
      * Transforms an incoming {@link ObjectMessage} to an XML encoded {@link TextMessage}
      * @param session - JMS session currently being used
-     * @param message - if this is an ObjectMessage, it will be transformed to a TextMessage
-     * @return XML encoded TextMessage, if the incoming messsage is an ObjectMessge, the
original message otherwise
+     * @param objectMessage - object message to transform to text message
+     * @return XML encoded TextMessage
      * @throws JMSException
      */
-    protected Message objectToText(Session session, Message message) throws JMSException
{
-        if (message instanceof ObjectMessage) {
-            TextMessage answer = session.createTextMessage(marshall(session, (ObjectMessage)
message));
-            copyProperties(message, answer);
-            return answer;
-        }
-        return message;
+    protected TextMessage objectToText(Session session, ObjectMessage objectMessage) throws
JMSException {
+        TextMessage answer = session.createTextMessage(marshall(session, objectMessage));
+        copyProperties(objectMessage, answer);
+        return answer;
 	}
 
     /**

Modified: activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/xstream/XStreamTransformTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/xstream/XStreamTransformTest.java?view=diff&rev=548562&r1=548561&r2=548562
==============================================================================
--- activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/xstream/XStreamTransformTest.java
(original)
+++ activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/xstream/XStreamTransformTest.java
Mon Jun 18 19:33:23 2007
@@ -31,6 +31,8 @@
 import org.apache.activemq.ActiveMQConnectionFactory;
 import org.apache.activemq.ActiveMQMessageConsumer;
 
+import static org.apache.activemq.util.xstream.XStreamMessageTransformer.MessageTransform.*;
+
 /**
  * @version $Revision$
  */
@@ -40,7 +42,7 @@
     protected long timeout = 5000;
 
     public void testSendObjectMessageReceiveAsTextMessageAndObjectMessage() throws Exception
{
-        connectionFactory.setTransformer(new XStreamMessageTransformer());
+        connectionFactory.setTransformer(new XStreamMessageTransformer(XML));
         connection = connectionFactory.createConnection();
         connection.start();
 
@@ -87,8 +89,7 @@
     }
 
     public void testSendTextMessageReceiveAsObjectMessageAndTextMessage() throws Exception
{
-        // Set reverse to true
-        connectionFactory.setTransformer(new XStreamMessageTransformer(true));
+        connectionFactory.setTransformer(new XStreamMessageTransformer(OBJECT));
         connection = connectionFactory.createConnection();
         connection.start();
 
@@ -135,6 +136,89 @@
         SamplePojo body = (SamplePojo) object;
         assertEquals("name", "James", body.getName());
         assertEquals("city", "London", body.getCity());
+
+    }
+
+    public void testAdaptiveTransform() throws Exception {
+        connectionFactory.setTransformer(new XStreamMessageTransformer(ADAPTIVE));
+        connection = connectionFactory.createConnection();
+        connection.start();
+
+        // lets create the consumers
+        Session adaptiveSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+        Destination destination = adaptiveSession.createTopic(getClass().getName());
+        MessageConsumer adaptiveConsumer = adaptiveSession.createConsumer(destination);
+
+        Session origSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+        MessageConsumer origConsumer = origSession.createConsumer(destination);
+        // lets clear the transformer on this consumer so we see the message as it really
is
+        ((ActiveMQMessageConsumer) origConsumer).setTransformer(null);
+
+        // Create producer
+        Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+        MessageProducer producer = producerSession.createProducer(destination);
+
+        Message message;
+        ObjectMessage objectMessage;
+        TextMessage textMessage;
+        SamplePojo body;
+        Object object;
+        String text;
+
+        // Send a text message
+        String xmlText =
+                "<org.apache.activemq.util.xstream.SamplePojo>" +
+                "<name>James</name>" +
+                "<city>London</city>" +
+                "</org.apache.activemq.util.xstream.SamplePojo>";
+
+        TextMessage txtRequest = producerSession.createTextMessage(xmlText);
+        producer.send(txtRequest);
+
+        // lets consume it as a text message
+        message = adaptiveConsumer.receive(timeout);
+        assertNotNull("Should have received a message!", message);
+        assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage);
+        textMessage = (TextMessage) message;
+        text = textMessage.getText();
+        assertTrue("Text should be non-empty!", text != null && text.length() >
0);
+
+        // lets consume it as an object message
+        message = origConsumer.receive(timeout);
+        assertNotNull("Should have received a message!", message);
+        assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage);
+        objectMessage = (ObjectMessage) message;
+        object = objectMessage.getObject();
+        assertTrue("object payload of wrong type: " + object, object instanceof SamplePojo);
+        body = (SamplePojo) object;
+        assertEquals("name", "James", body.getName());
+        assertEquals("city", "London", body.getCity());
+
+        // Send object message
+        ObjectMessage objRequest = producerSession.createObjectMessage(new SamplePojo("James",
"London"));
+        producer.send(objRequest);
+
+        // lets consume it as an object message
+        message = adaptiveConsumer.receive(timeout);
+        assertNotNull("Should have received a message!", message);
+        assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage);
+        objectMessage = (ObjectMessage) message;
+        object = objectMessage.getObject();
+        assertTrue("object payload of wrong type: " + object, object instanceof SamplePojo);
+        body = (SamplePojo) object;
+        assertEquals("name", "James", body.getName());
+        assertEquals("city", "London", body.getCity());
+
+
+        // lets consume it as a text message
+        message = origConsumer.receive(timeout);
+        assertNotNull("Should have received a message!", message);
+        assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage);
+        textMessage = (TextMessage) message;
+        text = textMessage.getText();
+        assertTrue("Text should be non-empty!", text != null && text.length() >
0);
+        System.out.println("Received XML...");
+        System.out.println(text);
 
     }
 



Mime
View raw message