1
2
3
4
5
6
7
8
9
10 package org.mule.transport.multicast;
11
12 import org.mule.api.endpoint.ImmutableEndpoint;
13 import org.mule.api.transport.Connector;
14 import org.mule.transport.udp.UdpSocketFactory;
15
16 import java.io.IOException;
17 import java.net.DatagramSocket;
18 import java.net.InetAddress;
19 import java.net.MulticastSocket;
20
21
22
23
24 public class MulticastSocketFactory extends UdpSocketFactory
25 {
26
27 public Object makeObject(Object key) throws Exception
28 {
29 ImmutableEndpoint ep = (ImmutableEndpoint)key;
30 MulticastSocket socket = (MulticastSocket)super.makeObject(key);
31 socket.setLoopbackMode(((MulticastConnector)ep.getConnector()).isLoopback());
32 int ttl = ((MulticastConnector)ep.getConnector()).getTimeToLive();
33 if(ttl!= Connector.INT_VALUE_NOT_SET)
34 {
35 socket.setTimeToLive(ttl);
36 }
37 return socket;
38 }
39
40
41
42 public void destroyObject(Object key, Object object) throws Exception
43 {
44 ImmutableEndpoint ep = (ImmutableEndpoint)key;
45 InetAddress inetAddress;
46 String host = ep.getEndpointURI().getHost();
47 if("null".equalsIgnoreCase(host))
48 {
49 inetAddress = InetAddress.getLocalHost();
50 }
51 else
52 {
53 inetAddress = InetAddress.getByName(host);
54 }
55 MulticastSocket socket = (MulticastSocket)object;
56 socket.leaveGroup(inetAddress);
57 super.destroyObject(key, object);
58 }
59
60 protected DatagramSocket createSocket() throws IOException
61 {
62 return new MulticastSocket();
63 }
64
65 protected DatagramSocket createSocket(int port) throws IOException
66 {
67 throw new IllegalArgumentException("A group host or IP address is required");
68 }
69
70 protected DatagramSocket createSocket(int port, InetAddress inetAddress) throws IOException
71 {
72 MulticastSocket socket = new MulticastSocket(port);
73 socket.joinGroup(inetAddress);
74 return socket;
75 }
76 }