git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@814586 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2009-09-14 11:34:34 +00:00
parent 7167041ee6
commit 5ab78c1234
11 changed files with 777 additions and 30 deletions

View File

@ -47,7 +47,8 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<artifactId>spring</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
@ -92,7 +93,6 @@
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
@ -132,6 +132,7 @@
<artifactId>stax-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
@ -139,29 +140,9 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<scope>test</scope>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-oxm-tiger</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
@ -171,9 +152,8 @@
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,149 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util.oxm;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.MessageTransformerSupport;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
/**
* Abstract class used as a base for implementing transformers from object to text messages (in XML/JSON format)
* and vice versa using.
* Supports plugging of custom marshallers
*/
public abstract class AbstractXMLMessageTransformer extends
MessageTransformerSupport {
protected MessageTransform transformType;
/**
* Defines the type of transformation. If XML (default), - producer
* transformation transforms from Object to XML. - consumer transformation
* transforms from XML to Object. 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
*/
public enum MessageTransform {
XML, OBJECT, ADAPTIVE
};
public AbstractXMLMessageTransformer() {
this(MessageTransform.XML);
}
public AbstractXMLMessageTransformer(MessageTransform transformType) {
this.transformType = transformType;
}
public Message consumerTransform(Session session, MessageConsumer consumer, Message message) throws JMSException {
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;
default:
}
return message;
}
public Message producerTransform(Session session, MessageProducer producer, Message message) throws JMSException {
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;
default:
}
return message;
}
public MessageTransform getTransformType() {
return transformType;
}
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 textMessage - text message to transform to object message
* @return ObjectMessage
* @throws JMSException
*/
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);
}
}
/**
* Transforms an incoming {@link ObjectMessage} to an XML encoded
* {@link TextMessage}
*
* @param session - JMS session currently being used
* @param objectMessage - object message to transform to text message
* @return XML encoded TextMessage
* @throws JMSException
*/
protected TextMessage objectToText(Session session, ObjectMessage objectMessage) throws JMSException {
TextMessage answer = session.createTextMessage(marshall(session, objectMessage));
copyProperties(objectMessage, answer);
return answer;
}
/**
* Marshalls the Object in the {@link ObjectMessage} to a string using XML
* encoding
*/
protected abstract String marshall(Session session, ObjectMessage objectMessage) throws JMSException;
/**
* Unmarshalls the XML encoded message in the {@link TextMessage} to an
* Object
*/
protected abstract Object unmarshall(Session session, TextMessage textMessage) throws JMSException;
}

View File

@ -0,0 +1,78 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util.oxm;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.oxm.AbstractMarshaller;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
/**
* Transforms object messages to text messages and vice versa using {@link Spring OXM}
*
*/
public class OXMMessageTransformer extends AbstractXMLMessageTransformer {
/**
* OXM marshaller used to marshall/unmarshall messages
*/
private AbstractMarshaller marshaller;
public AbstractMarshaller getMarshaller() {
return marshaller;
}
public void setMarshaller(AbstractMarshaller marshaller) {
this.marshaller = marshaller;
}
/**
* Marshalls the Object in the {@link ObjectMessage} to a string using XML
* encoding
*/
protected String marshall(Session session, ObjectMessage objectMessage)
throws JMSException {
StringResult result = new StringResult();
try {
marshaller.marshal(objectMessage.getObject(), result);
return result.toString();
} catch (Exception e) {
throw new JMSException(e.getMessage());
}
}
/**
* Unmarshalls the XML encoded message in the {@link TextMessage} to an
* Object
*/
protected Object unmarshall(Session session, TextMessage textMessage)
throws JMSException {
try {
return marshaller.unmarshal(new StringSource(textMessage.getText()));
} catch (Exception e) {
throw new JMSException(e.getMessage());
}
}
}

View File

@ -0,0 +1,108 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util.oxm;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
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;
import com.thoughtworks.xstream.io.xml.XppReader;
/**
* Transforms object messages to text messages and vice versa using
* {@link XStream}
*
*/
public class XStreamMessageTransformer extends AbstractXMLMessageTransformer {
private XStream xStream;
/**
* Specialized driver to be used with stream readers and writers
*/
private HierarchicalStreamDriver streamDriver;
// Properties
// -------------------------------------------------------------------------
public XStream getXStream() {
if (xStream == null) {
xStream = createXStream();
}
return xStream;
}
public void setXStream(XStream xStream) {
this.xStream = xStream;
}
public HierarchicalStreamDriver getStreamDriver() {
return streamDriver;
}
public void setStreamDriver(HierarchicalStreamDriver streamDriver) {
this.streamDriver = streamDriver;
}
// Implementation methods
// -------------------------------------------------------------------------
protected XStream createXStream() {
return new XStream();
}
/**
* Marshalls the Object in the {@link ObjectMessage} to a string using XML
* encoding
*/
protected String marshall(Session session, ObjectMessage objectMessage) throws JMSException {
Serializable object = objectMessage.getObject();
StringWriter buffer = new StringWriter();
HierarchicalStreamWriter out;
if (streamDriver != null) {
out = streamDriver.createWriter(buffer);
} else {
out = new PrettyPrintWriter(buffer);
}
getXStream().marshal(object, out);
return buffer.toString();
}
/**
* Unmarshalls the XML encoded message in the {@link TextMessage} to an
* Object
*/
protected Object unmarshall(Session session, TextMessage textMessage) throws JMSException {
HierarchicalStreamReader in;
if (streamDriver != null) {
in = streamDriver.createReader(new StringReader(textMessage.getText()));
} else {
in = new XppReader(new StringReader(textMessage.getText()));
}
return getXStream().unmarshal(in);
}
}

View File

@ -28,6 +28,8 @@ import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.MessageTransformerSupport;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
@ -35,14 +37,15 @@ import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppReader;
import org.apache.activemq.MessageTransformerSupport;
/**
* Transforms object messages to text messages and vice versa using
* {@link XStream}
*
* @deprecated as of 5.3.0 release replaced by {@link org.apache.activemq.util.oxm.XStreamMessageTransformer}
*
* @version $Revision$
*/
@Deprecated
public class XStreamMessageTransformer extends MessageTransformerSupport {
protected MessageTransform transformType;

View File

@ -0,0 +1,241 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util.oxm;
import static org.apache.activemq.util.oxm.AbstractXMLMessageTransformer.MessageTransform.ADAPTIVE;
import static org.apache.activemq.util.oxm.AbstractXMLMessageTransformer.MessageTransform.OBJECT;
import static org.apache.activemq.util.oxm.AbstractXMLMessageTransformer.MessageTransform.XML;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQMessageConsumer;
import org.apache.activemq.MessageTransformer;
import org.apache.activemq.util.xstream.SamplePojo;
import org.apache.activemq.util.xstream.XStreamMessageTransformer;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public abstract class AbstractXMLMessageTransformerTest extends TestCase {
protected ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
protected Connection connection;
protected long timeout = 5000;
protected Connection createConnection(MessageTransformer transformer) throws Exception {
connectionFactory.setTransformer(transformer);
connection = connectionFactory.createConnection();
connection.start();
return connection;
}
protected abstract AbstractXMLMessageTransformer createTransformer();
public void testSendObjectMessageReceiveAsTextMessageAndObjectMessage() throws Exception {
AbstractXMLMessageTransformer transformer = createTransformer();
transformer.setTransformType(XML);
connection = createConnection(transformer);
// lets create the consumers
Session objectSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = objectSession.createTopic(getClass().getName());
MessageConsumer objectConsumer = objectSession.createConsumer(destination);
Session textSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer textConsumer = textSession.createConsumer(destination);
// lets clear the transformer on this consumer so we see the message as
// it really is
((ActiveMQMessageConsumer)textConsumer).setTransformer(null);
// send a message
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = producerSession.createProducer(destination);
ObjectMessage request = producerSession.createObjectMessage(new SamplePojo("James", "London"));
producer.send(request);
// lets consume it as an object message
Message message = objectConsumer.receive(timeout);
assertNotNull("Should have received a message!", message);
assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage);
ObjectMessage objectMessage = (ObjectMessage)message;
Object object = objectMessage.getObject();
assertTrue("object payload of wrong type: " + object, object instanceof SamplePojo);
SamplePojo body = (SamplePojo)object;
assertEquals("name", "James", body.getName());
assertEquals("city", "London", body.getCity());
// lets consume it as a text message
message = textConsumer.receive(timeout);
assertNotNull("Should have received a message!", message);
assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage);
TextMessage textMessage = (TextMessage)message;
String text = textMessage.getText();
assertTrue("Text should be non-empty!", text != null && text.length() > 0);
System.out.println("Received XML...");
System.out.println(text);
}
public void testSendTextMessageReceiveAsObjectMessageAndTextMessage() throws Exception {
AbstractXMLMessageTransformer transformer = createTransformer();
transformer.setTransformType(OBJECT);
connection = createConnection(transformer);
// lets create the consumers
Session textSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = textSession.createTopic(getClass().getName());
MessageConsumer textConsumer = textSession.createConsumer(destination);
Session objectSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer objectConsumer = objectSession.createConsumer(destination);
// lets clear the transformer on this consumer so we see the message as
// it really is
((ActiveMQMessageConsumer)objectConsumer).setTransformer(null);
// send a message
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = producerSession.createProducer(destination);
String xmlText = "<org.apache.activemq.util.xstream.SamplePojo>"
+ "<name>James</name>"
+ "<city>London</city>"
+ "</org.apache.activemq.util.xstream.SamplePojo>";
TextMessage request = producerSession.createTextMessage(xmlText);
producer.send(request);
Message message;
// lets consume it as a text message
message = textConsumer.receive(timeout);
assertNotNull("Should have received a message!", message);
assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage);
TextMessage textMessage = (TextMessage)message;
String text = textMessage.getText();
assertTrue("Text should be non-empty!", text != null && text.length() > 0);
// lets consume it as an object message
message = objectConsumer.receive(timeout);
assertNotNull("Should have received a message!", message);
assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage);
ObjectMessage objectMessage = (ObjectMessage)message;
Object object = objectMessage.getObject();
assertTrue("object payload of wrong type: " + object, object instanceof SamplePojo);
SamplePojo body = (SamplePojo)object;
assertEquals("name", "James", body.getName());
assertEquals("city", "London", body.getCity());
}
public void testAdaptiveTransform() throws Exception {
AbstractXMLMessageTransformer transformer = createTransformer();
transformer.setTransformType(ADAPTIVE);
connection = createConnection(transformer);
// 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);
}
protected void tearDown() throws Exception {
if (connection != null) {
connection.close();
}
}
}

View File

@ -0,0 +1,30 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util.oxm;
import org.springframework.oxm.xstream.XStreamMarshaller;
public class OXMMessageTransformTest extends AbstractXMLMessageTransformerTest {
protected AbstractXMLMessageTransformer createTransformer() {
OXMMessageTransformer transformer = new OXMMessageTransformer();
transformer.setMarshaller(new XStreamMarshaller());
return transformer;
}
}

View File

@ -0,0 +1,136 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util.oxm;
import static org.apache.activemq.util.oxm.AbstractXMLMessageTransformer.MessageTransform.ADAPTIVE;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQMessageConsumer;
import org.apache.activemq.util.xstream.SamplePojo;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class XStreamMessageTransformTest extends
AbstractXMLMessageTransformerTest {
protected AbstractXMLMessageTransformer createTransformer() {
return new XStreamMessageTransformer();
}
public void testStreamDriverTransform() throws Exception {
XStreamMessageTransformer transformer = (XStreamMessageTransformer) createTransformer();
transformer.setTransformType(ADAPTIVE);
transformer.setStreamDriver(new JettisonMappedXmlDriver());
connection = createConnection(transformer);
// 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);
}
}

View File

@ -234,6 +234,14 @@
<artifactId>spring-webmvc</artifactId>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-oxm-tiger</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>

View File

@ -202,6 +202,8 @@
<include>xmlpull:xmlpull</include>
<include>org.apache.hadoop.zookeeper:zookeeper</include>
<include>org.codehaus.woodstox:wstx-asl</include>
<include>org.springframework.ws:spring-oxm-tiger</include>
<include>org.codehaus.jettison:jettison</include>
</includes>
</dependencySet>
<dependencySet>

14
pom.xml
View File

@ -62,6 +62,7 @@
<hsqldb-version>1.7.2.2</hsqldb-version>
<jdom-version>1.0</jdom-version>
<jetty-version>6.1.9</jetty-version>
<jettison-version>1.1</jettison-version>
<jmock-version>2.5.1</jmock-version>
<junit-version>4.4</junit-version>
<jxta-version>2.0</jxta-version>
@ -72,6 +73,7 @@
<regexp-version>1.3</regexp-version>
<rome-version>0.8</rome-version>
<slf4j-version>1.5.0</slf4j-version>
<spring-oxm-version>1.5.8</spring-oxm-version>
<stax-version>1.2.0</stax-version>
<xalan-version>2.6.0</xalan-version>
<xmlbeans-version>2.0.0-beta1</xmlbeans-version>
@ -92,7 +94,7 @@
<activemq.osgi.symbolic.name>${groupId}.${artifactId}</activemq.osgi.symbolic.name>
</properties>
<prerequisites>
<maven>2.0.10</maven>
<maven>2.0.9</maven>
</prerequisites>
<url>http://activemq.apache.org</url>
@ -592,6 +594,11 @@
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-oxm-tiger</artifactId>
<version>${spring-oxm-version}</version>
</dependency>
<!-- Optional Derby support-->
<dependency>
@ -817,6 +824,11 @@
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>${jettison-version}</version>
</dependency>
<dependency>
<groupId>annogen</groupId>