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
.exec
.CommandLine
;
13 import org
.apache
.commons
.io
.IOUtils
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.slc
.SlcException
;
17 import org
.springframework
.core
.io
.Resource
;
19 public class JvmProcess
extends SystemCall
{
20 private final static Log log
= LogFactory
.getLog(JvmProcess
.class);
22 private Properties systemProperties
= new Properties();
23 private List
<Resource
> classpath
= new ArrayList
<Resource
>();
24 private List
<Resource
> pBootClasspath
= new ArrayList
<Resource
>();
25 private Resource jvm
= null;
26 private String mainClass
;
27 private List
<String
> jvmArgs
= new ArrayList
<String
>();
28 private List
<String
> args
= new ArrayList
<String
>();
31 protected CommandLine
createCommandLine() {
34 cl
= new CommandLine(asFile(jvm
));
36 cl
= new CommandLine("java");
38 if (pBootClasspath
.size() > 0) {
39 StringBuffer buf
= new StringBuffer("-Xbootclasspath/p:");
41 for (Resource res
: pBootClasspath
) {
45 buf
.append(File
.pathSeparatorChar
);
47 buf
.append(asFile(res
));
49 cl
.addArgument(buf
.toString());
52 for (String jvmArg
: jvmArgs
) {
53 cl
.addArgument(jvmArg
);
56 cl
.addArgument("-cp");
57 StringBuffer buf
= new StringBuffer("");
58 for (Resource res
: classpath
) {
59 if (buf
.length() != 0)
60 buf
.append(File
.pathSeparatorChar
);
61 buf
.append(asFile(res
));
63 cl
.addArgument(buf
.toString());
65 for (Map
.Entry
<Object
, Object
> entry
: systemProperties
.entrySet()) {
66 cl
.addArgument("-D" + entry
.getKey() + "=" + entry
.getValue());
70 cl
.addArgument(mainClass
);
72 for (String arg
: args
) {
76 if (log
.isDebugEnabled())
77 log
.debug("Command line:\n" + cl
.toString() + "\n");
82 protected File
asFile(Resource res
) {
84 return res
.getFile().getCanonicalFile();
85 } catch (FileNotFoundException e
) {
86 return copyToTempFile(res
);
87 } catch (IOException e
) {
88 throw new SlcException("Cannot convert resource to file", e
);
93 protected File
copyToTempFile(Resource res
) {
97 tempFile
= File
.createTempFile("slcJvmProcess-", res
.getFilename());
98 tempFile
.deleteOnExit();
99 fos
= new FileOutputStream(tempFile
);
100 IOUtils
.copy(res
.getInputStream(), fos
);
101 } catch (IOException e
) {
102 throw new SlcException("Cannot copy " + res
+ " to temp file.", e
);
104 IOUtils
.closeQuietly(fos
);
108 public Properties
getSystemProperties() {
109 return systemProperties
;
112 public void setSystemProperties(Properties systemProperties
) {
113 this.systemProperties
= systemProperties
;
116 public List
<Resource
> getClasspath() {
120 public void setClasspath(List
<Resource
> classpath
) {
121 this.classpath
= classpath
;
124 public List
<Resource
> getPBootClasspath() {
125 return pBootClasspath
;
128 public void setPBootClasspath(List
<Resource
> bootClasspath
) {
129 pBootClasspath
= bootClasspath
;
132 public Resource
getJvm() {
136 public void setJvm(Resource jvm
) {
140 public String
getMainClass() {
144 public void setMainClass(String mainClass
) {
145 this.mainClass
= mainClass
;
148 public List
<String
> getJvmArgs() {
152 public void setJvmArgs(List
<String
> jvmArgs
) {
153 this.jvmArgs
= jvmArgs
;
156 public List
<String
> getArgs() {
160 public void setArgs(List
<String
> args
) {