IpConvertManager.java
959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.dianping.cat.consumer.cross;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import com.dianping.cat.Cat;
public class IpConvertManager {
private Map<String, String> m_hosts = new HashMap<String, String>();
public String convertHostNameToIP(String hostName) {
String result = m_hosts.get(hostName);
if (result == null) {
if (isIPAddress(hostName)) {
result = hostName;
} else {
try {
InetAddress address = InetAddress.getByName(hostName);
result = address.getHostAddress();
} catch (Exception e) {
Cat.logError(e);
result = "";
}
}
m_hosts.put(hostName, result);
}
return result;
}
public boolean isIPAddress(String str) {
Pattern pattern = Pattern
.compile("^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$");
return pattern.matcher(str).matches();
}
}