Hi Andreas, +1 from me.
I have now got a working version of the MapMessage support, and am looking forward to replace the hacks I've done to get the thing to work with some solid code. I agree with what you mean in SYNAPSE-304. I indeed had to introduce another method, getMessageMapPayload which will attempt to retrieve a Map. It would be great if you could consider the requirements [1] would need as well when fixing SYNAPSE-304, or else I'll still not be able to make the maximum out of the OMSourcedElement unless I do some further modifications. [1] webservices/commons/trunk/scratch/senaka/sci-flex/transport Thanks, Senaka On Mon, Oct 27, 2008 at 12:05 AM, Andreas Veithen <[EMAIL PROTECTED] > wrote: > I think that the introduction of MapMessageInputStream makes you loose > the benefit of representing the MapMessage using an OMSourcedElement. > Indeed the code will serialize the XML representation of the > MapMessage into a byte array only the parse it into an AXIOM tree > again immediately afterwards. I guess that you have chosen this > approach because the JMS transport is centered around input streams. > As discussed earlier this should change in the future. Maybe this is > the right moment to solve SYNAPSE-304 for the JMS transport so that > you have a more solid basis to implement MapMessage support. WDYT? > > Andreas > > On Mon, Oct 20, 2008 at 21:46, <[EMAIL PROTECTED]> wrote: > > Author: senaka > > Date: Mon Oct 20 13:46:11 2008 > > New Revision: 706401 > > > > URL: http://svn.apache.org/viewvc?rev=706401&view=rev > > Log: > > Adding JMS MapMessage support. > > > > Added: > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/MapMessageInputStream.java > > Modified: > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/pom.xml > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseConstants.java > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseUtils.java > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/pom.xml > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSConstants.java > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSMessageReceiver.java > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSSender.java > > > > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/pom.xml > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/pom.xml?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/pom.xml > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/pom.xml > Mon Oct 20 13:46:11 2008 > > @@ -92,6 +92,11 @@ > > <artifactId>axiom-dom</artifactId> > > <version>${axiom.version}</version> > > </dependency> > > + <dependency> > > + <groupId>org.apache.ws.commons.axiom</groupId> > > + <artifactId>sciflex-axiom-patch</artifactId> > > + <version>${sciflex-axiom-patch.version}</version> > > + </dependency> > > <!-- Axis2 --> > > <dependency> > > <groupId>org.apache.axis2</groupId> > > @@ -148,6 +153,7 @@ > > <axiom.version>SNAPSHOT</axiom.version> > > <axis2.version>SNAPSHOT</axis2.version> > > <commons.logging.version>1.1</commons.logging.version> > > + > <sciflex-axiom-patch.version>0.9-SNAPSHOT</sciflex-axiom-patch.version> > > </properties> > > > > </project> > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseConstants.java > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseConstants.java?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseConstants.java > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseConstants.java > Mon Oct 20 13:46:11 2008 > > @@ -72,6 +72,12 @@ > > public static final QName DEFAULT_TEXT_WRAPPER = > > new QName(AXIOMPAYLOADNS, "text"); > > > > + /** > > + * The name of the element which wraps map content into a SOAP > envelope > > + */ > > + public static final QName DEFAULT_MAP_WRAPPER = > > + new QName(AXIOMPAYLOADNS, "map"); > > + > > //-------------------------- services.xml parameters > -------------------------------- > > /** > > * The Parameter name indicating the operation to dispatch non > SOAP/XML messages > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseUtils.java > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseUtils.java?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseUtils.java > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/base/src/main/java/org/apache/axis2/transport/base/BaseUtils.java > Mon Oct 20 13:46:11 2008 > > @@ -22,6 +22,7 @@ > > import java.io.IOException; > > import java.io.InputStream; > > import java.util.List; > > +import java.util.Map; > > > > import javax.activation.DataHandler; > > import javax.mail.internet.ContentType; > > @@ -35,7 +36,9 @@ > > import org.apache.axiom.om.OMElement; > > import org.apache.axiom.om.OMNamespace; > > import org.apache.axiom.om.OMOutputFormat; > > +import org.apache.axiom.om.OMSourcedElement; > > import org.apache.axiom.om.OMText; > > +import org.apache.axiom.om.ds.MapDataSource; > > import org.apache.axiom.om.impl.builder.StAXBuilder; > > import org.apache.axiom.om.impl.builder.StAXOMBuilder; > > import org.apache.axiom.om.impl.llom.OMTextImpl; > > @@ -322,8 +325,19 @@ > > msgContext.setDoingMTOM(true); > > > > } else { > > - handleException("Unable to read payload from message of > type : " > > - + message.getClass().getName()); > > + Map msgMap = getMessageMapPayload(message); > > + if (msgMap != null) { > > + if (wrapperQName == null) { > > + wrapperQName = > BaseConstants.DEFAULT_MAP_WRAPPER; > > + } > > + OMSourcedElement omData = > soapFactory.createOMElement(new MapDataSource(msgMap, > > + wrapperQName.getLocalPart(), > soapFactory.createOMNamespace(wrapperQName.getNamespaceURI(), > wrapperQName.getPrefix())), > > + wrapperQName.getLocalPart(), > soapFactory.createOMNamespace(wrapperQName.getNamespaceURI(), > wrapperQName.getPrefix())); > > + wrapper = (OMElement) omData; > > + } else { > > + handleException("Unable to read payload from message > of type : " > > + + message.getClass().getName()); > > + } > > } > > } > > > > @@ -366,6 +380,14 @@ > > */ > > public abstract byte[] getMessageBinaryPayload(Object message); > > > > + /** > > + * Get the message payload as a Map, if the message is a non-SOAP, > non-XML, binary Map-based message > > + * > > + * @param message the message Object > > + * @return the payload of the message as a Map > > + */ > > + public abstract Map getMessageMapPayload(Object message); > > + > > protected static void handleException(String s) { > > log.error(s); > > throw new BaseTransportException(s); > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/pom.xml > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/pom.xml?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/pom.xml > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/pom.xml > Mon Oct 20 13:46:11 2008 > > @@ -71,6 +71,12 @@ > > <artifactId>geronimo-jms_1.1_spec</artifactId> > > <version>${jms-1.1-spec.version}</version> > > </dependency> > > + > > + <dependency> > > + <groupId>org.apache.ws.commons.axiom</groupId> > > + <artifactId>sciflex-axiom-patch</artifactId> > > + <version>${sciflex-axiom-patch.version}</version> > > + </dependency> > > </dependencies> > > > > <properties> > > @@ -78,6 +84,7 @@ > > <commons.logging.version>1.1</commons.logging.version> > > > <axis2-transport-base.version>SNAPSHOT</axis2-transport-base.version> > > <jms-1.1-spec.version>1.1</jms-1.1-spec.version> > > + > <sciflex-axiom-patch.version>0.9-SNAPSHOT</sciflex-axiom-patch.version> > > </properties> > > > > -</project> > > \ No newline at end of file > > +</project> > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSConstants.java > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSConstants.java?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSConstants.java > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSConstants.java > Mon Oct 20 13:46:11 2008 > > @@ -93,6 +93,10 @@ > > */ > > public static final String JMS_BYTE_MESSAGE = "JMS_BYTE_MESSAGE"; > > /** > > + * The message type indicating a MapMessage. See JMS_MESSAGE_TYPE > > + */ > > + public static final String JMS_MAP_MESSAGE = "JMS_MAP_MESSAGE"; > > + /** > > * The message type indicating a TextMessage. See JMS_MESSAGE_TYPE > > */ > > public static final String JMS_TEXT_MESSAGE = "JMS_TEXT_MESSAGE"; > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSMessageReceiver.java > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSMessageReceiver.java?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSMessageReceiver.java > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSMessageReceiver.java > Mon Oct 20 13:46:11 2008 > > @@ -103,6 +103,8 @@ > > try { > > if (message instanceof BytesMessage) { > > > metrics.incrementBytesReceived((JMSUtils.getBodyLength((BytesMessage) > message))); > > + } else if (message instanceof MapMessage) { > > + > metrics.incrementBytesReceived((JMSUtils.getBodyLength((MapMessage) > message))); > > } else if (message instanceof TextMessage) { > > metrics.incrementBytesReceived(((TextMessage) > message).getText().getBytes().length); > > } else { > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSSender.java > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSSender.java?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSSender.java > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSSender.java > Mon Oct 20 13:46:11 2008 > > @@ -16,9 +16,12 @@ > > package org.apache.axis2.transport.jms; > > > > import org.apache.axiom.om.OMOutputFormat; > > +import org.apache.axiom.om.OMDataSourceExt; > > import org.apache.axiom.om.OMElement; > > +import org.apache.axiom.om.OMSourcedElement; > > import org.apache.axiom.om.OMText; > > import org.apache.axiom.om.OMNode; > > +import org.apache.axiom.om.ds.MapDataSource; > > import org.apache.axis2.AxisFault; > > import org.apache.axis2.context.MessageContext; > > import org.apache.axis2.context.ConfigurationContext; > > @@ -243,6 +246,8 @@ > > try { > > if (message instanceof BytesMessage) { > > > metrics.incrementBytesSent(JMSUtils.getBodyLength((BytesMessage) message)); > > + } else if (message instanceof MapMessage) { > > + > metrics.incrementBytesSent((JMSUtils.getBodyLength((MapMessage) message))); > > } else if (message instanceof TextMessage) { > > metrics.incrementBytesSent(( > > (TextMessage) > message).getText().getBytes().length); > > @@ -337,6 +342,8 @@ > > try { > > if (reply instanceof BytesMessage) { > > > metrics.incrementBytesReceived(JMSUtils.getBodyLength((BytesMessage) > reply)); > > + } else if (reply instanceof MapMessage) { > > + > metrics.incrementBytesReceived((JMSUtils.getBodyLength((MapMessage) > reply))); > > } else if (reply instanceof TextMessage) { > > metrics.incrementBytesReceived(( > > (TextMessage) > reply).getText().getBytes().length); > > @@ -418,6 +425,26 @@ > > message = session.createBytesMessage(); > > BytesMessage bytesMsg = (BytesMessage) message; > > bytesMsg.writeBytes(baos.toByteArray()); > > + } else if (msgType != null && > JMSConstants.JMS_MAP_MESSAGE.equals(msgType)) { > > + message = session.createMapMessage(); > > + MapMessage mapMsg = (MapMessage) message; > > + OMElement wrapper = > msgContext.getEnvelope().getBody().getFirstElement(); > > + if (wrapper != null && wrapper instanceof > OMSourcedElement) { > > + OMSourcedElement omNode = (OMSourcedElement) > wrapper; > > + Object ds = omNode.getDataSource(); > > + if (ds != null && ds instanceof MapDataSource) { > > + OMDataSourceExt dse = (OMDataSourceExt) > omNode.getDataSource(); > > + Map map = (Map) dse.getObject(); > > + Iterator it = map.keySet().iterator(); > > + while (it.hasNext()) { > > + Object key = it.next(); > > + Object value = map.get(key); > > + if (key != null && value != null && key > instanceof String) { > > + mapMsg.setObject((String)key, value); > > + } > > + } > > + } > > + } > > } else { > > message = session.createTextMessage(); // default > > TextMessage txtMsg = (TextMessage) message; > > @@ -454,6 +481,27 @@ > > TextMessage txtMsg = (TextMessage) message; > > txtMsg.setText(msgContext.getEnvelope().getBody(). > > > getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText()); > > + } else if (JMSConstants.JMS_MAP_MESSAGE.equals(jmsPayloadType)) > { > > + message = session.createMapMessage(); > > + MapMessage mapMsg = (MapMessage) message; > > + OMElement wrapper = msgContext.getEnvelope().getBody(). > > + > getFirstChildWithName(BaseConstants.DEFAULT_MAP_WRAPPER); > > + if (wrapper != null && wrapper instanceof OMSourcedElement) > { > > + OMSourcedElement omNode = (OMSourcedElement) wrapper; > > + Object ds = omNode.getDataSource(); > > + if (ds != null && ds instanceof MapDataSource) { > > + OMDataSourceExt dse = (OMDataSourceExt) > omNode.getDataSource(); > > + Map map = (Map) dse.getObject(); > > + Iterator it = map.keySet().iterator(); > > + while (it.hasNext()) { > > + Object key = it.next(); > > + Object value = map.get(key); > > + if (key != null && value != null && key > instanceof String) { > > + mapMsg.setObject((String)key, value); > > + } > > + } > > + } > > + } > > } > > > > // set the JMS correlation ID if specified > > @@ -483,7 +531,8 @@ > > /** > > * Guess the message type to use for JMS looking at the message > contexts' envelope > > * @param msgContext the message context > > - * @return JMSConstants.JMS_BYTE_MESSAGE or > JMSConstants.JMS_TEXT_MESSAGE or null > > + * @return JMSConstants.JMS_BYTE_MESSAGE, > JMSConstants.JMS_TEXT_MESSAGE, > > + * JMSConstants.JMS_MAP_MESSAGE or null > > */ > > private String guessMessageType(MessageContext msgContext) { > > OMElement firstChild = > msgContext.getEnvelope().getBody().getFirstElement(); > > @@ -492,6 +541,8 @@ > > return JMSConstants.JMS_BYTE_MESSAGE; > > } else if > (BaseConstants.DEFAULT_TEXT_WRAPPER.equals(firstChild.getQName())) { > > return JMSConstants.JMS_TEXT_MESSAGE; > > + } else if > (BaseConstants.DEFAULT_MAP_WRAPPER.equals(firstChild.getQName())) { > > + return JMSConstants.JMS_MAP_MESSAGE; > > } > > } > > return null; > > > > Modified: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java?rev=706401&r1=706400&r2=706401&view=diff > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java > (original) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java > Mon Oct 20 13:46:11 2008 > > @@ -330,6 +330,16 @@ > > > txtMsg.getText().getBytes(MessageContext.DEFAULT_CHAR_SET_ENCODING)); > > } > > > > + } else if (message instanceof MapMessage) { > > + MapMessage mapMsg = (MapMessage) message; > > + String contentType = getProperty(mapMsg, > BaseConstants.CONTENT_TYPE); > > + > > + if (contentType != null) { > > + return new MapMessageInputStream(mapMsg, > BuilderUtil.getCharSetEncoding(contentType)); > > + } else { > > + return new MapMessageInputStream(mapMsg, > MessageContext.DEFAULT_CHAR_SET_ENCODING); > > + } > > + > > } else { > > handleException("Unsupported JMS message type : " + > message.getClass().getName()); > > } > > @@ -722,6 +732,31 @@ > > return null; > > } > > > > + public Map getMessageMapPayload(Object message) { > > + > > + if (message instanceof MapMessage) { > > + MapMessage mapMessage = (MapMessage) message; > > + > > + try { > > + Map outMap = new TreeMap(); > > + for (Enumeration e = mapMessage.getMapNames() ; > e.hasMoreElements() ;) { > > + String key = (String) e.nextElement(); > > + Object value = mapMessage.getObject(key); > > + if (value != null) { > > + outMap.put(key, value); > > + } else { > > + log.warn("Ignoring key " + key + " that did not > return any value"); > > + } > > + } > > + return outMap; > > + > > + } catch (JMSException e) { > > + handleException("Error reading JMS map message payload", > e); > > + } > > + } > > + return null; > > + } > > + > > // ----------- JMS 1.0.2b compatibility methods ------------- > > public static Connection createConnection(ConnectionFactory > conFactory, String user, > > String pass, String destinationType) throws JMSException { > > @@ -830,6 +865,42 @@ > > } catch (JMSException ignore) {} > > return length; > > } > > + > > + public static long getBodyLength(MapMessage mMsg) { > > + long length = 0; > > + MapMessage mapMessage = (MapMessage) mMsg; > > + try { > > + for (Enumeration e = mMsg.getMapNames() ; > e.hasMoreElements() ;) { > > + String key = (String) e.nextElement(); > > + Object value = mMsg.getObject(key); > > + if (value != null) { > > + if (value instanceof Boolean || value instanceof > Byte) { > > + length += 1; > > + } else if (value instanceof Character || value > instanceof Short) { > > + length += 2; > > + } else if (value instanceof Integer || value > instanceof Float) { > > + length += 4; > > + } else if (value instanceof Long || value > instanceof Double) { > > + length += 8; > > + } else if (value instanceof byte[]) { > > + length += ((byte[])value).length; > > + } else if (value instanceof String) { > > + length += ((String) > value).getBytes().length; > > + } else { > > + log.error("Unable to determine message size. > Invalid Object Type : " + value.getClass().getName()); > > + return 0; > > + } > > + } else { > > + log.warn("Ignoring key " + key + " that did not > return any value"); > > + } > > + length += key.getBytes().length; > > + } > > + > > + } catch (JMSException e) { > > + handleException("Error reading JMS map message payload", > e); > > + } > > + return length; > > + } > > > > public static <T> T lookup(Context context, Class<T> clazz, String > name) > > throws NamingException { > > > > Added: > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/MapMessageInputStream.java > > URL: > http://svn.apache.org/viewvc/webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/MapMessageInputStream.java?rev=706401&view=auto > > > ============================================================================== > > --- > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/MapMessageInputStream.java > (added) > > +++ > webservices/commons/trunk/scratch/senaka/sci-flex/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/MapMessageInputStream.java > Mon Oct 20 13:46:11 2008 > > @@ -0,0 +1,101 @@ > > +/* > > +* Copyright 2004,2005 The Apache Software Foundation. > > +* > > +* Licensed 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.axis2.transport.jms; > > + > > +import org.apache.axiom.om.OMAbstractFactory; > > +import org.apache.axiom.om.OMFactory; > > +import org.apache.axiom.om.ds.MapDataSource; > > +import org.apache.axis2.transport.base.BaseConstants; > > +import org.apache.commons.logging.Log; > > +import org.apache.commons.logging.LogFactory; > > + > > +import java.io.ByteArrayInputStream; > > +import java.io.InputStream; > > +import java.io.IOException; > > +import java.io.UnsupportedEncodingException; > > +import java.util.Map; > > + > > +import javax.jms.MapMessage; > > +import javax.jms.JMSException; > > +import javax.jms.MessageEOFException; > > +import javax.xml.namespace.QName; > > + > > +/** > > + * Input stream that reads data from a JMS [EMAIL PROTECTED] MapMessage}. > > + */ > > +public class MapMessageInputStream extends InputStream { > > + > > + private static final Log log = > LogFactory.getLog(MapMessageInputStream.class); > > + private MapMessage message; > > + private String encoding; > > + private ByteArrayInputStream byteStream = null; > > + > > + public MapMessageInputStream(MapMessage message, String encoding) { > > + this.message = message; > > + this.encoding = encoding; > > + } > > + > > + private ByteArrayInputStream getByteStream() { > > + if (byteStream != null) { > > + return byteStream; > > + } else { > > + JMSUtils utils = new JMSUtils(); > > + Map payloadMap = utils.getMessageMapPayload(message); > > + if (payloadMap != null) { > > + QName wrapperQName = BaseConstants.DEFAULT_MAP_WRAPPER; > > + OMFactory ombuilderFactory = > OMAbstractFactory.getOMFactory(); > > + // It was assumed that the creation of a MapDataSource > in here will > > + // always be based on the default wrapper QName. > > + MapDataSource mds = new MapDataSource(payloadMap, > wrapperQName.getLocalPart(), > > + > ombuilderFactory.createOMNamespace(wrapperQName.getNamespaceURI(), > wrapperQName.getPrefix())); > > + try { > > + byteStream = new > ByteArrayInputStream(mds.getXMLBytes(encoding)); > > + } catch (UnsupportedEncodingException e) { > > + log.error("Unsupported Encoding"); > > + byteStream = null; > > + } > > + } > > + return byteStream; > > + } > > + } > > + > > + @Override > > + public int read() { > > + ByteArrayInputStream readStream = getByteStream(); > > + if (readStream == null) { > > + return -1; > > + } > > + return readStream.read(); > > + } > > + > > + @Override > > + public int read(byte[] b, int off, int len) { > > + ByteArrayInputStream readStream = getByteStream(); > > + if (readStream == null) { > > + return -1; > > + } > > + return readStream.read(b, off, len); > > + } > > + > > + @Override > > + public int read(byte[] b) throws IOException { > > + ByteArrayInputStream readStream = getByteStream(); > > + if (readStream == null) { > > + return -1; > > + } > > + return readStream.read(b); > > + } > > +} > > > > > > >
