You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2013/03/27 21:19:10 UTC

svn commit: r1461837 - in /geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core: GeronimoServerBehaviourDelegate.java util/Utils.java

Author: gawor
Date: Wed Mar 27 20:19:10 2013
New Revision: 1461837

URL: http://svn.apache.org/r1461837
Log:
GERONIMODEVTOOLS-809: Improve in-use port check

Modified:
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/util/Utils.java

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java?rev=1461837&r1=1461836&r2=1461837&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java (original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java Wed Mar 27 20:19:10 2013
@@ -62,6 +62,7 @@ import org.apache.geronimo.st.v30.core.o
 import org.apache.geronimo.st.v30.core.osgi.AriesHelper;
 import org.apache.geronimo.st.v30.core.osgi.AriesHelper.BundleInfo;
 import org.apache.geronimo.st.v30.core.osgi.OSGiModuleHandler;
+import org.apache.geronimo.st.v30.core.util.Utils;
 import org.apache.geronimo.system.jmx.KernelDelegate;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IFile;
@@ -211,14 +212,18 @@ public class GeronimoServerBehaviourDele
     synchronized protected void setupLaunch(ILaunch launch, String launchMode, IProgressMonitor monitor) throws CoreException {
         Trace.tracePoint("Entry", Activator.traceCore, "GeronimoServerBehaviourDelegate.setupLaunch", launch, launchMode, monitor); 
 
-        if (!SocketUtil.isLocalhost(getServer().getHost()))
+        String host = getServer().getHost();
+        
+        if (!SocketUtil.isLocalhost(host)) {
             return;
+        }
 
         ServerPort[] ports = getServer().getServerPorts(null);
         for (int i = 0; i < ports.length; i++) {
             ServerPort sp = ports[i];
-            if (SocketUtil.isPortInUse(ports[i].getPort(), 5))
+            if (Utils.isPortInUse(host, ports[i].getPort(), 5)) {
                 throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, Messages.bind(Messages.errorPortInUse, Integer.toString(sp.getPort()), sp.getName()), null));
+            }
         }
         
         stopUpdateServerStateTask();

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/util/Utils.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/util/Utils.java?rev=1461837&r1=1461836&r2=1461837&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/util/Utils.java (original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/util/Utils.java Wed Mar 27 20:19:10 2013
@@ -17,17 +17,59 @@
 package org.apache.geronimo.st.v30.core.util;
 
 import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.geronimo.st.v30.core.Activator;
+import org.apache.geronimo.st.v30.core.internal.Trace;
+import org.eclipse.wst.server.core.util.SocketUtil;
 
 /**
- * 
- *
  * @version $Rev$ $Date$
  */
 public class Utils {
+    
     public static Object getValueByFieldName(String fieldName, Object instance) throws Exception {
         Field f = instance.getClass().getDeclaredField(fieldName);
         f.setAccessible(true);
         return f.get(instance);
     } 
     
+    public static boolean isPortInUse(String host, int port, int count) {
+        List<InetAddress> addresses = new ArrayList<InetAddress>();
+        // check any/all address
+        addresses.add(null);
+        try {
+            // check specific address
+            addresses.add(InetAddress.getByName(host));
+        } catch (UnknownHostException e) {
+            Trace.trace(Trace.WARNING, "Host lookup error for " + host, e, Activator.traceCore);
+        }
+        return isPortInUse(addresses, port, count);
+    }
+    
+    public static boolean isPortInUse(List<InetAddress> addresses, int port, int count) {
+        boolean inUse = isPortInUse(addresses, port);
+        while (inUse && count > 0) {
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException e) {
+                // ignore
+            }
+            inUse = isPortInUse(addresses, port);
+            count--;
+        }
+        return inUse;
+    }
+    
+    public static boolean isPortInUse(List<InetAddress> addresses, int port) {
+        for (InetAddress address : addresses) {
+            if (SocketUtil.isPortInUse(address, port)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }