net.sf.hajdbc.util.LocalHost Maven / Gradle / Ivy
package net.sf.hajdbc.util;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class LocalHost {
public static String getFirstRemoteIp(String... ips){
List lips = getRemoteIp(ips);
if(!lips.isEmpty()){
return lips.get(0);
}
return null;
}
public static String getFirstRemoteIp(Collection ips){
return getFirstRemoteIp(new ArrayList(ips));
}
public static String getFirstLocalIp(String... ips){
List lips = getLocalIp(ips);
if(!lips.isEmpty()){
return lips.get(0);
}
return null;
}
public static String getFirstLocalIp(Collection ips){
return getFirstLocalIp(new ArrayList(ips));
}
public static List getLocalIp(String... ips){
List lips = getLocalIp(Arrays.asList(ips));
return lips;
}
public static List getLocalIp(Collection ips){
List lips = new ArrayList();
Set all_lips = getAllIp();
for(String ip :ips){
if(!isNullOrEmpty(ip) && all_lips.contains(ip)){
lips.add(ip);
}
}
return lips;
}
private static boolean isNullOrEmpty(String ip) {
return ip == null || ip.isEmpty();
}
public static List getRemoteIp(String... ips){
List lips = getRemoteIp(Arrays.asList(ips));
return lips;
}
public static List getRemoteIp(Collection ips){
List lips = new ArrayList();
Set all_lips = getAllIp();
for(String ip :ips){
if(!isNullOrEmpty(ip)&&!all_lips.contains(ip)){
lips.add(ip);
}
}
return lips;
}
public static Set getAllIp() {
Set ips = new LinkedHashSet();
Set addrs = getAllInetAddress();
for(InetAddress addr:addrs){
ips.add(addr.getHostAddress());
}
return ips;
}
public static Set getAllIpv4() {
Set ips = new LinkedHashSet();
Set addrs = getAllInet4Address();
for(InetAddress addr:addrs){
ips.add(addr.getHostAddress());
}
return ips;
}
public static Set getAllIpv6() {
Set ips = new LinkedHashSet();
Set addrs = getAllInet6Address();
for(InetAddress addr:addrs){
ips.add(addr.getHostAddress());
}
return ips;
}
public static Set getAllInet4Address() {
Set ipv4s = new LinkedHashSet();
Set addrs = getAllInetAddress();
for(InetAddress addr:addrs){
if(addr instanceof Inet4Address){
ipv4s.add((Inet4Address)addr);
}
}
return ipv4s;
}
public static Set getAllInet6Address() {
Set ipv6s = new LinkedHashSet();
Set addrs = getAllInetAddress();
for(InetAddress addr:addrs){
if(addr instanceof Inet6Address){
ipv6s.add((Inet6Address)addr);
}
}
return ipv6s;
}
public static short getPrefixLength(String ip){
short prefixLen = 0;
try {
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
while (nifs.hasMoreElements()) {
NetworkInterface nif = nifs.nextElement();
if (nif.isUp()) {
List addresses = nif.getInterfaceAddresses();
for (InterfaceAddress address : addresses) {
if(address.getAddress().getHostAddress().equals(ip)){
prefixLen = address.getNetworkPrefixLength();
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return prefixLen;
}
public static Set getAllInetAddress() {
Set addrs = new LinkedHashSet();
try {
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
while (nifs.hasMoreElements()) {
NetworkInterface nif = nifs.nextElement();
if (nif.isUp()) {
Enumeration enumAddr = nif.getInetAddresses();
while (enumAddr.hasMoreElements()) {
InetAddress addr = enumAddr.nextElement();
addrs.add(addr);
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return addrs;
}
}