hide vm options for special cases only, e.g. due to class loader problems (IDEA-84541)

This commit is contained in:
anna
2012-04-14 13:43:01 +02:00
parent a37c58c745
commit 1effe9cca2
2 changed files with 83 additions and 54 deletions

View File

@@ -39,7 +39,8 @@ public class SimpleJavaParameters extends SimpleProgramParameters {
private final ParametersList myVmParameters = new ParametersList();
private Charset myCharset = CharsetToolkit.getDefaultSystemCharset();
private boolean myUseDynamicClasspath;
private boolean myUseDynamicVMOptions;
public String getMainClass() {
return myMainClass;
}
@@ -83,6 +84,14 @@ public class SimpleJavaParameters extends SimpleProgramParameters {
myUseDynamicClasspath = useDynamicClasspath;
}
public void setUseDynamicVMOptions(boolean useDynamicVMOptions) {
myUseDynamicVMOptions = useDynamicVMOptions;
}
public boolean isDynamicVMOptions() {
return myUseDynamicVMOptions;
}
public OSProcessHandler createOSProcessHandler() throws ExecutionException {
final Sdk sdk = getJdk();
assert sdk != null : "SDK should be defined";

View File

@@ -161,46 +161,20 @@ public class JdkUtil {
final GeneralCommandLine commandLine = new GeneralCommandLine();
commandLine.setExePath(exePath);
ParametersList parametersList = javaParameters.getVMParametersList();
final ParametersList vmParametersList = javaParameters.getVMParametersList();
commandLine.setEnvParams(javaParameters.getEnv());
commandLine.setPassParentEnvs(javaParameters.isPassParentEnvs());
final Class commandLineWrapper;
if (forceDynamicClasspath && (commandLineWrapper = getCommandLineWrapperClass()) != null) {
File classpathFile = null;
File vmParamsFile = null;
if(!parametersList.hasParameter("-classpath") && !parametersList.hasParameter("-cp")){
try {
classpathFile = FileUtil.createTempFile("classpath", null);
final PrintWriter writer = new PrintWriter(classpathFile);
try {
for (String path : javaParameters.getClassPath().getPathList()) {
writer.println(path);
}
}
finally {
writer.close();
}
if ((commandLineWrapper = getCommandLineWrapperClass()) != null) {
String classpath = PathUtil.getJarPathForClass(commandLineWrapper);
final Class<UrlClassLoader> ourUrlClassLoader = UrlClassLoader.class;
if (ourUrlClassLoader.getName().equals(parametersList.getPropertyValue("java.system.class.loader"))) {
classpath += File.pathSeparator + PathUtil.getJarPathForClass(ourUrlClassLoader);
classpath += File.pathSeparator + PathUtil.getJarPathForClass(THashMap.class);
}
commandLine.addParameter("-classpath");
commandLine.addParameter(classpath);
}
catch (IOException e) {
LOG.error(e);
}
if (javaParameters.isDynamicVMOptions() && useDynamicVMOptions()) {
File vmParamsFile = null;
try {
vmParamsFile = FileUtil.createTempFile("vm_params", null);
final PrintWriter writer = new PrintWriter(vmParamsFile);
try {
for (String param : parametersList.getList()) {
for (String param : vmParametersList.getList()) {
if (param.startsWith("-D")) {
writer.println(param);
}
@@ -213,37 +187,62 @@ public class JdkUtil {
catch (IOException e) {
LOG.error(e);
}
}
final List<String> list = parametersList.getList();
if (vmParamsFile == null) {
commandLine.addParameters(list);
} else {
final List<String> list = vmParametersList.getList();
for (String param : list) {
if (!param.trim().startsWith("-D")) {
commandLine.addParameter(param);
}
}
}
appendEncoding(javaParameters, commandLine, parametersList);
if (classpathFile != null) {
commandLine.addParameter(commandLineWrapper.getName());
commandLine.addParameter(classpathFile.getAbsolutePath());
}
if (vmParamsFile != null) {
commandLine.addParameter("@vm_params");
commandLine.addParameter(vmParamsFile.getAbsolutePath());
}
}
else if (!parametersList.hasParameter("-classpath") && !parametersList.hasParameter("-cp")){
commandLine.addParameters(parametersList.getList());
appendEncoding(javaParameters, commandLine, parametersList);
else {
commandLine.addParameters(vmParametersList.getList());
}
commandLine.addParameter("-classpath");
commandLine.addParameter(javaParameters.getClassPath().getPathsString());
} else {
commandLine.addParameters(parametersList.getList());
appendEncoding(javaParameters, commandLine, parametersList);
if (forceDynamicClasspath) {
File classpathFile = null;
if (!vmParametersList.hasParameter("-classpath") && !vmParametersList.hasParameter("-cp")) {
try {
classpathFile = FileUtil.createTempFile("classpath", null);
final PrintWriter writer = new PrintWriter(classpathFile);
try {
for (String path : javaParameters.getClassPath().getPathList()) {
writer.println(path);
}
}
finally {
writer.close();
}
String classpath = PathUtil.getJarPathForClass(commandLineWrapper);
final Class<UrlClassLoader> ourUrlClassLoader = UrlClassLoader.class;
if (ourUrlClassLoader.getName().equals(vmParametersList.getPropertyValue("java.system.class.loader"))) {
classpath += File.pathSeparator + PathUtil.getJarPathForClass(ourUrlClassLoader);
classpath += File.pathSeparator + PathUtil.getJarPathForClass(THashMap.class);
}
commandLine.addParameter("-classpath");
commandLine.addParameter(classpath);
}
catch (IOException e) {
LOG.error(e);
}
}
appendEncoding(javaParameters, commandLine, vmParametersList);
if (classpathFile != null) {
commandLine.addParameter(commandLineWrapper.getName());
commandLine.addParameter(classpathFile.getAbsolutePath());
}
}
else {
appendEncodingClasspath(javaParameters, commandLine, vmParametersList);
}
}
else {
appendParamsEncodingClasspath(javaParameters, commandLine, vmParametersList);
}
final String mainClass = javaParameters.getMainClass();
@@ -255,6 +254,23 @@ public class JdkUtil {
return commandLine;
}
private static void appendParamsEncodingClasspath(SimpleJavaParameters javaParameters,
GeneralCommandLine commandLine,
ParametersList parametersList) {
commandLine.addParameters(parametersList.getList());
appendEncodingClasspath(javaParameters, commandLine, parametersList);
}
private static void appendEncodingClasspath(SimpleJavaParameters javaParameters,
GeneralCommandLine commandLine,
ParametersList parametersList) {
appendEncoding(javaParameters, commandLine, parametersList);
if (!parametersList.hasParameter("-classpath") && !parametersList.hasParameter("-cp")){
commandLine.addParameter("-classpath");
commandLine.addParameter(javaParameters.getClassPath().getPathsString());
}
}
private static void appendEncoding(SimpleJavaParameters javaParameters, GeneralCommandLine commandLine, ParametersList parametersList) {
// Value of -Dfile.encoding and charset of GeneralCommandLine should be in sync in order process's input and output be correctly handled.
String encoding = parametersList.getPropertyValue("file.encoding");
@@ -291,4 +307,8 @@ public class JdkUtil {
? PropertiesComponent.getInstance(project).getOrInit("dynamic.classpath", hasDynamicProperty)
: hasDynamicProperty).booleanValue();
}
public static boolean useDynamicVMOptions() {
return Boolean.valueOf(PropertiesComponent.getInstance().getOrInit("dynamic.vmoptions", "true")).booleanValue();
}
}