cleanup — NetUtils.getLoopbackAddress(), Overrides

continue WI-17365 internal web server should serve PHP files
This commit is contained in:
Vladimir Krivosheev
2013-07-31 17:31:42 +02:00
parent f7f0f914c3
commit 9795fd2dc7
8 changed files with 50 additions and 36 deletions
@@ -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();
@@ -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");
}
@@ -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) {
}
}
}
@@ -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();
}
@@ -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) {
@@ -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<PsiClass> classes, final boolean isJunit4) {
try {
addClassesListToJavaParameters(classes, new Function<PsiElement, String>() {
@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<Module> modulesWithPackage = new HashSet<Module>();
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();
@@ -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;
@@ -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;
}