1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.transport.udp; |
11 | |
|
12 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
13 | |
import org.mule.api.endpoint.InboundEndpoint; |
14 | |
import org.mule.api.transport.Connector; |
15 | |
import org.mule.util.MapUtils; |
16 | |
|
17 | |
import java.io.IOException; |
18 | |
import java.net.DatagramSocket; |
19 | |
import java.net.InetAddress; |
20 | |
import java.net.Socket; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
import org.apache.commons.pool.KeyedPoolableObjectFactory; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 28 | public class UdpSocketFactory implements KeyedPoolableObjectFactory |
31 | |
{ |
32 | |
|
33 | |
|
34 | |
|
35 | 28 | protected transient final Log logger = LogFactory.getLog(UdpSocketFactory.class); |
36 | |
|
37 | |
public Object makeObject(Object key) throws Exception |
38 | |
{ |
39 | 4 | ImmutableEndpoint ep = (ImmutableEndpoint)key; |
40 | |
DatagramSocket socket; |
41 | |
|
42 | 4 | if(ep instanceof InboundEndpoint) |
43 | |
{ |
44 | 2 | int port = ep.getEndpointURI().getPort(); |
45 | 2 | String host = ep.getEndpointURI().getHost(); |
46 | 2 | if(port > 0) |
47 | |
{ |
48 | 2 | if("null".equalsIgnoreCase(host)) |
49 | |
{ |
50 | 0 | socket = createSocket(port); |
51 | |
} |
52 | |
else |
53 | |
{ |
54 | 2 | socket = createSocket(port, InetAddress.getByName(host)); |
55 | |
} |
56 | |
} |
57 | |
else |
58 | |
{ |
59 | 0 | socket = createSocket(); |
60 | |
} |
61 | 2 | } |
62 | |
else |
63 | |
{ |
64 | |
|
65 | 2 | socket = createSocket(); |
66 | |
} |
67 | |
|
68 | 4 | UdpConnector connector = (UdpConnector)ep.getConnector(); |
69 | |
|
70 | |
|
71 | 4 | if (connector.getReceiveBufferSize() != Connector.INT_VALUE_NOT_SET |
72 | |
&& socket.getReceiveBufferSize() != connector.getReceiveBufferSize()) |
73 | |
{ |
74 | 4 | socket.setReceiveBufferSize(connector.getReceiveBufferSize()); |
75 | |
} |
76 | 4 | if (connector.getSendBufferSize() != Connector.INT_VALUE_NOT_SET |
77 | |
&& socket.getSendBufferSize() != connector.getSendBufferSize()) |
78 | |
{ |
79 | 4 | socket.setSendBufferSize(connector.getSendBufferSize()); |
80 | |
} |
81 | 4 | if (connector.getReceiveTimeout() != Connector.INT_VALUE_NOT_SET |
82 | |
&& socket.getSoTimeout() != connector.getReceiveTimeout()) |
83 | |
{ |
84 | 0 | socket.setSoTimeout(connector.getSendTimeout()); |
85 | |
} |
86 | 4 | socket.setBroadcast(connector.isBroadcast()); |
87 | 4 | return socket; |
88 | |
} |
89 | |
|
90 | |
public void destroyObject(Object key, Object object) throws Exception |
91 | |
{ |
92 | 2 | Socket socket = (Socket)object; |
93 | 0 | if(!socket.isClosed()) |
94 | |
{ |
95 | 0 | socket.close(); |
96 | |
} |
97 | 0 | } |
98 | |
|
99 | |
public boolean validateObject(Object key, Object object) |
100 | |
{ |
101 | 6006 | DatagramSocket socket = (DatagramSocket)object; |
102 | 6006 | return !socket.isClosed(); |
103 | |
} |
104 | |
|
105 | |
public void activateObject(Object key, Object object) throws Exception |
106 | |
{ |
107 | |
|
108 | 3004 | } |
109 | |
|
110 | |
public void passivateObject(Object key, Object object) throws Exception |
111 | |
{ |
112 | 3002 | ImmutableEndpoint ep = (ImmutableEndpoint)key; |
113 | |
|
114 | 3002 | boolean keepSocketOpen = MapUtils.getBooleanValue(ep.getProperties(), |
115 | |
UdpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, ((UdpConnector)ep.getConnector()).isKeepSendSocketOpen()); |
116 | 3002 | DatagramSocket socket = (DatagramSocket)object; |
117 | |
|
118 | 3002 | if (!keepSocketOpen) |
119 | |
{ |
120 | 0 | if (socket != null) |
121 | |
{ |
122 | 0 | socket.close(); |
123 | |
} |
124 | |
} |
125 | 3002 | } |
126 | |
|
127 | |
protected DatagramSocket createSocket() throws IOException |
128 | |
{ |
129 | 2 | return new DatagramSocket(); |
130 | |
} |
131 | |
|
132 | |
protected DatagramSocket createSocket(int port) throws IOException |
133 | |
{ |
134 | 0 | return new DatagramSocket(port); |
135 | |
} |
136 | |
|
137 | |
protected DatagramSocket createSocket(int port, InetAddress inetAddress) throws IOException |
138 | |
{ |
139 | 2 | return new DatagramSocket(port, inetAddress); |
140 | |
} |
141 | |
} |