From 90b1294dc79e9d39a5afff480cdae51ff6afcf5e Mon Sep 17 00:00:00 2001 From: Eugene Zhuravlev Date: Sat, 19 Mar 2005 15:28:15 +0300 Subject: [PATCH] (no message) --- .../debugger/engine/DebuggerUtils.java | 3 +++ .../debugger/impl/DebuggerManagerImpl.java | 23 +++++----------- .../debugger/impl/DebuggerUtilsImpl.java | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/openapi/src/com/intellij/debugger/engine/DebuggerUtils.java b/openapi/src/com/intellij/debugger/engine/DebuggerUtils.java index 5f6c2c48514d..e116b13b113b 100644 --- a/openapi/src/com/intellij/debugger/engine/DebuggerUtils.java +++ b/openapi/src/com/intellij/debugger/engine/DebuggerUtils.java @@ -14,6 +14,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.InheritanceUtil; +import com.intellij.execution.ExecutionException; import com.sun.jdi.*; import org.jdom.Element; @@ -302,6 +303,8 @@ public abstract class DebuggerUtils implements ApplicationComponent { } } + public abstract int findAvailablePort() throws ExecutionException; + protected static class ArrayClass { public String className; public int dims; diff --git a/source/com/intellij/debugger/impl/DebuggerManagerImpl.java b/source/com/intellij/debugger/impl/DebuggerManagerImpl.java index 434f06aa617f..2c46a97d8442 100644 --- a/source/com/intellij/debugger/impl/DebuggerManagerImpl.java +++ b/source/com/intellij/debugger/impl/DebuggerManagerImpl.java @@ -35,7 +35,6 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; -import java.net.ServerSocket; import java.net.UnknownHostException; import java.util.Collection; import java.util.HashMap; @@ -324,23 +323,12 @@ public class DebuggerManagerImpl extends DebuggerManagerEx { if (debugPort == null || "".equals(debugPort)) { if(useSockets) { try { - ServerSocket serverSocket = new ServerSocket(0); - address = Integer.toString(serverSocket.getLocalPort()); - //workaround for linux : calling close() immediately after opening socket - //may result that socket is not closed - synchronized(parameters) { - try { - parameters.wait(1); - } - catch (InterruptedException e) { - LOG.error(e); - } - } - serverSocket.close(); + final int freePort = DebuggerUtils.getInstance().findAvailablePort(); + address = Integer.toString(freePort); } - catch (IOException e) { + catch (ExecutionException e) { if (checkValidity) { - throw new ExecutionException(DebugProcessImpl.processError(e)); + throw e; } } } @@ -363,7 +351,8 @@ public class DebuggerManagerImpl extends DebuggerManagerEx { if(serverMode && useSockets) { try { listenTo = InetAddress.getLocalHost().getHostName() + ":" + address; - } catch (UnknownHostException e) { + } + catch (UnknownHostException e) { listenTo = "localhost:" + address; } } diff --git a/source/com/intellij/debugger/impl/DebuggerUtilsImpl.java b/source/com/intellij/debugger/impl/DebuggerUtilsImpl.java index b493bc4ca956..32a63207550a 100644 --- a/source/com/intellij/debugger/impl/DebuggerUtilsImpl.java +++ b/source/com/intellij/debugger/impl/DebuggerUtilsImpl.java @@ -24,10 +24,13 @@ import com.intellij.ide.util.TreeClassChooserDialog; import com.intellij.ide.util.TreeClassChooser; import com.intellij.ide.util.TreeClassChooserFactory; import com.intellij.util.IncorrectOperationException; +import com.intellij.execution.ExecutionException; import com.sun.jdi.Value; import org.jdom.Element; import java.util.HashMap; +import java.net.ServerSocket; +import java.io.IOException; /* * Copyright (c) 2000-2004 by JetBrains s.r.o. All Rights Reserved. @@ -112,4 +115,27 @@ public class DebuggerUtilsImpl extends DebuggerUtilsEx{ public CompletionEditor createEditor(Project project, PsiElement context, String recentsId) { return new DebuggerExpressionComboBox(project, context, recentsId); } + + public final int findAvailablePort() throws ExecutionException { + int port; + try { + final ServerSocket serverSocket = new ServerSocket(0); + port = serverSocket.getLocalPort(); + //workaround for linux : calling close() immediately after opening socket + //may result that socket is not closed + synchronized(serverSocket) { + try { + serverSocket.wait(1); + } + catch (InterruptedException e) { + LOG.error(e); + } + } + serverSocket.close(); + } + catch (IOException e) { + throw new ExecutionException(DebugProcessImpl.processError(e)); + } + return port; + } }