KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > udp > UdpMessageAdapter


1 /*
2  * $Id: UdpMessageAdapter.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 java.net.DatagramPacket JavaDoc;
14 import java.net.InetAddress JavaDoc;
15
16 import org.mule.providers.AbstractMessageAdapter;
17 import org.mule.umo.MessagingException;
18 import org.mule.umo.provider.MessageTypeNotSupportedException;
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 JavaDoc ADDRESS_PROPERTY = "packet.address";
32     public static final String JavaDoc PORT_PROPERTY = "packet.port";
33
34     private byte[] message;
35
36     public UdpMessageAdapter(Object JavaDoc message) throws MessagingException
37     {
38         if (message instanceof DatagramPacket JavaDoc)
39         {
40             DatagramPacket JavaDoc dp = (DatagramPacket JavaDoc)message;
41             this.message = new byte[dp.getLength()];
42             System.arraycopy(dp.getData(), 0, this.message, 0, dp.getLength());
43
44             InetAddress JavaDoc address = dp.getAddress();
45             if (address != null)
46             {
47                 setProperty(ADDRESS_PROPERTY, address);
48             }
49
50             setProperty(PORT_PROPERTY, new Integer JavaDoc(dp.getPort()));
51         }
52         else
53         {
54             throw new MessageTypeNotSupportedException(message, getClass());
55         }
56     }
57
58     /**
59      * Converts the message implementation into a String representation
60      *
61      * @param encoding The encoding to use when transforming the message (if
62      * necessary). The parameter is used when converting from a byte array
63      * @return String representation of the message payload
64      * @throws Exception Implementation may throw an endpoint specific exception
65      */

66     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
67     {
68         return new String JavaDoc(message, encoding);
69
70     }
71
72     public byte[] getPayloadAsBytes() throws Exception JavaDoc
73     {
74         return message;
75     }
76
77     public Object JavaDoc getPayload()
78     {
79         return message;
80     }
81 }
82
Popular Tags