]> 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
Add generate script
[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.io.IOUtils;
13 import org.argeo.slc.SlcException;
14 import org.springframework.beans.factory.InitializingBean;
15 import org.springframework.core.io.Resource;
16
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>();
25
26 private String systemPropertiesFileProperty = null;
27 private String systemPropertiesFileDir = null;
28 private String systemPropertiesFileName = null;
29
30 public void afterPropertiesSet() throws Exception {
31 List<Object> command = new ArrayList<Object>();
32 if (jvm != null)
33 command.add(asFile(jvm).getPath());
34 else
35 command.add("java");
36
37 if (pBootClasspath.size() > 0) {
38 StringBuffer buf = new StringBuffer("-Xbootclasspath/p:");
39 Boolean first = true;
40 for (Resource res : pBootClasspath) {
41 if (first)
42 first = false;
43 else
44 buf.append(File.pathSeparatorChar);
45
46 buf.append(asFile(res));
47 }
48 command.add(buf.toString());
49 }
50
51 for (String jvmArg : jvmArgs) {
52 command.add(jvmArg);
53 }
54
55 command.add("-cp");
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));
61 }
62 command.add(buf.toString());
63
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());
68 }
69 } else {
70 // write system properties in a file to work around OS limitations
71 // with command line (e.g. Win XP)
72 String dir = systemPropertiesFileDir;
73 if (dir == null)
74 dir = getExecDirToUse();
75 String fileName = systemPropertiesFileName;
76 if (fileName == null)
77 fileName = systemPropertiesFileProperty + ".properties";
78
79 // Write file
80 FileOutputStream fos = null;
81 File file = new File(dir + File.separator + fileName);
82 try {
83
84 if (!file.getParentFile().exists())
85 file.getParentFile().mkdirs();
86 fos = new FileOutputStream(file);
87 systemProperties.store(fos, "Automatically generated by "
88 + getClass());
89 command.add("-D" + systemPropertiesFileProperty + "="
90 + file.getCanonicalPath());
91 } catch (IOException e) {
92 throw new SlcException("Cannot write to system properties to "
93 + file, e);
94 } finally {
95 IOUtils.closeQuietly(fos);
96 }
97 }
98
99 // Program
100 command.add(mainClass);
101
102 for (String arg : args) {
103 command.add(arg);
104 }
105
106 setCommand(command);
107 }
108
109 protected File asFile(Resource res) {
110 try {
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);
116 }
117
118 }
119
120 protected File copyToTempFile(Resource res) {
121 File tempFile;
122 FileOutputStream fos;
123 try {
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);
130 }
131 IOUtils.closeQuietly(fos);
132 return tempFile;
133 }
134
135 public Properties getSystemProperties() {
136 return systemProperties;
137 }
138
139 public void setSystemProperties(Properties systemProperties) {
140 this.systemProperties = systemProperties;
141 }
142
143 public List<Resource> getClasspath() {
144 return classpath;
145 }
146
147 public void setClasspath(List<Resource> classpath) {
148 this.classpath = classpath;
149 }
150
151 public List<Resource> getPBootClasspath() {
152 return pBootClasspath;
153 }
154
155 public void setPBootClasspath(List<Resource> bootClasspath) {
156 pBootClasspath = bootClasspath;
157 }
158
159 public Resource getJvm() {
160 return jvm;
161 }
162
163 public void setJvm(Resource jvm) {
164 this.jvm = jvm;
165 }
166
167 public String getMainClass() {
168 return mainClass;
169 }
170
171 public void setMainClass(String mainClass) {
172 this.mainClass = mainClass;
173 }
174
175 public List<String> getJvmArgs() {
176 return jvmArgs;
177 }
178
179 public void setJvmArgs(List<String> jvmArgs) {
180 this.jvmArgs = jvmArgs;
181 }
182
183 public List<String> getArgs() {
184 return args;
185 }
186
187 public void setArgs(List<String> args) {
188 this.args = args;
189 }
190
191 public void setSystemPropertiesFileProperty(
192 String systemPropertiesFilePropertyName) {
193 this.systemPropertiesFileProperty = systemPropertiesFilePropertyName;
194 }
195
196 public void setSystemPropertiesFileDir(String systemPropertiesFileDir) {
197 this.systemPropertiesFileDir = systemPropertiesFileDir;
198 }
199
200 public void setSystemPropertiesFileName(String systemPropertiesFileName) {
201 this.systemPropertiesFileName = systemPropertiesFileName;
202 }
203
204 }