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 public class JvmProcess
extends SystemCall
implements InitializingBean
{
18 private Properties systemProperties
= new Properties();
19 private List
<Resource
> classpath
= new ArrayList
<Resource
>();
20 private List
<Resource
> pBootClasspath
= new ArrayList
<Resource
>();
21 private Resource jvm
= null;
22 private String mainClass
;
23 private List
<String
> jvmArgs
= new ArrayList
<String
>();
24 private List
<String
> args
= new ArrayList
<String
>();
26 private String systemPropertiesFileProperty
= null;
27 private String systemPropertiesFileDir
= null;
28 private String systemPropertiesFileName
= null;
30 public void afterPropertiesSet() throws Exception
{
31 List
<Object
> command
= new ArrayList
<Object
>();
33 command
.add(asFile(jvm
).getPath());
37 if (pBootClasspath
.size() > 0) {
38 StringBuffer buf
= new StringBuffer("-Xbootclasspath/p:");
40 for (Resource res
: pBootClasspath
) {
44 buf
.append(File
.pathSeparatorChar
);
46 buf
.append(asFile(res
));
48 command
.add(buf
.toString());
51 for (String jvmArg
: jvmArgs
) {
56 StringBuffer buf
= new StringBuffer("");
57 for (Resource res
: classpath
) {
58 if (buf
.length() != 0)
59 buf
.append(File
.pathSeparatorChar
);
60 buf
.append(asFile(res
));
62 command
.add(buf
.toString());
64 if (systemPropertiesFileProperty
== null) {
65 // pass system properties as argument
66 for (Map
.Entry
<Object
, Object
> entry
: systemProperties
.entrySet()) {
67 command
.add("-D" + entry
.getKey() + "=" + entry
.getValue());
70 // write system properties in a file to work around OS limitations
71 // with command line (e.g. Win XP)
72 String dir
= systemPropertiesFileDir
;
74 dir
= getExecDirToUse();
75 String fileName
= systemPropertiesFileName
;
77 fileName
= systemPropertiesFileProperty
+ ".properties";
80 FileOutputStream fos
= null;
81 File file
= new File(dir
+ File
.separator
+ fileName
);
84 if (!file
.getParentFile().exists())
85 file
.getParentFile().mkdirs();
86 fos
= new FileOutputStream(file
);
87 systemProperties
.store(fos
, "Automatically generated by "
89 command
.add("-D" + systemPropertiesFileProperty
+ "="
90 + file
.getCanonicalPath());
91 } catch (IOException e
) {
92 throw new SlcException("Cannot write to system properties to "
95 IOUtils
.closeQuietly(fos
);
100 command
.add(mainClass
);
102 for (String arg
: args
) {
109 protected File
asFile(Resource res
) {
111 return res
.getFile();
112 } catch (FileNotFoundException e
) {
113 return copyToTempFile(res
);
114 } catch (IOException e
) {
115 throw new SlcException("Cannot convert resource to file", e
);
120 protected File
copyToTempFile(Resource res
) {
122 FileOutputStream fos
;
124 tempFile
= File
.createTempFile("slcJvmProcess-", res
.getFilename());
125 tempFile
.deleteOnExit();
126 fos
= new FileOutputStream(tempFile
);
127 IOUtils
.copy(res
.getInputStream(), fos
);
128 } catch (IOException e
) {
129 throw new SlcException("Cannot copy " + res
+ " to temp file.", e
);
131 IOUtils
.closeQuietly(fos
);
135 public Properties
getSystemProperties() {
136 return systemProperties
;
139 public void setSystemProperties(Properties systemProperties
) {
140 this.systemProperties
= systemProperties
;
143 public List
<Resource
> getClasspath() {
147 public void setClasspath(List
<Resource
> classpath
) {
148 this.classpath
= classpath
;
151 public List
<Resource
> getPBootClasspath() {
152 return pBootClasspath
;
155 public void setPBootClasspath(List
<Resource
> bootClasspath
) {
156 pBootClasspath
= bootClasspath
;
159 public Resource
getJvm() {
163 public void setJvm(Resource jvm
) {
167 public String
getMainClass() {
171 public void setMainClass(String mainClass
) {
172 this.mainClass
= mainClass
;
175 public List
<String
> getJvmArgs() {
179 public void setJvmArgs(List
<String
> jvmArgs
) {
180 this.jvmArgs
= jvmArgs
;
183 public List
<String
> getArgs() {
187 public void setArgs(List
<String
> args
) {
191 public void setSystemPropertiesFileProperty(
192 String systemPropertiesFilePropertyName
) {
193 this.systemPropertiesFileProperty
= systemPropertiesFilePropertyName
;
196 public void setSystemPropertiesFileDir(String systemPropertiesFileDir
) {
197 this.systemPropertiesFileDir
= systemPropertiesFileDir
;
200 public void setSystemPropertiesFileName(String systemPropertiesFileName
) {
201 this.systemPropertiesFileName
= systemPropertiesFileName
;