01: /*
02: * $Id: MulticastSocketFactory.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: * TODO
23: */
24: public class MulticastSocketFactory extends UdpSocketFactory {
25:
26: public Object makeObject(Object key) throws Exception {
27: ImmutableEndpoint ep = (ImmutableEndpoint) key;
28: MulticastSocket socket = (MulticastSocket) super
29: .makeObject(key);
30: socket.setLoopbackMode(((MulticastConnector) ep.getConnector())
31: .isLoopback());
32: int ttl = ((MulticastConnector) ep.getConnector())
33: .getTimeToLive();
34: if (ttl != Connector.INT_VALUE_NOT_SET) {
35: socket.setTimeToLive(ttl);
36: }
37: return socket;
38: }
39:
40: //@java.lang.Override
41: public void destroyObject(Object key, Object object)
42: throws Exception {
43: ImmutableEndpoint ep = (ImmutableEndpoint) key;
44: InetAddress inetAddress;
45: String host = ep.getEndpointURI().getHost();
46: if ("null".equalsIgnoreCase(host)) {
47: inetAddress = InetAddress.getLocalHost();
48: } else {
49: inetAddress = InetAddress.getByName(host);
50: }
51: MulticastSocket socket = (MulticastSocket) object;
52: socket.leaveGroup(inetAddress);
53: super .destroyObject(key, object);
54: }
55:
56: protected DatagramSocket createSocket() throws IOException {
57: return new MulticastSocket();
58: }
59:
60: protected DatagramSocket createSocket(int port) throws IOException {
61: throw new IllegalArgumentException(
62: "A group host or IP address is required");
63: }
64:
65: protected DatagramSocket createSocket(int port,
66: InetAddress inetAddress) throws IOException {
67: MulticastSocket socket = new MulticastSocket(port);
68: socket.joinGroup(inetAddress);
69: return socket;
70: }
71: }
|