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