1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util; |
8 | |
|
9 | |
import java.net.Socket; |
10 | |
import java.net.URL; |
11 | |
|
12 | |
import org.apache.commons.logging.Log; |
13 | |
import org.apache.commons.logging.LogFactory; |
14 | |
|
15 | |
public final class NetworkUtils |
16 | |
{ |
17 | 0 | private static final Log logger = LogFactory.getLog(NetworkUtils.class); |
18 | |
|
19 | |
private NetworkUtils() |
20 | 0 | { |
21 | |
|
22 | 0 | } |
23 | |
|
24 | |
public static boolean isServerReachable(URL url, int timeout) |
25 | |
{ |
26 | 0 | int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort(); |
27 | 0 | return isServerReachable(url.getHost(), port, timeout); |
28 | |
} |
29 | |
|
30 | |
public static boolean isServerReachable(String host, int port, int timeout) |
31 | |
{ |
32 | 0 | boolean isServerReachable = false; |
33 | 0 | Socket socket = null; |
34 | |
|
35 | |
try |
36 | |
{ |
37 | 0 | socket = TimedSocket.createSocket(host, port, timeout); |
38 | 0 | isServerReachable = true; |
39 | |
} |
40 | 0 | catch (Exception e) |
41 | |
{ |
42 | 0 | logger.debug("Server at " + host + ":" + port + " not reachable. " + e.getMessage()); |
43 | |
try |
44 | |
{ |
45 | 0 | if (socket != null) |
46 | |
{ |
47 | 0 | socket.close(); |
48 | |
} |
49 | |
} |
50 | 0 | catch (Exception socketNotClosed) |
51 | |
{ |
52 | 0 | logger.debug(socketNotClosed); |
53 | 0 | } |
54 | 0 | } |
55 | |
|
56 | 0 | return isServerReachable; |
57 | |
} |
58 | |
} |