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