]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java
Modular distributions
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / tasks / JvmProcess.java
1 package org.argeo.slc.core.execution.tasks;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Properties;
11
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;
18
19 public class JvmProcess extends SystemCall {
20 private final static Log log = LogFactory.getLog(JvmProcess.class);
21
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>();
29
30 @Override
31 protected CommandLine createCommandLine() {
32 final CommandLine cl;
33 if (jvm != null)
34 cl = new CommandLine(asFile(jvm));
35 else
36 cl = new CommandLine("java");
37
38 if (pBootClasspath.size() > 0) {
39 StringBuffer buf = new StringBuffer("-Xbootclasspath/p:");
40 Boolean first = true;
41 for (Resource res : pBootClasspath) {
42 if (first)
43 first = false;
44 else
45 buf.append(File.pathSeparatorChar);
46
47 buf.append(asFile(res));
48 }
49 cl.addArgument(buf.toString());
50 }
51
52 for (String jvmArg : jvmArgs) {
53 cl.addArgument(jvmArg);
54 }
55
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));
62 }
63 cl.addArgument(buf.toString());
64
65 for (Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
66 cl.addArgument("-D" + entry.getKey() + "=" + entry.getValue());
67 }
68
69 // Program
70 cl.addArgument(mainClass);
71
72 for (String arg : args) {
73 cl.addArgument(arg);
74 }
75
76 if (log.isDebugEnabled())
77 log.debug("Command line:\n" + cl.toString() + "\n");
78
79 return cl;
80 }
81
82 protected File asFile(Resource res) {
83 try {
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);
89 }
90
91 }
92
93 protected File copyToTempFile(Resource res) {
94 File tempFile;
95 FileOutputStream fos;
96 try {
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);
103 }
104 IOUtils.closeQuietly(fos);
105 return tempFile;
106 }
107
108 public Properties getSystemProperties() {
109 return systemProperties;
110 }
111
112 public void setSystemProperties(Properties systemProperties) {
113 this.systemProperties = systemProperties;
114 }
115
116 public List<Resource> getClasspath() {
117 return classpath;
118 }
119
120 public void setClasspath(List<Resource> classpath) {
121 this.classpath = classpath;
122 }
123
124 public List<Resource> getPBootClasspath() {
125 return pBootClasspath;
126 }
127
128 public void setPBootClasspath(List<Resource> bootClasspath) {
129 pBootClasspath = bootClasspath;
130 }
131
132 public Resource getJvm() {
133 return jvm;
134 }
135
136 public void setJvm(Resource jvm) {
137 this.jvm = jvm;
138 }
139
140 public String getMainClass() {
141 return mainClass;
142 }
143
144 public void setMainClass(String mainClass) {
145 this.mainClass = mainClass;
146 }
147
148 public List<String> getJvmArgs() {
149 return jvmArgs;
150 }
151
152 public void setJvmArgs(List<String> jvmArgs) {
153 this.jvmArgs = jvmArgs;
154 }
155
156 public List<String> getArgs() {
157 return args;
158 }
159
160 public void setArgs(List<String> args) {
161 this.args = args;
162 }
163
164 }