
com.gw.common.utils.HostTools Maven / Gradle / Ivy
/**
* HostTools.java
*
* Copyright (c) 2009 MDT.
* All rights reserved.
*
* This Demo.java is create by Terry Huang [Huanghaipeng] on 2008-11-6 .
* Any question about this code please email to [email protected] or [email protected] .
* If you edit this source code , please add infomation on Revision History and let me know .
*
* Project:
* ATS - Credit Limit
*
* Revision History
*
* Version Date Programmer Notes
* ------- --------- --------------------------- --------------------------------------------
* V 1.00 2008-11-6 Terry Huang [Huanghaipeng] initial
*/
package com.gw.common.utils;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class HostTools {
public static String[][] getHost(boolean flag){
String as[][] = (String[][]) null;
String s = null;
String s1 = null;
try {
InetAddress ainetaddress[] = (InetAddress[]) null;
URL url = null;
Class> class1 = HostTools.class;
String s2 = class1.getName().replace('.', '/') + ".class";
if (flag)
System.out.println("*** DEBUG *** Class file name [" + s2
+ "].");
ClassLoader classloader = class1.getClassLoader();
if (classloader == null) {
if (flag)
System.out
.println("*** DEBUG *** Loaded via bootstrap class loader.");
url = class1.getResource("/" + s2);
if (url == null) {
if (flag)
System.out
.println("*** DEBUG *** Class file URL null when using leading / .");
url = class1.getResource(s2);
}
} else {
url = classloader.getResource("/" + s2);
if (url == null) {
if (flag)
System.out
.println("*** DEBUG *** Class file URL null when using leading / .");
url = classloader.getResource(s2);
}
}
if (flag)
System.out.println("*** DEBUG *** Class file URL [" + url
+ "].");
if (url == null){
System.out.println("URL is null ");
return null;
}
if (url.getProtocol().equals("jar")) {
if (flag)
System.out
.println("*** DEBUG *** Class file URL specified jar protocol..");
url = new URL(url.toString().substring(4));
}
boolean flag1 = false;
if (classloader != null
&& classloader.getClass().getName().equals(
"com.ms.vm.loader.DLLClassLoader")) {
if (flag)
System.out.println("*** DEBUG *** Loaded via MS COM DLL.");
flag1 = true;
}
if (url.getHost().length() > 0 && !flag1) {
if (flag)
System.out
.println("*** DEBUG *** Running as an Applet or via COM.");
s1 = url.getHost();
s = s1;
if (flag) {
System.out.println("*** DEBUG *** Fallback Addr set to["
+ s1 + "].");
System.out.println("*** DEBUG *** Fallback Name set to["
+ s + "].");
}
ainetaddress = InetAddress.getAllByName(s);
} else {
if (flag)
System.out
.println("*** DEBUG *** Running as an Application.");
s1 = InetAddress.getLocalHost().getHostAddress();
s = InetAddress.getLocalHost().getHostName();
if (flag) {
System.out.println("*** DEBUG *** Fallback Addr set to["
+ s1 + "].");
System.out.println("*** DEBUG *** Fallback Name set to["
+ s + "].");
}
ainetaddress = InetAddress.getAllByName(s1);
}
int i = ainetaddress.length;
if (flag)
System.out
.println("*** DEBUG *** [" + i + "] addresses found.");
as = new String[i][];
for (int j = 0; j < i; j++) {
as[j] = new String[2];
String s3 = ainetaddress[j].getHostAddress();
if ((s3 == null || s3.length() == 0) && flag)
System.out
.println("*** DEBUG *** Original host address empty or null.");
InetAddress inetaddress = InetAddress.getByName(s3);
String s4 = inetaddress.getHostName();
if ((s4 == null || s4.length() == 0) && flag)
System.out
.println("*** DEBUG *** Host name empty or null.");
as[j][0] = s4;
String s5 = inetaddress.getHostAddress();
if ((s5 == null || s5.length() == 0) && flag)
System.out
.println("*** DEBUG *** Host address empty or null.");
as[j][1] = s5;
}
} catch (SecurityException securityexception) {
if (s1 != null && s != null) {
if (flag)
System.out
.println("*** DEBUG *** Security Exception - using fallback details.");
as = new String[1][2];
as[0][0] = s;
as[0][1] = s1;
} else {
}
} catch (MalformedURLException malformedurlexception) {
if (s1 != null && s != null) {
if (flag)
System.out
.println("*** DEBUG *** Unknown Host Exception - using fallback details.");
as = new String[1][2];
as[0][0] = s;
as[0][1] = s1;
} else {
}
} catch (UnknownHostException unknownhostexception) {
if (s1 != null && s != null) {
if (flag)
System.out
.println("*** DEBUG *** Unknown Host Exception - using fallback details.");
as = new String[1][2];
as[0][0] = s;
as[0][1] = s1;
} else {
}
}
return as;
}
/**
* 获得本机name对应的ip,如果name为空,则使用本机名称
* @param name
* @return
* @throws InvalidHostException
*/
public static String[] getAddress(String name) throws InvalidHostException {
if(name==null || (name=name.trim()).length()==0){
name=getHostName();
}
InetAddress[] addressArr=null;
try {
addressArr = InetAddress.getAllByName(name);
} catch (UnknownHostException e) {
throw new InvalidHostException(e);
}
if(addressArr==null || addressArr.length==0){
throw new InvalidHostException("Can't get the ip for "+name);
}
String r[]=new String[addressArr.length];
for (int i=0;i0){
throw new InvalidHostException("Contains space character. - "+hostName);
}
return hostName;
}
/**
* 通过网段获取ip,如果有多个同网段的,则返回第一个
* @param filter 例如192、192.168、192.168.2
* @return ip地址
* @throws InvalidHostException
*/
public static String getIP(String filter) throws InvalidHostException{
String[] ips=getAllIP();
if(ips.length==0){
throw new InvalidHostException("Can't get ip. Maybe no ip is set.");
}
List al=new ArrayList();
for(String ip:ips){
if (ip.startsWith(filter)) {
al.add(ip);
}
}
if(al.size()==0){
throw new InvalidHostException("Can't get the ip for "+filter);
}else if(al.size()==1){
return al.get(0);
}else{
throw new InvalidHostException("More than one ip for "+filter);
}
}
/**
* 获得所有ip,不包括127.0.0.1
* @return
* @throws InvalidHostException
*/
public static String[] getAllIP() throws InvalidHostException{
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
throw new InvalidHostException(e);
}
List all=new ArrayList();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
String ipStr = ip.getHostAddress();
if (ipStr.equals("127.0.0.1")) {
continue;
}
all.add(ipStr);
}
}
}
return all.toArray(new String[all.size()]);
}
public static class InvalidHostException extends Exception{
public InvalidHostException(Exception e){
super(e);
}
public InvalidHostException(String str){
super(str);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy