From fea84bc2dd440ae3c7d36990ad24455166455415 Mon Sep 17 00:00:00 2001 From: slaws Date: Fri, 24 Apr 2009 11:49:38 +0000 Subject: TUSCANY-2930 main part of separating wireFormat.jmsdefault out from wireFormat.jmsTextXML. I've spotted a few more things that need fixing but this basic functionality passes all of our current JMS tests and now better matched what the specification says about default format. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@768263 13f79535-47bb-0310-9956-ffa450edef68 --- .../jms/provider/DefaultMessageProcessor.java | 267 +++++++++++++++++++++ .../binding/jms/provider/RRBJMSBindingInvoker.java | 6 +- 2 files changed, 268 insertions(+), 5 deletions(-) create mode 100644 branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/DefaultMessageProcessor.java (limited to 'branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider') diff --git a/branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/DefaultMessageProcessor.java b/branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/DefaultMessageProcessor.java new file mode 100644 index 0000000000..76f069c6d9 --- /dev/null +++ b/branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/DefaultMessageProcessor.java @@ -0,0 +1,267 @@ +/* + * 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.tuscany.sca.binding.jms.provider; + +import java.io.ByteArrayInputStream; +import java.io.StringReader; +import java.util.logging.Logger; + +import javax.jms.BytesMessage; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMNode; +import org.apache.axiom.om.impl.builder.StAXOMBuilder; +import org.apache.tuscany.sca.binding.jms.impl.JMSBinding; +import org.apache.tuscany.sca.binding.jms.impl.JMSBindingConstants; +import org.apache.tuscany.sca.binding.jms.impl.JMSBindingException; +import org.apache.tuscany.sca.interfacedef.util.FaultException; + +/** + * MessageProcessor for sending/receiving XML over javax.jms.TextMessage or javax.jms.BytesMessage + * with the JMSBinding. + * This is very specific to the default wire format and is not tied into the usual hierarchy + * of message processors + * + * @version $Rev$ $Date$ + */ +public class DefaultMessageProcessor extends AbstractMessageProcessor { + private static final Logger logger = Logger.getLogger(DefaultMessageProcessor.class.getName()); + + public DefaultMessageProcessor(JMSBinding jmsBinding) { + super(jmsBinding); + } + + // inherited methods that don't do anything useful + @Override + protected Message createJMSMessage(Session session, Object o) { + // should not be used + return null; + } + + @Override + protected Object extractPayload(Message msg) { + // if it's not a text/bytes message or a fault then we don;t know what to do with it + return null; + } + + // TODO - This makes the assumption that whatever the text/bytes configuration of the + // jms binding, unchecked faults will be sent as bytes. + @Override + public Message createFaultMessage(Session session, Throwable o) { + return createFaultJMSBytesMessage(session, o); + } + + // handle text messages + + public Object extractPayloadFromJMSTextMessage(Message msg, OMElement wrapper) { + if (msg instanceof TextMessage) { + try { + String xml = ((TextMessage) msg).getText(); + Object os; + if (xml != null) { + XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml)); + StAXOMBuilder builder = new StAXOMBuilder(reader); + os = builder.getDocumentElement(); + } else { + os = null; + } + + if (wrapper != null){ + //don't modify the original wrapper since it will be reused + //clone the wrapper + OMElement newWrapper = wrapper.cloneOMElement(); + newWrapper.addChild((OMNode)os); + return newWrapper; + } + + return os; + + } catch (XMLStreamException e) { + throw new JMSBindingException(e); + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } else { + // handle the non-text fault case + return super.extractPayloadFromJMSMessage(msg); + } + } + + public Message insertPayloadIntoJMSTextMessage(Session session, Object o, boolean unwrap) { + + try { + + TextMessage message = session.createTextMessage(); + + if (o instanceof OMElement) { + + if (unwrap){ + if (((OMElement)o).getFirstElement() != null ){ + message.setText(((OMElement)o).getFirstElement().toString()); + } + }else { + message.setText(o.toString()); + } + } else if ((o instanceof Object[]) && ((Object[]) o)[0] instanceof OMElement) { + if (unwrap){ + message.setText(((OMElement)((Object[]) o)[0]).getFirstElement().toString()); + //message.setText((((Object[]) o)[0]).toString()); + }else { + message.setText(((Object[]) o)[0].toString()); + } + } else if (o != null) { + throw new IllegalStateException("expecting OMElement payload: " + o); + } + + return message; + + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } + + public Message createFaultJMSTextMessage(Session session, Throwable o) { + + if (session == null) { + logger.fine("no response session to create fault message: " + String.valueOf(o)); + return null; + } + if (o instanceof FaultException) { + try { + + TextMessage message = session.createTextMessage(); + message.setText(String.valueOf(((FaultException) o).getFaultInfo())); + message.setBooleanProperty(JMSBindingConstants.FAULT_PROPERTY, true); + return message; + + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } else { + // handle the non XML fault case + return super.createFaultMessage(session, o); + } + } + + // handle bytes messages + + public Object extractPayloadFromJMSBytesMessage(Message msg, OMElement wrapper) { + + if (msg instanceof BytesMessage) { + try { + Object os; + + long noOfBytes = ((BytesMessage) msg).getBodyLength(); + byte[] bytes = new byte[(int) noOfBytes]; + ((BytesMessage) msg).readBytes(bytes); + + if ((bytes != null) && (bytes.length > 0)) { + XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(bytes)); + StAXOMBuilder builder = new StAXOMBuilder(reader); + os = builder.getDocumentElement(); + } else { + os = null; + } + + if (wrapper != null){ + //don't modify the original wrapper since it will be reused + //clone the wrapper + OMElement newWrapper = wrapper.cloneOMElement(); + if (os != null){ + newWrapper.addChild((OMNode)os); + } + return newWrapper; + } + + return os; + + } catch (XMLStreamException e) { + throw new JMSBindingException(e); + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } else { + // trap the non-bytes fault case + return super.extractPayloadFromJMSMessage(msg); + } + } + + public Message insertPayloadIntoJMSBytesMessage(Session session, Object o, boolean unwrap) { + + try { + + BytesMessage message = session.createBytesMessage(); + + + if (o instanceof OMElement) { + if (unwrap) { + if (((OMElement)o).getFirstElement() != null){ + message.writeBytes(((OMElement)o).getFirstElement().toString().getBytes()); + } + } else { + message.writeBytes(o.toString().getBytes()); + } + + } else if ((o instanceof Object[]) && ((Object[]) o)[0] instanceof OMElement) { + if (unwrap){ + message.writeBytes(((OMElement)((Object[]) o)[0]).getFirstElement().toString().getBytes()); + }else { + message.writeBytes(((Object[]) o)[0].toString().getBytes()); + } + } else if (o != null) { + throw new IllegalStateException("expecting OMElement payload: " + o); + } + + return message; + + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } + + public Message createFaultJMSBytesMessage(Session session, Throwable o) { + + if (session == null) { + logger.fine("no response session to create fault message: " + String.valueOf(o)); + return null; + } + + if (o instanceof FaultException) { + try { + + BytesMessage message = session.createBytesMessage(); + message.writeBytes(String.valueOf(((FaultException) o).getFaultInfo()).getBytes()); + message.setBooleanProperty(JMSBindingConstants.FAULT_PROPERTY, true); + return message; + + } catch (JMSException e) { + throw new JMSBindingException(e); + } + } else { + return super.createFaultMessage(session, o); + } + } +} diff --git a/branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/RRBJMSBindingInvoker.java b/branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/RRBJMSBindingInvoker.java index e2b75de5b9..ee9501bf89 100644 --- a/branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/RRBJMSBindingInvoker.java +++ b/branches/sca-java-1.x/modules/binding-jms-runtime/src/main/java/org/apache/tuscany/sca/binding/jms/provider/RRBJMSBindingInvoker.java @@ -49,8 +49,6 @@ public class RRBJMSBindingInvoker implements Invoker, DataExchangeSemantics { protected JMSBinding jmsBinding; protected JMSResourceFactory jmsResourceFactory; - protected JMSMessageProcessor requestMessageProcessor; - protected JMSMessageProcessor responseMessageProcessor; protected Destination bindingRequestDest; protected Destination bindingReplyDest; protected RuntimeComponentReference reference; @@ -65,9 +63,7 @@ public class RRBJMSBindingInvoker implements Invoker, DataExchangeSemantics { this.jmsResourceFactory = jmsResourceFactory; this.reference = reference; this.runtimeWire = reference.getRuntimeWire(jmsBinding); - this.requestMessageProcessor = JMSMessageProcessorUtil.getRequestMessageProcessor(jmsBinding); - this.responseMessageProcessor = JMSMessageProcessorUtil.getResponseMessageProcessor(jmsBinding); - + try { // If this is a callback reference, the destination is determined dynamically based on // properties of the inbound service request. We should not look for or require a -- cgit v1.2.3