]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java
SystemCall, JvmProcess, DetachedLauncher enhancements
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / tasks / JvmProcess.java
index edc7188b1a30d150e93c44ca228f201dcc50ccad..1dc91bf5d59558af6dde216a5878744cd5ffbfb2 100644 (file)
@@ -9,16 +9,12 @@ 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.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
+import org.springframework.beans.factory.InitializingBean;
 import org.springframework.core.io.Resource;
 
-public class JvmProcess extends SystemCall {
-       private final static Log log = LogFactory.getLog(JvmProcess.class);
-
+public class JvmProcess extends SystemCall implements InitializingBean {
        private Properties systemProperties = new Properties();
        private List<Resource> classpath = new ArrayList<Resource>();
        private List<Resource> pBootClasspath = new ArrayList<Resource>();
@@ -27,13 +23,16 @@ public class JvmProcess extends SystemCall {
        private List<String> jvmArgs = new ArrayList<String>();
        private List<String> args = new ArrayList<String>();
 
-       @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<Object> command = new ArrayList<Object>();
                if (jvm != null)
-                       cl = new CommandLine(asFile(jvm));
+                       command.add(asFile(jvm).getAbsolutePath());
                else
-                       cl = new CommandLine("java");
+                       command.add("java");
 
                if (pBootClasspath.size() > 0) {
                        StringBuffer buf = new StringBuffer("-Xbootclasspath/p:");
@@ -46,37 +45,65 @@ public class JvmProcess extends SystemCall {
 
                                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<Object, Object> entry : systemProperties.entrySet()) {
-                       cl.addArgument("-D" + entry.getKey() + "=" + entry.getValue());
+               if (systemPropertiesFileProperty == null) {
+                       // pass system properties as argument
+                       for (Map.Entry<Object, Object> 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);
                }
 
-               if (log.isTraceEnabled())
-                       log.debug("Command line:\n" + cl.toString() + "\n");
-
-               return cl;
+               setCommand(command);
        }
 
        protected File asFile(Resource res) {
@@ -161,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;
+       }
+
 }