From 811cf5c0b860af834c7e41985ce7121894b1e2be Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 23 Aug 2011 15:42:04 +0200 Subject: [PATCH] Cleanup --- .../execution/runners/ProcessProxyImpl.java | 50 ++++++++++--------- .../execution/util/JavaParametersUtil.java | 9 ++-- .../configurations/CommandLineBuilder.java | 43 +++++++++------- .../configurations/SimpleJavaParameters.java | 8 +-- .../openapi/projectRoots/JdkUtil.java | 19 +++---- .../intellij/openapi/util/io/FileUtil.java | 14 +++--- .../openapi/util/system/ExecUtil.java | 21 ++++---- 7 files changed, 88 insertions(+), 76 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java b/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java index e1a5ff5810e3..db2f5df90eb2 100644 --- a/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java +++ b/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,37 +30,32 @@ import java.net.Socket; /** * @author ven */ - class ProcessProxyImpl implements ProcessProxy { public static final Key KEY = Key.create("ProcessProxyImpl"); - private final int myPortNumber; + @NonNls public static final String PROPERTY_BINPATH = "idea.launcher.bin.path"; + @NonNls public static final String PROPERTY_PORT_NUMBER = "idea.launcher.port"; + @NonNls public static final String LAUNCH_MAIN_CLASS = "com.intellij.rt.execution.application.AppMain"; + + @NonNls protected static final String LOCALHOST = "localhost"; + @NonNls private static final String DONT_USE_LAUNCHER_PROPERTY = "idea.no.launcher"; private static final int SOCKET_NUMBER_START = 7532; private static final int SOCKET_NUMBER = 100; private static final boolean[] ourUsedSockets = new boolean[SOCKET_NUMBER]; + private final int myPortNumber; private PrintWriter myWriter; private Socket mySocket; - @NonNls private static final String DONT_USE_LAUNCHER_PROPERTY = "idea.no.launcher"; - @NonNls public static final String PROPERTY_BINPATH = "idea.launcher.bin.path"; - @NonNls public static final String PROPERTY_PORT_NUMBER = "idea.launcher.port"; - @NonNls public static final String LAUNCH_MAIN_CLASS = "com.intellij.rt.execution.application.AppMain"; - @NonNls - protected static final String LOCALHOST = "localhost"; - - public int getPortNumber() { - return myPortNumber; - } public static class NoMoreSocketsException extends Exception { } - public ProcessProxyImpl () throws NoMoreSocketsException { - myPortNumber = getPortNumer(); + public ProcessProxyImpl() throws NoMoreSocketsException { + myPortNumber = findFreePort(); if (myPortNumber == -1) throw new NoMoreSocketsException(); } - private static int getPortNumer() { + private static int findFreePort() { synchronized (ourUsedSockets) { for (int j = 0; j < SOCKET_NUMBER; j++) { if (ourUsedSockets[j]) continue; @@ -69,15 +64,19 @@ class ProcessProxyImpl implements ProcessProxy { s.close(); ourUsedSockets[j] = true; return j + SOCKET_NUMBER_START; - } catch (IOException e) { - continue; } + catch (IOException ignore) { } } } return -1; } - public void finalize () throws Throwable { + public int getPortNumber() { + return myPortNumber; + } + + @SuppressWarnings("FinalizeDeclaration") + protected synchronized void finalize() throws Throwable { if (myWriter != null) { myWriter.close(); } @@ -89,13 +88,16 @@ class ProcessProxyImpl implements ProcessProxy { processHandler.putUserData(KEY, this); } - private synchronized void writeLine (@NonNls final String s) { + @SuppressWarnings({"SocketOpenedButNotSafelyClosed", "IOResourceOpenedButNotSafelyClosed"}) + private synchronized void writeLine(@NonNls final String s) { if (myWriter == null) { try { - if (mySocket == null) + if (mySocket == null) { mySocket = new Socket(InetAddress.getByName(LOCALHOST), myPortNumber); + } myWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(mySocket.getOutputStream()))); - } catch (IOException e) { + } + catch (IOException e) { return; } } @@ -103,11 +105,11 @@ class ProcessProxyImpl implements ProcessProxy { myWriter.flush(); } - public void sendBreak () { + public void sendBreak() { writeLine("BREAK"); } - public void sendStop () { + public void sendStop() { writeLine("STOP"); } diff --git a/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java b/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java index 32139d5cb299..bb8f62966e21 100644 --- a/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java +++ b/java/execution/impl/src/com/intellij/execution/util/JavaParametersUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,11 +36,12 @@ import com.intellij.psi.PsiFile; import java.util.Map; /** - * User: lex - * Date: Nov 26, 2003 - * Time: 10:38:01 PM + * @author lex + * @since Nov 26, 2003 */ public class JavaParametersUtil { + private JavaParametersUtil() { } + public static void configureConfiguration(SimpleJavaParameters parameters, CommonJavaRunConfigurationParameters configuration) { ProgramParametersUtil.configureConfiguration(parameters, configuration); diff --git a/java/execution/openapi/src/com/intellij/execution/configurations/CommandLineBuilder.java b/java/execution/openapi/src/com/intellij/execution/configurations/CommandLineBuilder.java index 9920fc5f5b73..9a9fac41638b 100644 --- a/java/execution/openapi/src/com/intellij/execution/configurations/CommandLineBuilder.java +++ b/java/execution/openapi/src/com/intellij/execution/configurations/CommandLineBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ package com.intellij.execution.configurations; import com.intellij.execution.CantRunException; import com.intellij.execution.ExecutionBundle; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.JavaSdkType; import com.intellij.openapi.projectRoots.JdkUtil; @@ -31,34 +30,43 @@ import com.intellij.openapi.projectRoots.SdkType; import com.intellij.openapi.util.Computable; public class CommandLineBuilder { - private static final Logger LOG = Logger.getInstance("#" + CommandLineBuilder.class.getName()); + private CommandLineBuilder() { } public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters) throws CantRunException { return createFromJavaParameters(javaParameters, false); } /** - * In order to avoid too long cmd problem dynamic classpath can be used + * In order to avoid too long cmd problem dynamic classpath can be used - if allowed by both {@code dynamicClasspath} parameter + * and project settings. + * + * @param javaParameters parameters. + * @param project a project to get a dynamic classpath setting from. * @param dynamicClasspath whether system properties and project settings will be able to cause using dynamic classpath. If false, - * classpath will always be passed through the command line. + * classpath will always be passed through the command line. + * @return a command line. + * @throws CantRunException if there are problems with JDK setup. */ - public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters, final Project project, final boolean dynamicClasspath) throws CantRunException { + public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters, + final Project project, + final boolean dynamicClasspath) throws CantRunException { return createFromJavaParameters(javaParameters, dynamicClasspath && JdkUtil.useDynamicClasspath(project)); } /** - * @param javaParameters parameters - * @param forceDynamicClasspath whether dynamic classpath will be used for this execution, to prevent problems caused by too long command line - * @return command line - * @throws CantRunException if there are problems with JDK setup + * @param javaParameters parameters. + * @param forceDynamicClasspath whether dynamic classpath will be used for this execution, to prevent problems caused by too long command line. + * @return a command line. + * @throws CantRunException if there are problems with JDK setup. */ - public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters, final boolean forceDynamicClasspath) throws CantRunException { + public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters, + final boolean forceDynamicClasspath) throws CantRunException { try { return ApplicationManager.getApplication().runReadAction(new Computable() { public GeneralCommandLine compute() { try { final Sdk jdk = javaParameters.getJdk(); - if(jdk == null) { + if (jdk == null) { throw new CantRunException(ExecutionBundle.message("run.configuration.error.no.jdk.specified")); } @@ -66,9 +74,9 @@ public class CommandLineBuilder { if (!(sdkType instanceof JavaSdkType)) { throw new CantRunException(ExecutionBundle.message("run.configuration.error.no.jdk.specified")); } - + final String exePath = ((JavaSdkType)sdkType).getVMExecutablePath(jdk); - if(exePath == null) { + if (exePath == null) { throw new CantRunException(ExecutionBundle.message("run.configuration.cannot.find.vm.executable")); } if (javaParameters.getMainClass() == null) { @@ -84,11 +92,12 @@ public class CommandLineBuilder { }); } catch (RuntimeException e) { - if(e.getCause() instanceof CantRunException) + if (e.getCause() instanceof CantRunException) { throw (CantRunException)e.getCause(); - else + } + else { throw e; + } } } - } diff --git a/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java b/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java index 0946e86544bd..042a0906bff9 100644 --- a/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java +++ b/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -82,9 +82,9 @@ public class SimpleJavaParameters extends SimpleProgramParameters { public OSProcessHandler createOSProcessHandler() throws ExecutionException { final Sdk sdk = getJdk(); assert sdk != null : "SDK should be defined"; - final GeneralCommandLine commandLine = JdkUtil.setupJVMCommandLine(((JavaSdkType)sdk.getSdkType()).getVMExecutablePath(sdk), this, - JdkUtil.useDynamicClasspath( - PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext()))); + final String exePath = ((JavaSdkType)sdk.getSdkType()).getVMExecutablePath(sdk); + final boolean forceDynamicClasspath = JdkUtil.useDynamicClasspath(PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext())); + final GeneralCommandLine commandLine = JdkUtil.setupJVMCommandLine(exePath, this, forceDynamicClasspath); final OSProcessHandler processHandler = new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString()) { @Override public Charset getCharset() { diff --git a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java index cf0259da26e8..33683e65c65e 100644 --- a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java +++ b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +39,6 @@ import org.jetbrains.annotations.Nullable; import java.io.*; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; -import java.util.Map; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -112,7 +111,6 @@ public class JdkUtil { return null; } - public static boolean checkForJdk(File file) { file = new File(file.getAbsolutePath() + File.separator + "bin"); if (!file.exists()) return false; @@ -141,10 +139,12 @@ public class JdkUtil { return children != null && children.length >= 1; } - public static GeneralCommandLine setupJVMCommandLine(final String exePath, final SimpleJavaParameters javaParameters, - final boolean forceDynamicClasspath) { + public static GeneralCommandLine setupJVMCommandLine(final String exePath, + final SimpleJavaParameters javaParameters, + final boolean forceDynamicClasspath) { final GeneralCommandLine commandLine = new GeneralCommandLine(); commandLine.setExePath(exePath); + ParametersList parametersList = javaParameters.getVMParametersList(); commandLine.addParameters(parametersList.getList()); @@ -166,11 +166,8 @@ public class JdkUtil { } } - final Map env = javaParameters.getEnv(); - if (env != null) { - commandLine.setEnvParams(env); - commandLine.setPassParentEnvs(javaParameters.isPassParentEnvs()); - } + commandLine.setEnvParams(javaParameters.getEnv()); + commandLine.setPassParentEnvs(javaParameters.isPassParentEnvs()); final Class commandLineWrapper; if (forceDynamicClasspath && (commandLineWrapper = getCommandLineWrapperClass()) != null) { @@ -215,8 +212,8 @@ public class JdkUtil { final String mainClass = javaParameters.getMainClass(); commandLine.addParameter(mainClass); commandLine.addParameters(javaParameters.getProgramParametersList().getList()); - commandLine.setWorkDirectory(javaParameters.getWorkingDirectory()); + commandLine.setWorkDirectory(javaParameters.getWorkingDirectory()); return commandLine; } diff --git a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java index 562fa027e7b2..73793d44b7ea 100644 --- a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java +++ b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java @@ -347,7 +347,7 @@ public class FileUtil { } @NotNull - public static File createTempDirectory(@NotNull @NonNls String prefix, @NonNls String suffix) throws IOException { + public static File createTempDirectory(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix) throws IOException { File file = doCreateTempFile(prefix, suffix); file.delete(); file.mkdir(); @@ -356,7 +356,7 @@ public class FileUtil { } @NotNull - public static File createTempDirectory(File dir, @NotNull @NonNls String prefix, @NonNls String suffix) throws IOException { + public static File createTempDirectory(File dir, @NotNull @NonNls String prefix, @Nullable @NonNls String suffix) throws IOException { File file = doCreateTempFile(prefix, suffix, dir); file.delete(); file.mkdir(); @@ -365,14 +365,14 @@ public class FileUtil { } @NotNull - public static File createTempFile(@NonNls final File dir, @NotNull @NonNls String prefix, @NonNls String suffix, final boolean create) + public static File createTempFile(@NonNls final File dir, @NotNull @NonNls String prefix, @Nullable @NonNls String suffix, final boolean create) throws IOException { return createTempFile(dir, prefix, suffix, create, true); } public static File createTempFile(@NonNls final File dir, @NotNull @NonNls String prefix, - @NonNls String suffix, + @Nullable @NonNls String suffix, final boolean create, boolean deleteOnExit) throws IOException { File file = doCreateTempFile(prefix, suffix, dir); @@ -387,12 +387,12 @@ public class FileUtil { } @NotNull - public static File createTempFile(@NotNull @NonNls String prefix, @NonNls String suffix) throws IOException { + public static File createTempFile(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix) throws IOException { return createTempFile(prefix, suffix, false); //false until TeamCity fixes its plugin } @NotNull - public static File createTempFile(@NotNull @NonNls String prefix, @NonNls String suffix, boolean deleteOnExit) throws IOException { + public static File createTempFile(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix, boolean deleteOnExit) throws IOException { File file = doCreateTempFile(prefix, suffix); file.delete(); file.createNewFile(); @@ -543,7 +543,7 @@ public class FileUtil { } public static boolean delete(@NotNull File file) { - if (!SymLinkUtil.isSymLink(file)) { + if (file.isDirectory() && !SymLinkUtil.isSymLink(file)) { File[] files = file.listFiles(); if (files != null) { for (File child : files) { diff --git a/platform/util/src/com/intellij/openapi/util/system/ExecUtil.java b/platform/util/src/com/intellij/openapi/util/system/ExecUtil.java index 509b804f0423..d3aecf5db9fc 100644 --- a/platform/util/src/com/intellij/openapi/util/system/ExecUtil.java +++ b/platform/util/src/com/intellij/openapi/util/system/ExecUtil.java @@ -53,17 +53,20 @@ public class ExecUtil { throw new IOException("Template '" + templateName + "' not found by " + loader); } - final StringBuilder template = new StringBuilder(FileUtil.loadTextAndClose(stream)); - if (variables != null) { - for (Map.Entry var : variables.entrySet()) { - final String name = var.getKey(); - final int pos = template.indexOf(name); - if (pos >= 0) { - template.replace(pos, pos + name.length(), var.getValue()); - } + final String template = FileUtil.loadTextAndClose(stream); + if (variables == null || variables.size() == 0) { + return template; + } + + final StringBuilder buffer = new StringBuilder(template); + for (Map.Entry var : variables.entrySet()) { + final String name = var.getKey(); + final int pos = buffer.indexOf(name); + if (pos >= 0) { + buffer.replace(pos, pos + name.length(), var.getValue()); } } - return template.toString(); + return buffer.toString(); } public static File createTempExecutableScript(@NotNull final String prefix,