1 package org
.argeo
.slc
.runtime
.tasks
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.IOException
;
6 import java
.nio
.file
.Files
;
7 import java
.nio
.file
.Path
;
8 import java
.util
.ArrayList
;
11 import java
.util
.Properties
;
13 import org
.apache
.commons
.io
.IOUtils
;
14 import org
.argeo
.api
.slc
.SlcException
;
16 /** A Java Virtual Machine process. */
17 public class JvmProcess
extends SystemCall
{
18 private Properties systemProperties
= new Properties();
19 private List
<Path
> classpath
= new ArrayList
<Path
>();
20 private List
<Path
> pBootClasspath
= new ArrayList
<Path
>();
21 private Path jvm
= null;
22 private String mainClass
;
23 private String mainJar
;
24 private List
<String
> jvmArgs
= new ArrayList
<String
>();
25 private List
<String
> args
= new ArrayList
<String
>();
27 private String systemPropertiesFileProperty
= null;
28 private String systemPropertiesFileDir
= null;
29 private String systemPropertiesFileName
= null;
31 public void init() throws Exception
{
32 List
<Object
> command
= new ArrayList
<Object
>();
34 command
.add(asFile(jvm
).getPath());
38 if (pBootClasspath
.size() > 0) {
39 StringBuffer buf
= new StringBuffer("-Xbootclasspath/p:");
41 for (Path res
: pBootClasspath
) {
45 buf
.append(File
.pathSeparatorChar
);
47 buf
.append(asFile(res
));
49 command
.add(buf
.toString());
52 for (String jvmArg
: jvmArgs
) {
56 if (classpath
.size() > 0) {
58 StringBuffer buf
= new StringBuffer("");
59 for (Path res
: classpath
) {
60 if (buf
.length() != 0)
61 buf
.append(File
.pathSeparatorChar
);
62 buf
.append(asFile(res
));
64 command
.add(buf
.toString());
67 if (systemPropertiesFileProperty
== null) {
68 // pass system properties as argument
69 for (Map
.Entry
<Object
, Object
> entry
: systemProperties
.entrySet()) {
70 command
.add("-D" + entry
.getKey() + "=" + entry
.getValue());
73 // write system properties in a file to work around OS limitations
74 // with command line (e.g. Win XP)
75 String dir
= systemPropertiesFileDir
;
77 dir
= getExecDirToUse();
78 String fileName
= systemPropertiesFileName
;
80 fileName
= systemPropertiesFileProperty
+ ".properties";
83 FileOutputStream fos
= null;
84 File file
= new File(dir
+ File
.separator
+ fileName
);
87 if (!file
.getParentFile().exists())
88 file
.getParentFile().mkdirs();
89 fos
= new FileOutputStream(file
);
90 systemProperties
.store(fos
, "Automatically generated by " + getClass());
91 command
.add("-D" + systemPropertiesFileProperty
+ "=" + file
.getCanonicalPath());
92 } catch (IOException e
) {
93 throw new SlcException("Cannot write to system properties to " + file
, e
);
95 IOUtils
.closeQuietly(fos
);
100 if (mainClass
!= null) {
101 command
.add(mainClass
);
102 } else if (mainJar
!= null) {
104 command
.add(mainJar
);
106 throw new SlcException("No main class or jar defined");
109 for (String arg
: args
) {
116 protected File
asFile(Path res
) {
117 File file
= res
.toFile();
119 return copyToTempFile(res
);
124 protected File
copyToTempFile(Path res
) {
126 FileOutputStream fos
;
128 tempFile
= File
.createTempFile("slcJvmProcess-", res
.getFileName().toString());
129 tempFile
.deleteOnExit();
130 fos
= new FileOutputStream(tempFile
);
131 IOUtils
.copy(Files
.newInputStream(res
), fos
);
132 } catch (IOException e
) {
133 throw new SlcException("Cannot copy " + res
+ " to temp file.", e
);
135 IOUtils
.closeQuietly(fos
);
139 /** Append the argument (for chaining) */
141 public SystemCall
arg(String arg
) {
146 /** Append the argument (for chaining) */
148 public SystemCall
arg(String arg
, String value
) {
154 public Properties
getSystemProperties() {
155 return systemProperties
;
158 public void setSystemProperties(Properties systemProperties
) {
159 this.systemProperties
= systemProperties
;
162 public List
<Path
> getClasspath() {
166 public void setClasspath(List
<Path
> classpath
) {
167 this.classpath
= classpath
;
170 public List
<Path
> getPBootClasspath() {
171 return pBootClasspath
;
174 public void setPBootClasspath(List
<Path
> bootClasspath
) {
175 pBootClasspath
= bootClasspath
;
178 public Path
getJvm() {
182 public void setJvm(Path jvm
) {
186 public String
getMainClass() {
190 public void setMainClass(String mainClass
) {
191 this.mainClass
= mainClass
;
194 public String
getMainJar() {
198 public void setMainJar(String mainJar
) {
199 this.mainJar
= mainJar
;
202 public List
<String
> getJvmArgs() {
206 public void setJvmArgs(List
<String
> jvmArgs
) {
207 this.jvmArgs
= jvmArgs
;
210 public List
<String
> getArgs() {
214 public void setArgs(List
<String
> args
) {
218 public void setSystemPropertiesFileProperty(String systemPropertiesFilePropertyName
) {
219 this.systemPropertiesFileProperty
= systemPropertiesFilePropertyName
;
222 public void setSystemPropertiesFileDir(String systemPropertiesFileDir
) {
223 this.systemPropertiesFileDir
= systemPropertiesFileDir
;
226 public void setSystemPropertiesFileName(String systemPropertiesFileName
) {
227 this.systemPropertiesFileName
= systemPropertiesFileName
;