Coverage Report - org.apache.camel.component.jms.JmsIOConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsIOConverter
0% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.jms;
 19  
 
 20  
 import java.io.ByteArrayOutputStream;
 21  
 import java.io.DataOutputStream;
 22  
 import java.io.ObjectOutputStream;
 23  
 import java.nio.ByteBuffer;
 24  
 import java.util.Enumeration;
 25  
 import javax.jms.BytesMessage;
 26  
 import javax.jms.MapMessage;
 27  
 import javax.jms.Message;
 28  
 import javax.jms.MessageEOFException;
 29  
 import javax.jms.ObjectMessage;
 30  
 import javax.jms.StreamMessage;
 31  
 import javax.jms.TextMessage;
 32  
 import org.apache.camel.Converter;
 33  
 import org.apache.camel.converter.NIOConverter;
 34  
 
 35  
 
 36  
 /**
 37  
  * Some simple payload conversions to I/O 
 38  
  * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
 39  
  *
 40  
  * @version $Revision: 533630 $
 41  
  */
 42  
 
 43  
 @Converter
 44  0
 public class JmsIOConverter{
 45  
     /**
 46  
      * @param message
 47  
      * @return a ByteBuffer
 48  
      * @throws Exception
 49  
      */
 50  
     @Converter
 51  
     public static ByteBuffer toByteBuffer(final Message message) throws Exception {
 52  
    
 53  0
         if (message instanceof TextMessage) {
 54  0
             final String text = ((TextMessage)message).getText();
 55  0
             return NIOConverter.toByteBuffer(text);
 56  
         }
 57  0
         if (message instanceof BytesMessage) {
 58  0
             final BytesMessage bmsg = (BytesMessage)message;
 59  0
             final int len = (int)bmsg.getBodyLength();
 60  0
             final byte[] data = new byte[len];
 61  0
             bmsg.readBytes(data,len);
 62  0
             return NIOConverter.toByteBuffer(data);
 63  
             
 64  
         }
 65  0
         if (message instanceof StreamMessage) {
 66  0
             final StreamMessage msg = (StreamMessage)message;
 67  0
             final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
 68  0
             final DataOutputStream dataOut = new DataOutputStream(bytesOut);
 69  
             try {
 70  
                 while (true) {
 71  0
                     final Object obj = msg.readObject();
 72  0
                     writeData(dataOut,obj);
 73  0
                 }
 74  0
             }catch(MessageEOFException e) {
 75  
                 //we have no other way of knowing the end of the message
 76  
             }
 77  0
             dataOut.close();
 78  0
             return NIOConverter.toByteBuffer(bytesOut.toByteArray());
 79  
         }
 80  0
         if (message instanceof MapMessage) {
 81  0
             final MapMessage msg = (MapMessage)message;
 82  0
             final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
 83  0
             final DataOutputStream dataOut = new DataOutputStream(bytesOut);
 84  0
             for (final Enumeration en = msg.getMapNames(); en.hasMoreElements();) {
 85  0
                 final Object obj = msg.getObject(en.nextElement().toString());
 86  0
                 writeData(dataOut,obj);
 87  0
             }
 88  0
             dataOut.close();
 89  0
             return NIOConverter.toByteBuffer(bytesOut.toByteArray());
 90  
         }
 91  0
         if (message instanceof ObjectMessage) {
 92  0
             ObjectMessage objMessage = (ObjectMessage)message;
 93  0
             Object object = objMessage.getObject();
 94  0
             ByteArrayOutputStream bytesOut=new ByteArrayOutputStream();
 95  0
             ObjectOutputStream objectOut=new ObjectOutputStream(bytesOut);
 96  0
             objectOut.writeObject(object);
 97  0
             objectOut.close();
 98  0
             return NIOConverter.toByteBuffer(bytesOut.toByteArray());
 99  
         }
 100  0
         return null;
 101  
        
 102  
     }
 103  
     private static void writeData(DataOutputStream dataOut, Object data) throws Exception {
 104  
         
 105  
         
 106  0
         if (data instanceof byte[]) {
 107  0
             dataOut.write((byte[])data);
 108  0
         }else if (data instanceof String) {
 109  0
             dataOut.writeUTF(data.toString());
 110  0
         }
 111  0
         else if (data instanceof Double) {
 112  0
             dataOut.writeDouble(((Double)data).doubleValue());
 113  0
         }else if (data instanceof Float) {
 114  0
             dataOut.writeFloat(((Float)data).floatValue());
 115  0
         }else if (data instanceof Long) {
 116  0
             dataOut.writeLong(((Long)data).longValue());
 117  0
         }else if (data instanceof Integer) {
 118  0
             dataOut.writeInt(((Integer)data).intValue());
 119  0
         }else if (data instanceof Short) {
 120  0
             dataOut.writeShort(((Short)data).shortValue());
 121  0
         }else if (data instanceof Character) {
 122  0
             dataOut.writeChar(((Character)data).charValue());
 123  0
         }else if (data instanceof Byte) {
 124  0
             dataOut.writeByte(((Byte)data).byteValue());
 125  0
         }else if (data instanceof Boolean) {
 126  0
             dataOut.writeBoolean(((Boolean)data).booleanValue());
 127  
         }
 128  
               
 129  0
     }
 130  
 }