Coverage Report - org.mule.providers.udp.UdpMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpMessageAdapter
0%
0/18
0%
0/2
1.5
 
 1  
 /*
 2  
  * $Id: UdpMessageAdapter.java 7976 2007-08-21 14:26:13Z 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.udp;
 12  
 
 13  
 import org.mule.impl.ThreadSafeAccess;
 14  
 import org.mule.providers.AbstractMessageAdapter;
 15  
 import org.mule.umo.provider.MessageTypeNotSupportedException;
 16  
 
 17  
 import java.net.DatagramPacket;
 18  
 import java.net.InetAddress;
 19  
 
 20  
 /**
 21  
  * <code>UdpMessageAdapter</code>
 22  
  */
 23  
 
 24  
 public class UdpMessageAdapter extends AbstractMessageAdapter
 25  
 {
 26  
     /**
 27  
      * Serial version
 28  
      */
 29  
     private static final long serialVersionUID = -7767141617682012504L;
 30  
 
 31  
     public static final String ADDRESS_PROPERTY = "packet.address";
 32  
     public static final String PORT_PROPERTY = "packet.port";
 33  
 
 34  
     private byte[] message;
 35  
 
 36  
     public UdpMessageAdapter(Object message) throws MessageTypeNotSupportedException
 37  0
     {
 38  0
         if (message instanceof DatagramPacket)
 39  
         {
 40  0
             DatagramPacket dp = (DatagramPacket)message;
 41  0
             this.message = new byte[dp.getLength()];
 42  0
             System.arraycopy(dp.getData(), 0, this.message, 0, dp.getLength());
 43  
 
 44  0
             InetAddress address = dp.getAddress();
 45  0
             if (address != null)
 46  
             {
 47  0
                 setProperty(ADDRESS_PROPERTY, address);
 48  
             }
 49  
 
 50  0
             setProperty(PORT_PROPERTY, new Integer(dp.getPort()));
 51  
         }
 52  
         else
 53  
         {
 54  0
             throw new MessageTypeNotSupportedException(message, getClass());
 55  
         }
 56  0
     }
 57  
 
 58  
     protected UdpMessageAdapter(UdpMessageAdapter template)
 59  
     {
 60  0
         super(template);
 61  0
         message = template.message;
 62  0
     }
 63  
 
 64  
     /**
 65  
      * Converts the message implementation into a String representation
 66  
      * 
 67  
      * @param encoding The encoding to use when transforming the message (if
 68  
      *            necessary). The parameter is used when converting from a byte array
 69  
      * @return String representation of the message payload
 70  
      * @throws Exception Implementation may throw an endpoint specific exception
 71  
      */
 72  
     public String getPayloadAsString(String encoding) throws Exception
 73  
     {
 74  0
         return new String(message, encoding);
 75  
 
 76  
     }
 77  
 
 78  
     public byte[] getPayloadAsBytes() throws Exception
 79  
     {
 80  0
         return message;
 81  
     }
 82  
 
 83  
     public Object getPayload()
 84  
     {
 85  0
         return message;
 86  
     }
 87  
 
 88  
     public ThreadSafeAccess newThreadCopy()
 89  
     {
 90  0
         return new UdpMessageAdapter(this);
 91  
     }
 92  
 
 93  
 }