Coverage Report - org.mule.providers.tcp.TcpMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
TcpMessageAdapter
96%
22/23
88%
7/8
2
 
 1  
 /*
 2  
  * $Id: TcpMessageAdapter.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.providers.tcp;
 12  
 
 13  
 import org.mule.impl.MuleMessage;
 14  
 import org.mule.impl.ThreadSafeAccess;
 15  
 import org.mule.providers.AbstractMessageAdapter;
 16  
 
 17  
 import java.io.Serializable;
 18  
 import java.util.Iterator;
 19  
 
 20  
 import org.apache.commons.lang.SerializationUtils;
 21  
 
 22  
 /**
 23  
  * <code>TcpMessageAdapter</code> TODO
 24  
  */
 25  
 
 26  
 public class TcpMessageAdapter extends AbstractMessageAdapter
 27  
 {
 28  
     /**
 29  
      * Serial version
 30  
      */
 31  
     private static final long serialVersionUID = 7229837140160407794L;
 32  
 
 33  
     private Object message;
 34  
 
 35  
     public TcpMessageAdapter(Object message)
 36  258
     {
 37  258
         if (message instanceof MuleMessage)
 38  
         {
 39  2
             MuleMessage muleMessage = (MuleMessage)message;
 40  2
             Iterator names = muleMessage.getPropertyNames().iterator();
 41  10
             while (names.hasNext())
 42  
             {
 43  8
                 Object name = names.next();
 44  8
                 this.properties.put(name, muleMessage.getProperty(name.toString()));
 45  8
             }
 46  2
             this.message = muleMessage.getPayload();
 47  2
         }
 48  
         else
 49  
         {
 50  256
             this.message = message;
 51  
         }
 52  258
     }
 53  
 
 54  
     protected TcpMessageAdapter(TcpMessageAdapter template)
 55  
     {
 56  4
         super(template);
 57  4
         message = template.message;
 58  4
     }
 59  
 
 60  
     /**
 61  
      * Converts the message implementation into a String representation
 62  
      * 
 63  
      * @param encoding The encoding to use when transforming the message (if
 64  
      *         necessary). The parameter is used when converting from a byte array
 65  
      * @return String representation of the message payload
 66  
      * @throws Exception Implementation may throw an endpoint specific exception
 67  
      */
 68  
     public String getPayloadAsString(String encoding) throws Exception
 69  
     {
 70  26
         if (message instanceof byte[])
 71  
         {
 72  24
             return new String((byte[])message, encoding);
 73  
         }
 74  
         else
 75  
         {
 76  2
             return message.toString();
 77  
         }
 78  
     }
 79  
 
 80  
     public byte[] getPayloadAsBytes() throws Exception
 81  
     {
 82  4
         if (message instanceof byte[])
 83  
         {
 84  4
             return (byte[])message;
 85  
         }
 86  
         else
 87  
         {
 88  0
             return SerializationUtils.serialize((Serializable)message);
 89  
         }
 90  
     }
 91  
 
 92  
     public Object getPayload()
 93  
     {
 94  494
         return message;
 95  
     }
 96  
 
 97  
     public ThreadSafeAccess newThreadCopy()
 98  
     {
 99  4
         return new TcpMessageAdapter(this);
 100  
     }
 101  
 
 102  
 }