1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.providers.multicast; |
11 | |
|
12 | |
import org.mule.providers.udp.UdpSocketFactory; |
13 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
14 | |
import org.mule.umo.provider.UMOConnector; |
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 | 22 | public class MulticastSocketFactory extends UdpSocketFactory |
25 | |
{ |
26 | |
|
27 | |
public Object makeObject(Object key) throws Exception |
28 | |
{ |
29 | 0 | UMOImmutableEndpoint ep = (UMOImmutableEndpoint)key; |
30 | 0 | MulticastSocket socket = (MulticastSocket)super.makeObject(key); |
31 | 0 | socket.setLoopbackMode(((MulticastConnector)ep.getConnector()).isLoopback()); |
32 | 0 | int ttl = ((MulticastConnector)ep.getConnector()).getTimeToLive(); |
33 | 0 | if(ttl!= UMOConnector.INT_VALUE_NOT_SET) |
34 | |
{ |
35 | 0 | socket.setTimeToLive(ttl); |
36 | |
} |
37 | 0 | return socket; |
38 | |
} |
39 | |
|
40 | |
|
41 | |
|
42 | |
public void destroyObject(Object key, Object object) throws Exception |
43 | |
{ |
44 | 0 | UMOImmutableEndpoint ep = (UMOImmutableEndpoint)key; |
45 | |
InetAddress inetAddress; |
46 | 0 | String host = ep.getEndpointURI().getHost(); |
47 | 0 | if("null".equalsIgnoreCase(host)) |
48 | |
{ |
49 | 0 | inetAddress = InetAddress.getLocalHost(); |
50 | |
} |
51 | |
else |
52 | |
{ |
53 | 0 | inetAddress = InetAddress.getByName(host); |
54 | |
} |
55 | 0 | MulticastSocket socket = (MulticastSocket)object; |
56 | 0 | socket.leaveGroup(inetAddress); |
57 | 0 | super.destroyObject(key, object); |
58 | 0 | } |
59 | |
|
60 | |
protected DatagramSocket createSocket() throws IOException |
61 | |
{ |
62 | 0 | return new MulticastSocket(); |
63 | |
} |
64 | |
|
65 | |
protected DatagramSocket createSocket(int port) throws IOException |
66 | |
{ |
67 | 0 | 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 | 0 | MulticastSocket socket = new MulticastSocket(port); |
73 | 0 | socket.joinGroup(inetAddress); |
74 | 0 | return socket; |
75 | |
} |
76 | |
} |