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