]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - cms/org.argeo.slc.runtime/src/org/argeo/slc/runtime/tasks/JvmProcess.java
Clarify SLC project structure.
[gpl/argeo-slc.git] / cms / org.argeo.slc.runtime / src / org / argeo / slc / runtime / tasks / JvmProcess.java
diff --git a/cms/org.argeo.slc.runtime/src/org/argeo/slc/runtime/tasks/JvmProcess.java b/cms/org.argeo.slc.runtime/src/org/argeo/slc/runtime/tasks/JvmProcess.java
new file mode 100644 (file)
index 0000000..c89f2b8
--- /dev/null
@@ -0,0 +1,230 @@
+package org.argeo.slc.runtime.tasks;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.io.IOUtils;
+import org.argeo.slc.SlcException;
+
+/** A Java Virtual Machine process. */
+public class JvmProcess extends SystemCall {
+       private Properties systemProperties = new Properties();
+       private List<Path> classpath = new ArrayList<Path>();
+       private List<Path> pBootClasspath = new ArrayList<Path>();
+       private Path jvm = null;
+       private String mainClass;
+       private String mainJar;
+       private List<String> jvmArgs = new ArrayList<String>();
+       private List<String> args = new ArrayList<String>();
+
+       private String systemPropertiesFileProperty = null;
+       private String systemPropertiesFileDir = null;
+       private String systemPropertiesFileName = null;
+
+       public void init() throws Exception {
+               List<Object> command = new ArrayList<Object>();
+               if (jvm != null)
+                       command.add(asFile(jvm).getPath());
+               else
+                       command.add("java");
+
+               if (pBootClasspath.size() > 0) {
+                       StringBuffer buf = new StringBuffer("-Xbootclasspath/p:");
+                       Boolean first = true;
+                       for (Path res : pBootClasspath) {
+                               if (first)
+                                       first = false;
+                               else
+                                       buf.append(File.pathSeparatorChar);
+
+                               buf.append(asFile(res));
+                       }
+                       command.add(buf.toString());
+               }
+
+               for (String jvmArg : jvmArgs) {
+                       command.add(jvmArg);
+               }
+
+               if (classpath.size() > 0) {
+                       command.add("-cp");
+                       StringBuffer buf = new StringBuffer("");
+                       for (Path res : classpath) {
+                               if (buf.length() != 0)
+                                       buf.append(File.pathSeparatorChar);
+                               buf.append(asFile(res));
+                       }
+                       command.add(buf.toString());
+               }
+
+               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
+               if (mainClass != null) {
+                       command.add(mainClass);
+               } else if (mainJar != null) {
+                       command.add("-jar");
+                       command.add(mainJar);
+               } else {
+                       throw new SlcException("No main class or jar defined");
+               }
+
+               for (String arg : args) {
+                       command.add(arg);
+               }
+
+               setCommand(command);
+       }
+
+       protected File asFile(Path res) {
+               File file = res.toFile();
+               if (!file.exists())
+                       return copyToTempFile(res);
+               else
+                       return res.toFile();
+       }
+
+       protected File copyToTempFile(Path res) {
+               File tempFile;
+               FileOutputStream fos;
+               try {
+                       tempFile = File.createTempFile("slcJvmProcess-", res.getFileName().toString());
+                       tempFile.deleteOnExit();
+                       fos = new FileOutputStream(tempFile);
+                       IOUtils.copy(Files.newInputStream(res), fos);
+               } catch (IOException e) {
+                       throw new SlcException("Cannot copy " + res + " to temp file.", e);
+               }
+               IOUtils.closeQuietly(fos);
+               return tempFile;
+       }
+
+       /** Append the argument (for chaining) */
+       @Override
+       public SystemCall arg(String arg) {
+               args.add(arg);
+               return this;
+       }
+
+       /** Append the argument (for chaining) */
+       @Override
+       public SystemCall arg(String arg, String value) {
+               args.add(arg);
+               args.add(value);
+               return this;
+       }
+
+       public Properties getSystemProperties() {
+               return systemProperties;
+       }
+
+       public void setSystemProperties(Properties systemProperties) {
+               this.systemProperties = systemProperties;
+       }
+
+       public List<Path> getClasspath() {
+               return classpath;
+       }
+
+       public void setClasspath(List<Path> classpath) {
+               this.classpath = classpath;
+       }
+
+       public List<Path> getPBootClasspath() {
+               return pBootClasspath;
+       }
+
+       public void setPBootClasspath(List<Path> bootClasspath) {
+               pBootClasspath = bootClasspath;
+       }
+
+       public Path getJvm() {
+               return jvm;
+       }
+
+       public void setJvm(Path jvm) {
+               this.jvm = jvm;
+       }
+
+       public String getMainClass() {
+               return mainClass;
+       }
+
+       public void setMainClass(String mainClass) {
+               this.mainClass = mainClass;
+       }
+
+       public String getMainJar() {
+               return mainJar;
+       }
+
+       public void setMainJar(String mainJar) {
+               this.mainJar = mainJar;
+       }
+
+       public List<String> getJvmArgs() {
+               return jvmArgs;
+       }
+
+       public void setJvmArgs(List<String> jvmArgs) {
+               this.jvmArgs = jvmArgs;
+       }
+
+       public List<String> getArgs() {
+               return args;
+       }
+
+       public void setArgs(List<String> args) {
+               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;
+       }
+
+}