From da5f06287c0afd3277fa5191a54192d39b5dbd00 Mon Sep 17 00:00:00 2001 From: Deepak Pansheriya Date: Sun, 21 Oct 2012 11:58:52 +0530 Subject: [PATCH] IDEMPIERE-389 - Fix to set non loopback and non link IP on ServerName --- .../src/org/compiere/util/Ini.java | 7 +-- .../src/org/compiere/util/WebUtil.java | 57 +++++++++++++++---- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/util/Ini.java b/org.adempiere.base/src/org/compiere/util/Ini.java index bf35e8ae39..7e9ae05d39 100644 --- a/org.adempiere.base/src/org/compiere/util/Ini.java +++ b/org.adempiere.base/src/org/compiere/util/Ini.java @@ -141,9 +141,6 @@ public final class Ini implements Serializable /** Role */ public static final String P_ROLE = "Role"; private static final String DEFAULT_ROLE = ""; - /**Server Name */ - public static final String P_SERVERNAME = "ServerName"; - private static final String DEFAULT_SERVERNAME = ""; /** Client Name */ public static final String P_CLIENT = "Client"; private static final String DEFAULT_CLIENT = ""; @@ -199,7 +196,7 @@ public final class Ini implements Serializable P_ADEMPIERESYS, P_LOGMIGRATIONSCRIPT, P_SHOW_ACCT, P_SHOW_TRL, P_SHOW_ADVANCED, P_CACHE_WINDOW, P_CONTEXT, P_TEMP_DIR, - P_ROLE, P_SERVERNAME, P_CLIENT, P_ORG, P_PRINTER, P_WAREHOUSE, P_TODAY, + P_ROLE, P_CLIENT, P_ORG, P_PRINTER, P_WAREHOUSE, P_TODAY, P_PRINTPREVIEW, P_VALIDATE_CONNECTION_ON_STARTUP, P_SINGLE_INSTANCE_PER_WINDOW, @@ -217,7 +214,7 @@ public final class Ini implements Serializable DEFAULT_ADEMPIERESYS?"Y":"N", DEFAULT_LOGMIGRATIONSCRIPT?"Y":"N", DEFAULT_SHOW_ACCT?"Y":"N", DEFAULT_SHOW_TRL?"Y":"N", DEFAULT_SHOW_ADVANCED?"Y":"N", DEFAULT_CACHE_WINDOW?"Y":"N", DEFAULT_CONTEXT, DEFAULT_TEMP_DIR, - DEFAULT_ROLE, DEFAULT_SERVERNAME, DEFAULT_CLIENT, DEFAULT_ORG, DEFAULT_PRINTER, DEFAULT_WAREHOUSE, DEFAULT_TODAY.toString(), + DEFAULT_ROLE, DEFAULT_CLIENT, DEFAULT_ORG, DEFAULT_PRINTER, DEFAULT_WAREHOUSE, DEFAULT_TODAY.toString(), DEFAULT_PRINTPREVIEW?"Y":"N", DEFAULT_VALIDATE_CONNECTION_ON_STARTUP?"Y":"N", DEFAULT_SINGLE_INSTANCE_PER_WINDOW?"Y":"N", diff --git a/org.adempiere.base/src/org/compiere/util/WebUtil.java b/org.adempiere.base/src/org/compiere/util/WebUtil.java index 5000605bbd..f5dbbfd74b 100644 --- a/org.adempiere.base/src/org/compiere/util/WebUtil.java +++ b/org.adempiere.base/src/org/compiere/util/WebUtil.java @@ -25,6 +25,8 @@ import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.net.URLDecoder; import java.net.URLEncoder; import java.net.UnknownHostException; @@ -32,6 +34,7 @@ import java.sql.Timestamp; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.SimpleDateFormat; +import java.util.Enumeration; import java.util.Properties; import java.util.logging.Level; @@ -1259,24 +1262,56 @@ public final class WebUtil */ public static String getServerName(){ StringBuilder strBuilder = new StringBuilder(); - String serverName = Ini.getProperties().getProperty("ServerName"); - try { strBuilder.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { log.log(Level.WARNING, "Local host or IP not found", e); } - strBuilder.append(":"); - try { - strBuilder.append(InetAddress.getLocalHost().getHostAddress()); - } catch (UnknownHostException e) { - log.log(Level.WARNING, "Local host or IP not found", e); - } - strBuilder.append(":"); - if(serverName!=null) - strBuilder.append(serverName); + strBuilder.append(":").append(getHostIP()); + return strBuilder.toString(); } + + public static String getHostIP() { + String retVal = null; + try { + InetAddress localAddress= InetAddress.getLocalHost(); + if (!localAddress.isLinkLocalAddress() && !localAddress.isLoopbackAddress() && localAddress.isSiteLocalAddress()) + return localAddress.getHostAddress(); + } catch (UnknownHostException e) { + log.log(Level.WARNING, + "UnknownHostException while retrieving host ip"); + } + + try { + for (Enumeration en = NetworkInterface + .getNetworkInterfaces(); en.hasMoreElements();) { + NetworkInterface intf = en.nextElement(); + for (Enumeration enumIpAddr = intf + .getInetAddresses(); enumIpAddr.hasMoreElements();) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress() + && !inetAddress.isLinkLocalAddress() + && inetAddress.isSiteLocalAddress()) { + retVal = inetAddress.getHostAddress().toString(); + break; + } + } + } + } catch (SocketException e) { + log.log(Level.WARNING, "Socket Exeception while retrieving host ip"); + } + + if (retVal == null) { + try { + retVal = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + log.log(Level.WARNING, + "UnknownHostException while retrieving host ip"); + } + } + return retVal; + } } // WUtil