From 9795fd2dc7c6d6bd680d18c7070ec251fef81184 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 31 Jul 2013 17:31:08 +0200 Subject: [PATCH] =?UTF-8?q?cleanup=20=E2=80=94=20NetUtils.getLoopbackAddre?= =?UTF-8?q?ss(),=20Overrides=20continue=20WI-17365=20internal=20web=20serv?= =?UTF-8?q?er=20should=20serve=20PHP=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compiler/server/BuildManager.java | 7 +++++-- .../src/com/intellij/util/net/NetUtils.java | 12 +++++++++++- .../src/com/intellij/idea/SocketLock.java | 16 ++++++++-------- .../src/org/jetbrains/io/BuiltInServer.java | 19 ++++--------------- .../src/org/jetbrains/io/SubServer.java | 4 ++-- .../intellij/execution/junit/TestPackage.java | 18 ++++++++++++++---- .../lang/xpath/xslt/run/OutputTabAdapter.java | 8 +++++--- .../plugins/xslt/run/rt/XSLTRunner.java | 2 +- 8 files changed, 50 insertions(+), 36 deletions(-) diff --git a/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java b/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java index b0a49698baba..e885a1670f1c 100644 --- a/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java +++ b/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java @@ -103,7 +103,6 @@ import org.jetbrains.jps.model.serialization.JpsGlobalLoader; import javax.tools.*; import java.io.File; import java.io.IOException; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.charset.Charset; import java.util.*; @@ -959,6 +958,7 @@ public class BuildManager implements ApplicationComponent{ private int startListening() throws Exception { final ChannelFactory channelFactory = new NioServerSocketChannelFactory(myPooledThreadExecutor, myPooledThreadExecutor, 1); final SimpleChannelUpstreamHandler channelRegistrar = new SimpleChannelUpstreamHandler() { + @Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { myAllOpenChannels.add(e.getChannel()); super.channelOpen(ctx, e); @@ -971,6 +971,7 @@ public class BuildManager implements ApplicationComponent{ } }; ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() { + @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( channelRegistrar, @@ -987,7 +988,7 @@ public class BuildManager implements ApplicationComponent{ bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.keepAlive", true); final int listenPort = NetUtils.findAvailableSocketPort(); - final Channel serverChannel = bootstrap.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), listenPort)); + final Channel serverChannel = bootstrap.bind(new InetSocketAddress(NetUtils.getLoopbackAddress(), listenPort)); myAllOpenChannels.add(serverChannel); return listenPort; } @@ -1222,6 +1223,7 @@ public class BuildManager implements ApplicationComponent{ super(path); } + @Override public String getValue() { if (myPath.length == 1) { final String name = FileNameCache.getVFileName(myPath[0]); @@ -1245,6 +1247,7 @@ public class BuildManager implements ApplicationComponent{ super(path); } + @Override public String getValue() { if (myPath.length > 0) { final StringBuilder buf = new StringBuilder(); diff --git a/platform/platform-api/src/com/intellij/util/net/NetUtils.java b/platform/platform-api/src/com/intellij/util/net/NetUtils.java index a36287e2bbd3..8d3bff92368f 100644 --- a/platform/platform-api/src/com/intellij/util/net/NetUtils.java +++ b/platform/platform-api/src/com/intellij/util/net/NetUtils.java @@ -32,7 +32,7 @@ import java.net.*; * @author yole */ public class NetUtils { - private static final Logger LOG = Logger.getInstance("#com.intellij.util.net.NetUtils"); + private static final Logger LOG = Logger.getInstance(NetUtils.class); private NetUtils() { } @@ -57,6 +57,16 @@ public class NetUtils { } } + public static InetAddress getLoopbackAddress() { + try { + // todo use JDK 7 InetAddress.getLoopbackAddress() + return InetAddress.getByName("127.0.0.1"); + } + catch (UnknownHostException e) { + throw new RuntimeException(e); + } + } + public static boolean isLocalhost(String host) { return host.equals("localhost") || host.equals("127.0.0.1"); } diff --git a/platform/platform-impl/src/com/intellij/idea/SocketLock.java b/platform/platform-impl/src/com/intellij/idea/SocketLock.java index d2b0fbf09b7d..149de23f8e46 100644 --- a/platform/platform-impl/src/com/intellij/idea/SocketLock.java +++ b/platform/platform-impl/src/com/intellij/idea/SocketLock.java @@ -22,6 +22,7 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.Consumer; +import com.intellij.util.net.NetUtils; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; @@ -108,7 +109,7 @@ public class SocketLock { try { FileUtil.writeToFile(portMarker, Integer.toString(port).getBytes()); } - catch (IOException e) { + catch (IOException ignored) { FileUtil.asyncDelete(portMarker); } } @@ -138,14 +139,14 @@ public class SocketLock { try { try { - ServerSocket serverSocket = new ServerSocket(portNumber, 50, InetAddress.getByName("127.0.0.1")); + ServerSocket serverSocket = new ServerSocket(portNumber, 50, NetUtils.getLoopbackAddress()); serverSocket.close(); return ActivateStatus.NO_INSTANCE; } - catch (IOException e) { + catch (IOException ignored) { } - Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), portNumber); + Socket socket = new Socket(NetUtils.getLoopbackAddress(), portNumber); socket.setSoTimeout(300); DataInputStream in = new DataInputStream(socket.getInputStream()); @@ -154,7 +155,7 @@ public class SocketLock { try { result.add(in.readUTF()); } - catch (IOException e) { + catch (IOException ignored) { break; } } @@ -168,7 +169,7 @@ public class SocketLock { return ActivateStatus.ACTIVATED; } } - catch(IOException e) { + catch(IOException ignored) { } return ActivateStatus.CANNOT_ACTIVATE; } @@ -197,7 +198,6 @@ public class SocketLock { } catch (IOException e) { LOG.info(e); - continue; } } @@ -238,7 +238,7 @@ public class SocketLock { } } } - catch (Throwable e) { + catch (Throwable ignored) { } } } diff --git a/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java b/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java index f3e04dec5359..6f6130652bce 100644 --- a/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java +++ b/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java @@ -19,6 +19,7 @@ import com.intellij.ide.XmlRpcServer; import com.intellij.openapi.Disposable; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Disposer; +import com.intellij.util.net.NetUtils; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.*; import org.jboss.netty.channel.group.ChannelGroup; @@ -33,7 +34,6 @@ import org.jetbrains.ide.PooledThreadExecutor; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.UnknownHostException; import java.util.Map; import java.util.concurrent.Executor; @@ -113,23 +113,12 @@ public class BuiltInServer implements Disposable { } } - // IDEA-91436 idea <121 binds to 127.0.0.1, but >=121 must be available not only from localhost - // but if we bind only to any local port (0.0.0.0), instance of idea <121 can bind to our ports and any request to us will be intercepted - // so, we bind to 127.0.0.1 and 0.0.0.0 private int bind(int firstPort, int portsCount, boolean tryAnyPort, ServerBootstrap bootstrap) { - InetAddress localAddress; - try { - localAddress = InetAddress.getByName("127.0.0.1"); - } - catch (UnknownHostException e) { - LOG.error(e); - return -1; - } - + InetAddress loopbackAddress = NetUtils.getLoopbackAddress(); for (int i = 0; i < portsCount; i++) { int port = firstPort + i; try { - openChannels.add(bootstrap.bind(new InetSocketAddress(localAddress, port))); + openChannels.add(bootstrap.bind(new InetSocketAddress(loopbackAddress, port))); return port; } catch (ChannelException e) { @@ -150,7 +139,7 @@ public class BuiltInServer implements Disposable { if (tryAnyPort) { LOG.info("We cannot bind to our default range, so, try to bind to any free port"); try { - Channel channel = bootstrap.bind(new InetSocketAddress(localAddress, 0)); + Channel channel = bootstrap.bind(new InetSocketAddress(loopbackAddress, 0)); openChannels.add(channel); return ((InetSocketAddress)channel.getLocalAddress()).getPort(); } diff --git a/platform/platform-impl/src/org/jetbrains/io/SubServer.java b/platform/platform-impl/src/org/jetbrains/io/SubServer.java index ba76208cde57..12512be2efd4 100644 --- a/platform/platform-impl/src/org/jetbrains/io/SubServer.java +++ b/platform/platform-impl/src/org/jetbrains/io/SubServer.java @@ -16,6 +16,7 @@ package org.jetbrains.io; import com.intellij.openapi.Disposable; +import com.intellij.util.net.NetUtils; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.group.ChannelGroup; import org.jboss.netty.channel.group.DefaultChannelGroup; @@ -23,7 +24,6 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jetbrains.ide.BuiltInServerManager; import org.jetbrains.ide.CustomPortServerManager; -import java.net.InetAddress; import java.net.InetSocketAddress; final class SubServer implements CustomPortServerManager.CustomPortService, Disposable { @@ -43,7 +43,7 @@ final class SubServer implements CustomPortServerManager.CustomPortService, Disp } try { - openChannels.add(bootstrap.bind(user.isAvailableExternally() ? new InetSocketAddress(port) : new InetSocketAddress(InetAddress.getByName("127.0.0.1"), port))); + openChannels.add(bootstrap.bind(user.isAvailableExternally() ? new InetSocketAddress(port) : new InetSocketAddress(NetUtils.getLoopbackAddress(), port))); return true; } catch (Exception e) { diff --git a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java index e6c1574e5617..7e6cf5c9c87b 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java @@ -33,7 +33,7 @@ import com.intellij.execution.testframework.TestSearchScope; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; -import com.intellij.openapi.module.ModuleUtil; +import com.intellij.openapi.module.ModuleUtilCore; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.Task; @@ -79,6 +79,7 @@ public class TestPackage extends TestObject { } + @Override public SourceScope getSourceScope() { final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); return data.getScope().getSourceScope(myConfiguration); @@ -114,15 +115,17 @@ public class TestPackage extends TestObject { try { filter = getClassFilter(data); } - catch (CantRunException e) { + catch (CantRunException ignored) { //should not happen return null; } return findTestsWithProgress(new FindCallback() { + @Override public void found(@NotNull final Collection classes, final boolean isJunit4) { try { addClassesListToJavaParameters(classes, new Function() { + @Override @Nullable public String fun(PsiElement element) { if (element instanceof PsiClass) { @@ -138,7 +141,7 @@ public class TestPackage extends TestObject { } }, getPackageName(data), false, isJunit4); } - catch (CantRunException e) { + catch (CantRunException ignored) { //can't be here } } @@ -149,6 +152,7 @@ public class TestPackage extends TestObject { return getPackage(data).getQualifiedName(); } + @Override protected void initialize() throws ExecutionException { super.initialize(); final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); @@ -185,6 +189,7 @@ public class TestPackage extends TestObject { protected void configureClasspath() throws ExecutionException { final ExecutionException[] exception = new ExecutionException[1]; ApplicationManager.getApplication().runReadAction(new Runnable() { + @Override public void run() { try { myConfiguration.configureClasspath(myJavaParameters); @@ -222,6 +227,7 @@ public class TestPackage extends TestObject { return aPackage; } + @Override public String suggestActionName() { final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); if (data.getPackageName().trim().length() > 0) { @@ -230,11 +236,13 @@ public class TestPackage extends TestObject { return ExecutionBundle.message("all.tests.scope.presentable.text"); } + @Override public RefactoringElementListener getListener(final PsiElement element, final JUnitConfiguration configuration) { if (!(element instanceof PsiPackage)) return null; return RefactoringListeners.getListener((PsiPackage)element, configuration.myPackage); } + @Override public boolean isConfiguredByElement(final JUnitConfiguration configuration, PsiClass testClass, PsiMethod testMethod, @@ -243,6 +251,7 @@ public class TestPackage extends TestObject { && Comparing.equal(testPackage.getQualifiedName(), configuration.getPersistentData().getPackageName()); } + @Override public void checkConfiguration() throws RuntimeConfigurationException { super.checkConfiguration(); final String packageName = myConfiguration.getPersistentData().getPackageName(); @@ -293,7 +302,7 @@ public class TestPackage extends TestObject { final Set modulesWithPackage = new HashSet(); final PsiDirectory[] directories = aPackage.getDirectories(); for (PsiDirectory directory : directories) { - final Module currentModule = ModuleUtil.findModuleForFile(directory.getVirtualFile(), project); + final Module currentModule = ModuleUtilCore.findModuleForFile(directory.getVirtualFile(), project); if (module != currentModule && currentModule != null) { modulesWithPackage.add(currentModule); } @@ -389,6 +398,7 @@ public class TestPackage extends TestObject { } + @Override public void run(@NotNull ProgressIndicator indicator) { try { mySocket = myServerSocket.accept(); diff --git a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/run/OutputTabAdapter.java b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/run/OutputTabAdapter.java index 2853eb7f6188..66aeeef4cfb6 100644 --- a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/run/OutputTabAdapter.java +++ b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/run/OutputTabAdapter.java @@ -21,6 +21,7 @@ import com.intellij.execution.process.ProcessHandler; import com.intellij.execution.process.ProcessOutputTypes; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.util.Key; +import com.intellij.util.net.NetUtils; import org.jetbrains.annotations.Nullable; import java.io.IOException; @@ -28,7 +29,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.ConnectException; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; @@ -43,6 +43,7 @@ class OutputTabAdapter extends ProcessAdapter { myConsole = console; } + @Override public void startNotified(ProcessEvent event) { final XsltCommandLineState state = event.getProcessHandler().getUserData(XsltCommandLineState.STATE); if (state != null) { @@ -52,6 +53,7 @@ class OutputTabAdapter extends ProcessAdapter { public void attachOutputConsole(final int port) { ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { + @Override public void run() { try { final InputStream stream; @@ -92,7 +94,7 @@ class OutputTabAdapter extends ProcessAdapter { @Nullable private InputStream connect(int port) throws IOException { final long s = System.currentTimeMillis(); - final InetSocketAddress endpoint = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), port); + final InetSocketAddress endpoint = new InetSocketAddress(NetUtils.getLoopbackAddress(), port); myStartedProcess.notifyTextAvailable("Connecting to XSLT runner on " + endpoint + "\n", ProcessOutputTypes.SYSTEM); @@ -109,7 +111,7 @@ class OutputTabAdapter extends ProcessAdapter { return socket.getInputStream(); } catch (ConnectException e) { ex = e; - try { Thread.sleep(500); } catch (InterruptedException e1) { break; } + try { Thread.sleep(500); } catch (InterruptedException ignored) { break; } } if (myStartedProcess.isProcessTerminated() || myStartedProcess.isProcessTerminating()) { return null; diff --git a/plugins/xpath/xslt-rt/src/org/intellij/plugins/xslt/run/rt/XSLTRunner.java b/plugins/xpath/xslt-rt/src/org/intellij/plugins/xslt/run/rt/XSLTRunner.java index 2655a5f4867f..b2e919c341ac 100644 --- a/plugins/xpath/xslt-rt/src/org/intellij/plugins/xslt/run/rt/XSLTRunner.java +++ b/plugins/xpath/xslt-rt/src/org/intellij/plugins/xslt/run/rt/XSLTRunner.java @@ -100,7 +100,7 @@ public class XSLTRunner implements XSLTMain { } else { result = new StreamResult(new OutputStreamWriter(socketStream, "UTF-8")); } - } catch (SocketTimeoutException e) { + } catch (SocketTimeoutException ignored) { System.err.println("Plugin did not connect to runner within timeout. Run aborted."); return; }