001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jms;
019    
020    import java.io.ByteArrayOutputStream;
021    import java.io.DataOutputStream;
022    import java.io.ObjectOutputStream;
023    import java.nio.ByteBuffer;
024    import java.util.Enumeration;
025    import javax.jms.BytesMessage;
026    import javax.jms.MapMessage;
027    import javax.jms.Message;
028    import javax.jms.MessageEOFException;
029    import javax.jms.ObjectMessage;
030    import javax.jms.StreamMessage;
031    import javax.jms.TextMessage;
032    import org.apache.camel.Converter;
033    import org.apache.camel.converter.NIOConverter;
034    
035    
036    /**
037     * Some simple payload conversions to I/O 
038     * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
039     *
040     * @version $Revision: 533630 $
041     */
042    
043    @Converter
044    public class JmsIOConverter{
045        /**
046         * @param message
047         * @return a ByteBuffer
048         * @throws Exception
049         */
050        @Converter
051        public static ByteBuffer toByteBuffer(final Message message) throws Exception {
052       
053            if (message instanceof TextMessage) {
054                final String text = ((TextMessage)message).getText();
055                return NIOConverter.toByteBuffer(text);
056            }
057            if (message instanceof BytesMessage) {
058                final BytesMessage bmsg = (BytesMessage)message;
059                final int len = (int)bmsg.getBodyLength();
060                final byte[] data = new byte[len];
061                bmsg.readBytes(data,len);
062                return NIOConverter.toByteBuffer(data);
063                
064            }
065            if (message instanceof StreamMessage) {
066                final StreamMessage msg = (StreamMessage)message;
067                final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
068                final DataOutputStream dataOut = new DataOutputStream(bytesOut);
069                try {
070                    while (true) {
071                        final Object obj = msg.readObject();
072                        writeData(dataOut,obj);
073                    }
074                }catch(MessageEOFException e) {
075                    //we have no other way of knowing the end of the message
076                }
077                dataOut.close();
078                return NIOConverter.toByteBuffer(bytesOut.toByteArray());
079            }
080            if (message instanceof MapMessage) {
081                final MapMessage msg = (MapMessage)message;
082                final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
083                final DataOutputStream dataOut = new DataOutputStream(bytesOut);
084                for (final Enumeration en = msg.getMapNames(); en.hasMoreElements();) {
085                    final Object obj = msg.getObject(en.nextElement().toString());
086                    writeData(dataOut,obj);
087                }
088                dataOut.close();
089                return NIOConverter.toByteBuffer(bytesOut.toByteArray());
090            }
091            if (message instanceof ObjectMessage) {
092                ObjectMessage objMessage = (ObjectMessage)message;
093                Object object = objMessage.getObject();
094                ByteArrayOutputStream bytesOut=new ByteArrayOutputStream();
095                ObjectOutputStream objectOut=new ObjectOutputStream(bytesOut);
096                objectOut.writeObject(object);
097                objectOut.close();
098                return NIOConverter.toByteBuffer(bytesOut.toByteArray());
099            }
100            return null;
101           
102        }
103        private static void writeData(DataOutputStream dataOut, Object data) throws Exception {
104            
105            
106            if (data instanceof byte[]) {
107                dataOut.write((byte[])data);
108            }else if (data instanceof String) {
109                dataOut.writeUTF(data.toString());
110            }
111            else if (data instanceof Double) {
112                dataOut.writeDouble(((Double)data).doubleValue());
113            }else if (data instanceof Float) {
114                dataOut.writeFloat(((Float)data).floatValue());
115            }else if (data instanceof Long) {
116                dataOut.writeLong(((Long)data).longValue());
117            }else if (data instanceof Integer) {
118                dataOut.writeInt(((Integer)data).intValue());
119            }else if (data instanceof Short) {
120                dataOut.writeShort(((Short)data).shortValue());
121            }else if (data instanceof Character) {
122                dataOut.writeChar(((Character)data).charValue());
123            }else if (data instanceof Byte) {
124                dataOut.writeByte(((Byte)data).byteValue());
125            }else if (data instanceof Boolean) {
126                dataOut.writeBoolean(((Boolean)data).booleanValue());
127            }
128                  
129        }
130    }