1 package org
.argeo
.slc
.core
.execution
.tasks
;
4 import java
.io
.FileNotFoundException
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.util
.ArrayList
;
10 import java
.util
.Properties
;
12 import org
.apache
.commons
.io
.IOUtils
;
13 import org
.argeo
.slc
.SlcException
;
14 import org
.springframework
.beans
.factory
.InitializingBean
;
15 import org
.springframework
.core
.io
.Resource
;
17 /** A Java Virtual Machine process. */
18 public class JvmProcess
extends SystemCall
implements InitializingBean
{
19 private Properties systemProperties
= new Properties();
20 private List
<Resource
> classpath
= new ArrayList
<Resource
>();
21 private List
<Resource
> pBootClasspath
= new ArrayList
<Resource
>();
22 private Resource jvm
= null;
23 private String mainClass
;
24 private String mainJar
;
25 private List
<String
> jvmArgs
= new ArrayList
<String
>();
26 private List
<String
> args
= new ArrayList
<String
>();
28 private String systemPropertiesFileProperty
= null;
29 private String systemPropertiesFileDir
= null;
30 private String systemPropertiesFileName
= null;
32 public void afterPropertiesSet() throws Exception
{
33 List
<Object
> command
= new ArrayList
<Object
>();
35 command
.add(asFile(jvm
).getPath());
39 if (pBootClasspath
.size() > 0) {
40 StringBuffer buf
= new StringBuffer("-Xbootclasspath/p:");
42 for (Resource res
: pBootClasspath
) {
46 buf
.append(File
.pathSeparatorChar
);
48 buf
.append(asFile(res
));
50 command
.add(buf
.toString());
53 for (String jvmArg
: jvmArgs
) {
57 if (classpath
.size() > 0) {
59 StringBuffer buf
= new StringBuffer("");
60 for (Resource res
: classpath
) {
61 if (buf
.length() != 0)
62 buf
.append(File
.pathSeparatorChar
);
63 buf
.append(asFile(res
));
65 command
.add(buf
.toString());
68 if (systemPropertiesFileProperty
== null) {
69 // pass system properties as argument
70 for (Map
.Entry
<Object
, Object
> entry
: systemProperties
.entrySet()) {
71 command
.add("-D" + entry
.getKey() + "=" + entry
.getValue());
74 // write system properties in a file to work around OS limitations
75 // with command line (e.g. Win XP)
76 String dir
= systemPropertiesFileDir
;
78 dir
= getExecDirToUse();
79 String fileName
= systemPropertiesFileName
;
81 fileName
= systemPropertiesFileProperty
+ ".properties";
84 FileOutputStream fos
= null;
85 File file
= new File(dir
+ File
.separator
+ fileName
);
88 if (!file
.getParentFile().exists())
89 file
.getParentFile().mkdirs();
90 fos
= new FileOutputStream(file
);
91 systemProperties
.store(fos
, "Automatically generated by "
93 command
.add("-D" + systemPropertiesFileProperty
+ "="
94 + file
.getCanonicalPath());
95 } catch (IOException e
) {
96 throw new SlcException("Cannot write to system properties to "
99 IOUtils
.closeQuietly(fos
);
104 if (mainClass
!= null) {
105 command
.add(mainClass
);
106 } else if (mainJar
!= null) {
108 command
.add(mainJar
);
110 throw new SlcException("No main class or jar defined");
113 for (String arg
: args
) {
120 protected File
asFile(Resource res
) {
122 return res
.getFile();
123 } catch (FileNotFoundException e
) {
124 return copyToTempFile(res
);
125 } catch (IOException e
) {
126 throw new SlcException("Cannot convert resource to file", e
);
131 protected File
copyToTempFile(Resource res
) {
133 FileOutputStream fos
;
135 tempFile
= File
.createTempFile("slcJvmProcess-", res
.getFilename());
136 tempFile
.deleteOnExit();
137 fos
= new FileOutputStream(tempFile
);
138 IOUtils
.copy(res
.getInputStream(), fos
);
139 } catch (IOException e
) {
140 throw new SlcException("Cannot copy " + res
+ " to temp file.", e
);
142 IOUtils
.closeQuietly(fos
);
146 /** Append the argument (for chaining) */
148 public SystemCall
arg(String arg
) {
153 /** Append the argument (for chaining) */
155 public SystemCall
arg(String arg
, String value
) {
161 public Properties
getSystemProperties() {
162 return systemProperties
;
165 public void setSystemProperties(Properties systemProperties
) {
166 this.systemProperties
= systemProperties
;
169 public List
<Resource
> getClasspath() {
173 public void setClasspath(List
<Resource
> classpath
) {
174 this.classpath
= classpath
;
177 public List
<Resource
> getPBootClasspath() {
178 return pBootClasspath
;
181 public void setPBootClasspath(List
<Resource
> bootClasspath
) {
182 pBootClasspath
= bootClasspath
;
185 public Resource
getJvm() {
189 public void setJvm(Resource jvm
) {
193 public String
getMainClass() {
197 public void setMainClass(String mainClass
) {
198 this.mainClass
= mainClass
;
201 public String
getMainJar() {
205 public void setMainJar(String mainJar
) {
206 this.mainJar
= mainJar
;
209 public List
<String
> getJvmArgs() {
213 public void setJvmArgs(List
<String
> jvmArgs
) {
214 this.jvmArgs
= jvmArgs
;
217 public List
<String
> getArgs() {
221 public void setArgs(List
<String
> args
) {
225 public void setSystemPropertiesFileProperty(
226 String systemPropertiesFilePropertyName
) {
227 this.systemPropertiesFileProperty
= systemPropertiesFilePropertyName
;
230 public void setSystemPropertiesFileDir(String systemPropertiesFileDir
) {
231 this.systemPropertiesFileDir
= systemPropertiesFileDir
;
234 public void setSystemPropertiesFileName(String systemPropertiesFileName
) {
235 this.systemPropertiesFileName
= systemPropertiesFileName
;