]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.runtime/src/org/argeo/slc/runtime/tasks/JvmProcess.java
Merge remote-tracking branch 'origin/unstable' into testing
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / tasks / JvmProcess.java
1 package org.argeo.slc.runtime.tasks;
2
3 import java.io.File;
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;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Properties;
12
13 import org.apache.commons.io.IOUtils;
14 import org.argeo.slc.SlcException;
15
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>();
26
27 private String systemPropertiesFileProperty = null;
28 private String systemPropertiesFileDir = null;
29 private String systemPropertiesFileName = null;
30
31 public void init() throws Exception {
32 List<Object> command = new ArrayList<Object>();
33 if (jvm != null)
34 command.add(asFile(jvm).getPath());
35 else
36 command.add("java");
37
38 if (pBootClasspath.size() > 0) {
39 StringBuffer buf = new StringBuffer("-Xbootclasspath/p:");
40 Boolean first = true;
41 for (Path res : pBootClasspath) {
42 if (first)
43 first = false;
44 else
45 buf.append(File.pathSeparatorChar);
46
47 buf.append(asFile(res));
48 }
49 command.add(buf.toString());
50 }
51
52 for (String jvmArg : jvmArgs) {
53 command.add(jvmArg);
54 }
55
56 if (classpath.size() > 0) {
57 command.add("-cp");
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));
63 }
64 command.add(buf.toString());
65 }
66
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());
71 }
72 } else {
73 // write system properties in a file to work around OS limitations
74 // with command line (e.g. Win XP)
75 String dir = systemPropertiesFileDir;
76 if (dir == null)
77 dir = getExecDirToUse();
78 String fileName = systemPropertiesFileName;
79 if (fileName == null)
80 fileName = systemPropertiesFileProperty + ".properties";
81
82 // Write file
83 FileOutputStream fos = null;
84 File file = new File(dir + File.separator + fileName);
85 try {
86
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);
94 } finally {
95 IOUtils.closeQuietly(fos);
96 }
97 }
98
99 // Program
100 if (mainClass != null) {
101 command.add(mainClass);
102 } else if (mainJar != null) {
103 command.add("-jar");
104 command.add(mainJar);
105 } else {
106 throw new SlcException("No main class or jar defined");
107 }
108
109 for (String arg : args) {
110 command.add(arg);
111 }
112
113 setCommand(command);
114 }
115
116 protected File asFile(Path res) {
117 File file = res.toFile();
118 if (!file.exists())
119 return copyToTempFile(res);
120 else
121 return res.toFile();
122 }
123
124 protected File copyToTempFile(Path res) {
125 File tempFile;
126 FileOutputStream fos;
127 try {
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);
134 }
135 IOUtils.closeQuietly(fos);
136 return tempFile;
137 }
138
139 /** Append the argument (for chaining) */
140 @Override
141 public SystemCall arg(String arg) {
142 args.add(arg);
143 return this;
144 }
145
146 /** Append the argument (for chaining) */
147 @Override
148 public SystemCall arg(String arg, String value) {
149 args.add(arg);
150 args.add(value);
151 return this;
152 }
153
154 public Properties getSystemProperties() {
155 return systemProperties;
156 }
157
158 public void setSystemProperties(Properties systemProperties) {
159 this.systemProperties = systemProperties;
160 }
161
162 public List<Path> getClasspath() {
163 return classpath;
164 }
165
166 public void setClasspath(List<Path> classpath) {
167 this.classpath = classpath;
168 }
169
170 public List<Path> getPBootClasspath() {
171 return pBootClasspath;
172 }
173
174 public void setPBootClasspath(List<Path> bootClasspath) {
175 pBootClasspath = bootClasspath;
176 }
177
178 public Path getJvm() {
179 return jvm;
180 }
181
182 public void setJvm(Path jvm) {
183 this.jvm = jvm;
184 }
185
186 public String getMainClass() {
187 return mainClass;
188 }
189
190 public void setMainClass(String mainClass) {
191 this.mainClass = mainClass;
192 }
193
194 public String getMainJar() {
195 return mainJar;
196 }
197
198 public void setMainJar(String mainJar) {
199 this.mainJar = mainJar;
200 }
201
202 public List<String> getJvmArgs() {
203 return jvmArgs;
204 }
205
206 public void setJvmArgs(List<String> jvmArgs) {
207 this.jvmArgs = jvmArgs;
208 }
209
210 public List<String> getArgs() {
211 return args;
212 }
213
214 public void setArgs(List<String> args) {
215 this.args = args;
216 }
217
218 public void setSystemPropertiesFileProperty(String systemPropertiesFilePropertyName) {
219 this.systemPropertiesFileProperty = systemPropertiesFilePropertyName;
220 }
221
222 public void setSystemPropertiesFileDir(String systemPropertiesFileDir) {
223 this.systemPropertiesFileDir = systemPropertiesFileDir;
224 }
225
226 public void setSystemPropertiesFileName(String systemPropertiesFileName) {
227 this.systemPropertiesFileName = systemPropertiesFileName;
228 }
229
230 }