X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2Ftasks%2FJvmProcess.java;h=be986f777cbe3695a67ee5d794107b50f1a50ddd;hb=dad97a87545102becea1beb153baa677a2dcd4d8;hp=ba1d67eb558ac0296e46d9dba4ddbb0ec7a979b6;hpb=09ab1aca27488e1feef6c8f46b34b7d27284be9a;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java index ba1d67eb5..be986f777 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java @@ -1,17 +1,20 @@ package org.argeo.slc.core.execution.tasks; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Properties; -import org.apache.commons.exec.CommandLine; +import org.apache.commons.io.IOUtils; import org.argeo.slc.SlcException; +import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; -public class JvmProcess extends SystemCall { +public class JvmProcess extends SystemCall implements InitializingBean { private Properties systemProperties = new Properties(); private List classpath = new ArrayList(); private List pBootClasspath = new ArrayList(); @@ -20,61 +23,115 @@ public class JvmProcess extends SystemCall { private List jvmArgs = new ArrayList(); private List args = new ArrayList(); - @Override - protected CommandLine createCommandLine() { - final CommandLine cl; + private String systemPropertiesFileProperty = null; + private String systemPropertiesFileDir = null; + private String systemPropertiesFileName = null; + + public void afterPropertiesSet() throws Exception { + List command = new ArrayList(); if (jvm != null) - cl = new CommandLine(asFile(jvm)); + command.add(asFile(jvm).getPath()); else - cl = new CommandLine("java"); + command.add("java"); if (pBootClasspath.size() > 0) { StringBuffer buf = new StringBuffer("-Xbootclasspath/p:"); + Boolean first = true; for (Resource res : pBootClasspath) { - if (buf.length() != 0) + if (first) + first = false; + else buf.append(File.pathSeparatorChar); + buf.append(asFile(res)); } - cl.addArgument(buf.toString()); + command.add(buf.toString()); } for (String jvmArg : jvmArgs) { - cl.addArgument(jvmArg); + command.add(jvmArg); } - cl.addArgument("-cp"); + command.add("-cp"); StringBuffer buf = new StringBuffer(""); for (Resource res : classpath) { if (buf.length() != 0) buf.append(File.pathSeparatorChar); buf.append(asFile(res)); } - cl.addArgument(buf.toString()); + command.add(buf.toString()); - for (Map.Entry entry : systemProperties.entrySet()) { - cl.addArgument("-D" + entry.getKey() + "=" + entry.getValue()); + if (systemPropertiesFileProperty == null) { + // pass system properties as argument + for (Map.Entry entry : systemProperties.entrySet()) { + command.add("-D" + entry.getKey() + "=" + entry.getValue()); + } + } else { + // write system properties in a file to work around OS limitations + // with command line (e.g. Win XP) + String dir = systemPropertiesFileDir; + if (dir == null) + dir = getExecDirToUse(); + String fileName = systemPropertiesFileName; + if (fileName == null) + fileName = systemPropertiesFileProperty + ".properties"; + + // Write file + FileOutputStream fos = null; + File file = new File(dir + File.separator + fileName); + try { + + if (!file.getParentFile().exists()) + file.getParentFile().mkdirs(); + fos = new FileOutputStream(file); + systemProperties.store(fos, "Automatically generated by " + + getClass()); + command.add("-D" + systemPropertiesFileProperty + "=" + + file.getCanonicalPath()); + } catch (IOException e) { + throw new SlcException("Cannot write to system properties to " + + file, e); + } finally { + IOUtils.closeQuietly(fos); + } } // Program - cl.addArgument(mainClass); + command.add(mainClass); for (String arg : args) { - cl.addArgument(arg); + command.add(arg); } - return cl; + setCommand(command); } protected File asFile(Resource res) { - // TODO: implements local copy try { - return res.getFile().getCanonicalFile(); + return res.getFile(); + } catch (FileNotFoundException e) { + return copyToTempFile(res); } catch (IOException e) { throw new SlcException("Cannot convert resource to file", e); } } + protected File copyToTempFile(Resource res) { + File tempFile; + FileOutputStream fos; + try { + tempFile = File.createTempFile("slcJvmProcess-", res.getFilename()); + tempFile.deleteOnExit(); + fos = new FileOutputStream(tempFile); + IOUtils.copy(res.getInputStream(), fos); + } catch (IOException e) { + throw new SlcException("Cannot copy " + res + " to temp file.", e); + } + IOUtils.closeQuietly(fos); + return tempFile; + } + public Properties getSystemProperties() { return systemProperties; } @@ -131,4 +188,17 @@ public class JvmProcess extends SystemCall { this.args = args; } + public void setSystemPropertiesFileProperty( + String systemPropertiesFilePropertyName) { + this.systemPropertiesFileProperty = systemPropertiesFilePropertyName; + } + + public void setSystemPropertiesFileDir(String systemPropertiesFileDir) { + this.systemPropertiesFileDir = systemPropertiesFileDir; + } + + public void setSystemPropertiesFileName(String systemPropertiesFileName) { + this.systemPropertiesFileName = systemPropertiesFileName; + } + }