KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > multicast > MulticastMessageReceiver


1 /*
2  * $Id: MulticastMessageReceiver.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.multicast;
12
13 import org.mule.providers.AbstractConnector;
14 import org.mule.providers.udp.UdpMessageReceiver;
15 import org.mule.umo.UMOComponent;
16 import org.mule.umo.endpoint.UMOEndpoint;
17 import org.mule.umo.lifecycle.InitialisationException;
18
19 import javax.resource.spi.work.Work JavaDoc;
20
21 import java.io.IOException JavaDoc;
22 import java.net.DatagramPacket JavaDoc;
23 import java.net.DatagramSocket JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.MulticastSocket JavaDoc;
26 import java.net.URI JavaDoc;
27
28 /**
29  * <code>MulticastMessageReceiver</code> TODO (document class)
30  *
31  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
32  * @version $Revision: 3798 $
33  */

34 public class MulticastMessageReceiver extends UdpMessageReceiver
35 {
36     public MulticastMessageReceiver(AbstractConnector connector, UMOComponent component, UMOEndpoint endpoint)
37         throws InitialisationException
38     {
39         super(connector, component, endpoint);
40     }
41
42     protected DatagramSocket JavaDoc createSocket(URI JavaDoc uri, InetAddress JavaDoc inetAddress) throws IOException JavaDoc
43     {
44         // SocketAddress sa = new InetSocketAddress(uri.getHost(),
45
// uri.getPort());
46
MulticastSocket JavaDoc socket = new MulticastSocket JavaDoc(uri.getPort());
47         socket.joinGroup(inetAddress);
48         return socket;
49     }
50
51     protected Work JavaDoc createWork(DatagramPacket JavaDoc packet) throws IOException JavaDoc
52     {
53         return new MulticastWorker(packet);
54     }
55
56     public class MulticastWorker extends UdpWorker
57     {
58         public MulticastWorker(DatagramPacket JavaDoc packet)
59         {
60             super(socket, packet);
61         }
62
63         public void dispose()
64         {
65             // Do not close socket as we reuse it
66
// So do not call super.doDispose();
67
}
68     }
69
70     protected void doDispose()
71     {
72         if (socket != null && !socket.isClosed())
73         {
74             try
75             {
76                 ((MulticastSocket JavaDoc)socket).leaveGroup(inetAddress);
77             }
78             catch (IOException JavaDoc e)
79             {
80                 logger.error("failed to leave group: " + e.getMessage(), e);
81             }
82         }
83         super.doDispose();
84     }
85 }
86
Popular Tags