import mfutil.ValidateUtil;
/**
* A server contain it's message.
* Such as IP,Hostname
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class Server
{
private final String IP;
private final String HostName;
private String reason;
public static ValidateUtil vu;
/**
*
* @param IP
* @param HostName
*/
private Server(String IP,String HostName)
{
this.IP = IP;
this.HostName = HostName;
if(!this.validate())
throw new java.lang.IllegalArgumentException("Invalid argument to create server object,reason:" +
this.reason);
}
/**
* Create a server object by specified name and ip.
* @param IP
* @param HostName
* @return
*/
public static Server Create(String IP,String HostName)
{
return new Server(IP,HostName);
}
public String getIP()
{
return this.IP;
}
public String getHostName()
{
return this.HostName;
}
public String getReason()
{
return this.reason;
}
private boolean validate()
{
vu = new mfutil.ValidateUtil();
if(vu.isBlankOrNull(IP)){
this.reason = "IP address can not be null,while create server";
return false;
}
if(vu.isBlankOrNull(HostName)){
this.reason = "HostName can not be null,while create server";
return false;
}
/**
* Get the server by specified server type.
*
* @param serverType The server type indicates the server
* belongs to local netwoork or outside network
* @return the server indicates a computer links to the network
*/
public static Server getServer(ServerType serverType)
{
try
{
Enumeration e = java.net.NetworkInterface.getNetworkInterfaces();
ArrayList al = new ArrayList();
while(e.hasMoreElements())
{
NetworkInterface ni = (NetworkInterface)e.nextElement();
Enumeration e2 = ni.getInetAddresses();
while(e2.hasMoreElements())
{
InetAddress ia = (InetAddress)e2.nextElement();
Server server = Server.Create(ia.getHostAddress(),ia.getHostName());
al.add(server);
}
}
Server[] servers = new Server[al.size()];
al.toArray(servers);
return serverType.getServer(servers);
}
catch(java.net.SocketException e)
{
throw new mfutil.MFUtilException("Can not get the ip address to initalzie server object");
}
}
public static void main(String args[])
{
mfutil.server.LocalServerType lst = new LocalServerType();
Server server = NetworkUtil.getServer(lst);
System.out.println(server.getIP());
System.out.println(server.getHostName());
// LocalSeverType st = new LocalServerType();
//System.out.println(NetworkUtil.getLocalIP(args[0]));
}
}