activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dej...@apache.org
Subject svn commit: r729844 - in /activemq/trunk: ./ activemq-optional/ activemq-optional/src/main/java/org/apache/activemq/util/xstream/ activemq-optional/src/test/java/org/apache/activemq/util/xstream/
Date Mon, 29 Dec 2008 09:20:09 GMT
Author: dejanb
Date: Mon Dec 29 01:20:09 2008
New Revision: 729844

URL: http://svn.apache.org/viewvc?rev=729844&view=rev
Log:
fix for https://issues.apache.org/activemq/browse/AMQ-1494

Modified:
    activemq/trunk/activemq-optional/pom.xml
    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
    activemq/trunk/pom.xml

Modified: activemq/trunk/activemq-optional/pom.xml
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-optional/pom.xml?rev=729844&r1=729843&r2=729844&view=diff
==============================================================================
--- activemq/trunk/activemq-optional/pom.xml (original)
+++ activemq/trunk/activemq-optional/pom.xml Mon Dec 29 01:20:09 2008
@@ -133,6 +133,12 @@
       <scope>compile</scope>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.jettison</groupId>
+      <artifactId>jettison</artifactId>
+      <version>1.0</version>
+      <scope>test</scope>
+    </dependency>     
   </dependencies>
 
   <build>

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?rev=729844&r1=729843&r2=729844&view=diff
==============================================================================
--- 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 Dec 29 01:20:09 2008
@@ -29,6 +29,7 @@
 import javax.jms.TextMessage;
 
 import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
@@ -46,6 +47,11 @@
 
     protected MessageTransform transformType;
     private XStream xStream;
+    
+    /**
+     * Specialized driver to be used with stream readers and writers
+     */
+    private HierarchicalStreamDriver streamDriver;
 
     /**
      * Defines the type of transformation. If XML (default), - producer
@@ -109,7 +115,15 @@
         this.xStream = xStream;
     }
 
-    // Implementation methods
+    public HierarchicalStreamDriver getStreamDriver() {
+		return streamDriver;
+    }
+
+	public void setStreamDriver(HierarchicalStreamDriver streamDriver) {
+		this.streamDriver = streamDriver;
+    }
+
+	// Implementation methods
     // -------------------------------------------------------------------------
     protected XStream createXStream() {
         return new XStream();
@@ -165,7 +179,12 @@
     protected String marshall(Session session, ObjectMessage objectMessage) throws JMSException
{
         Serializable object = objectMessage.getObject();
         StringWriter buffer = new StringWriter();
-        HierarchicalStreamWriter out = new PrettyPrintWriter(buffer);
+        HierarchicalStreamWriter out;
+        if (streamDriver != null) {
+        	out = streamDriver.createWriter(buffer);
+        } else {
+        	out = new PrettyPrintWriter(buffer);
+        }
         getXStream().marshal(object, out);
         return buffer.toString();
     }
@@ -175,7 +194,12 @@
      * Object
      */
     protected Object unmarshall(Session session, TextMessage textMessage) throws JMSException
{
-        HierarchicalStreamReader in = new XppReader(new StringReader(textMessage.getText()));
+        HierarchicalStreamReader in;
+        if (streamDriver != null) {
+        	in = streamDriver.createReader(new StringReader(textMessage.getText()));
+        } else {
+        	in = new XppReader(new StringReader(textMessage.getText()));
+        }
         return getXStream().unmarshal(in);
     }
 

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?rev=729844&r1=729843&r2=729844&view=diff
==============================================================================
--- 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 Dec 29 01:20:09 2008
@@ -30,6 +30,8 @@
 import org.apache.activemq.ActiveMQConnectionFactory;
 import org.apache.activemq.ActiveMQMessageConsumer;
 
+import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
+
 import static org.apache.activemq.util.xstream.XStreamMessageTransformer.MessageTransform.*;
 
 /**
@@ -216,6 +218,87 @@
         System.out.println(text);
 
     }
+    
+    public void testStreamDriverTransform() throws Exception {
+    	XStreamMessageTransformer transformer = new XStreamMessageTransformer(ADAPTIVE);
+    	transformer.setStreamDriver(new JettisonMappedXmlDriver());
+        connectionFactory.setTransformer(transformer);
+        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\",\"city\":\"London\"}}";
+
+        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 JSON...");
+        System.out.println(text);
+
+    }
 
     protected void tearDown() throws Exception {
         if (connection != null) {

Modified: activemq/trunk/pom.xml
URL: http://svn.apache.org/viewvc/activemq/trunk/pom.xml?rev=729844&r1=729843&r2=729844&view=diff
==============================================================================
--- activemq/trunk/pom.xml (original)
+++ activemq/trunk/pom.xml Mon Dec 29 01:20:09 2008
@@ -75,7 +75,7 @@
     <xalan-version>2.6.0</xalan-version>
     <xmlbeans-version>2.0.0-beta1</xmlbeans-version>
     <xmlpull-version>1.1.3.4d_b4_min</xmlpull-version>
-    <xstream-version>1.3</xstream-version>
+    <xstream-version>1.3.1</xstream-version>
     <xbean-version>3.4</xbean-version>
     <felix-version>1.0.0</felix-version>
     <dist-repo-url>scpexe://people.apache.org/www/people.apache.org/repo/m2-incubating-repository</dist-repo-url>



Mime
View raw message