[Schmitzm-commits] r2007 - in trunk/schmitzm-core/src/main/java/de/schmitzm: io swing

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu May 24 19:21:03 CEST 2012


Author: mojays
Date: 2012-05-24 19:21:03 +0200 (Thu, 24 May 2012)
New Revision: 2007

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ConnectionSettingsPanel.java
Log:
ConnectionSettingsPanel: removed unused code
IOUtil: workaround for getHostString() problem (avoid Java 1.7 method)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-05-24 14:23:38 UTC (rev 2006)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-05-24 17:21:03 UTC (rev 2007)
@@ -1458,6 +1458,52 @@
 	  return new Proxy(proxyType, proxySocket);
 	}
 	
+	/**
+	 * Workaround method to determine if the hostname of a {@link SocketAddress} is
+	 * already resolved WITHOUT causing an automatical DNS lookup to resolve it!!
+	 * @param addr socket address
+	 * @deprecated method not recommended for common use, just for the internal workaround
+	 *             in {@link #getHostString(InetSocketAddress)} unless the Java 1.7 method
+	 *             {@link InetSocketAddress#getHostString()} can be established!
+	 */
+	private static boolean isHostnameResolved(SocketAddress addr) {
+	  if ( addr == null )
+	    return false;
+	  // If hostname is not yet present, toString() contains only the
+	  // IP address and starts with "/"
+	  return !addr.toString().trim().startsWith("/");
+	}
+
+	/**
+	 * Returns the hostname or IP address without causing a DNS (reverse) lookup
+	 * @param socketAddr a socket address
+	 */
+	public static String getHostString(InetSocketAddress socketAddr) {
+	  LOGGER.debug(socketAddr);
+      // Workaround to
+      // - not use InetSocketAddress.getHostString() because it is Java 1.7 
+      // - avoid DNS lookup in any direction
+      // -> Check if hostname already resolved
+      //    If yes, return getHostName() because there will be no further lookup
+      //    If no, return socketAddr.getAddress().getHostAddress() because socket
+      //           was specified by IP address
+      if ( isHostnameResolved(socketAddr) )
+        return socketAddr.getHostName();
+      else
+        return socketAddr.getAddress().getHostAddress();
+      
+// http://hudson.wikisquare.de/hudson/job/schmitzm-library-trunk/1083/changes      
+//      try {
+//        // Java 1.7, avoids DNS reverse lookup :-)
+//        return socketAddr.getHostString();
+//      } catch (Throwable err) {
+//        LOGGER.debug("Error using InetSocketAddress.getHostString() [Java 1.7]",err);
+//        LOGGER.debug("Instead InetSocketAddress.getHostName() is used [Java 1.6]");
+//        // Java 1.6, may cause DNS reverse lookup :-(
+//        return socketAddr.getHostName();
+//      } 
+	}
+	
     /**
      * Returns the host name of a proxy.
      */
@@ -1467,12 +1513,7 @@
       InetSocketAddress socketAddr = (InetSocketAddress)proxy.address();
       if ( socketAddr == null )
         return null;
-// http://hudson.wikisquare.de/hudson/job/schmitzm-library-trunk/1083/changes      
-//      try {
-//        return socketAddr.getHostString();
-//      } catch (Throwable err) {
-        return socketAddr.getHostName();
-//      } 
+      return getHostString(socketAddr);
     }
 
     /**

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ConnectionSettingsPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ConnectionSettingsPanel.java	2012-05-24 14:23:38 UTC (rev 2006)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ConnectionSettingsPanel.java	2012-05-24 17:21:03 UTC (rev 2007)
@@ -184,15 +184,7 @@
       connRetries.setValue(0);
       return;
     }
-    Proxy             proxy      = settings.getProxy();
-    InetSocketAddress socketAddr = proxy != null ? (InetSocketAddress)proxy.address() : null;
-    String host = "";
-//    try {
-// Das ist ein Compile-Fehler.. da hilft kein try/catch ;-)    
-//      host = socketAddr != null ? socketAddr.getHostString() : null;
-//    } catch (Throwable err) {
-      host = socketAddr != null ? socketAddr.getHostName() : null;
-//    }    
+    Proxy proxy = settings.getProxy();
     setProxy(settings.isProxyUsed(),
              proxy != null ? proxy.type() : Type.HTTP,
              IOUtil.getProxyHost(proxy),



More information about the Schmitzm-commits mailing list