]> 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 license headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / tasks / JvmProcess.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.execution.tasks;
18
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.apache.commons.io.IOUtils;
29 import org.argeo.slc.SlcException;
30 import org.springframework.beans.factory.InitializingBean;
31 import org.springframework.core.io.Resource;
32
33 public class JvmProcess extends SystemCall implements InitializingBean {
34 private Properties systemProperties = new Properties();
35 private List<Resource> classpath = new ArrayList<Resource>();
36 private List<Resource> pBootClasspath = new ArrayList<Resource>();
37 private Resource jvm = null;
38 private String mainClass;
39 private List<String> jvmArgs = new ArrayList<String>();
40 private List<String> args = new ArrayList<String>();
41
42 private String systemPropertiesFileProperty = null;
43 private String systemPropertiesFileDir = null;
44 private String systemPropertiesFileName = null;
45
46 public void afterPropertiesSet() throws Exception {
47 List<Object> command = new ArrayList<Object>();
48 if (jvm != null)
49 command.add(asFile(jvm).getPath());
50 else
51 command.add("java");
52
53 if (pBootClasspath.size() > 0) {
54 StringBuffer buf = new StringBuffer("-Xbootclasspath/p:");
55 Boolean first = true;
56 for (Resource res : pBootClasspath) {
57 if (first)
58 first = false;
59 else
60 buf.append(File.pathSeparatorChar);
61
62 buf.append(asFile(res));
63 }
64 command.add(buf.toString());
65 }
66
67 for (String jvmArg : jvmArgs) {
68 command.add(jvmArg);
69 }
70
71 command.add("-cp");
72 StringBuffer buf = new StringBuffer("");
73 for (Resource res : classpath) {
74 if (buf.length() != 0)
75 buf.append(File.pathSeparatorChar);
76 buf.append(asFile(res));
77 }
78 command.add(buf.toString());
79
80 if (systemPropertiesFileProperty == null) {
81 // pass system properties as argument
82 for (Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
83 command.add("-D" + entry.getKey() + "=" + entry.getValue());
84 }
85 } else {
86 // write system properties in a file to work around OS limitations
87 // with command line (e.g. Win XP)
88 String dir = systemPropertiesFileDir;
89 if (dir == null)
90 dir = getExecDirToUse();
91 String fileName = systemPropertiesFileName;
92 if (fileName == null)
93 fileName = systemPropertiesFileProperty + ".properties";
94
95 // Write file
96 FileOutputStream fos = null;
97 File file = new File(dir + File.separator + fileName);
98 try {
99
100 if (!file.getParentFile().exists())
101 file.getParentFile().mkdirs();
102 fos = new FileOutputStream(file);
103 systemProperties.store(fos, "Automatically generated by "
104 + getClass());
105 command.add("-D" + systemPropertiesFileProperty + "="
106 + file.getCanonicalPath());
107 } catch (IOException e) {
108 throw new SlcException("Cannot write to system properties to "
109 + file, e);
110 } finally {
111 IOUtils.closeQuietly(fos);
112 }
113 }
114
115 // Program
116 command.add(mainClass);
117
118 for (String arg : args) {
119 command.add(arg);
120 }
121
122 setCommand(command);
123 }
124
125 protected File asFile(Resource res) {
126 try {
127 return res.getFile();
128 } catch (FileNotFoundException e) {
129 return copyToTempFile(res);
130 } catch (IOException e) {
131 throw new SlcException("Cannot convert resource to file", e);
132 }
133
134 }
135
136 protected File copyToTempFile(Resource res) {
137 File tempFile;
138 FileOutputStream fos;
139 try {
140 tempFile = File.createTempFile("slcJvmProcess-", res.getFilename());
141 tempFile.deleteOnExit();
142 fos = new FileOutputStream(tempFile);
143 IOUtils.copy(res.getInputStream(), fos);
144 } catch (IOException e) {
145 throw new SlcException("Cannot copy " + res + " to temp file.", e);
146 }
147 IOUtils.closeQuietly(fos);
148 return tempFile;
149 }
150
151 public Properties getSystemProperties() {
152 return systemProperties;
153 }
154
155 public void setSystemProperties(Properties systemProperties) {
156 this.systemProperties = systemProperties;
157 }
158
159 public List<Resource> getClasspath() {
160 return classpath;
161 }
162
163 public void setClasspath(List<Resource> classpath) {
164 this.classpath = classpath;
165 }
166
167 public List<Resource> getPBootClasspath() {
168 return pBootClasspath;
169 }
170
171 public void setPBootClasspath(List<Resource> bootClasspath) {
172 pBootClasspath = bootClasspath;
173 }
174
175 public Resource getJvm() {
176 return jvm;
177 }
178
179 public void setJvm(Resource jvm) {
180 this.jvm = jvm;
181 }
182
183 public String getMainClass() {
184 return mainClass;
185 }
186
187 public void setMainClass(String mainClass) {
188 this.mainClass = mainClass;
189 }
190
191 public List<String> getJvmArgs() {
192 return jvmArgs;
193 }
194
195 public void setJvmArgs(List<String> jvmArgs) {
196 this.jvmArgs = jvmArgs;
197 }
198
199 public List<String> getArgs() {
200 return args;
201 }
202
203 public void setArgs(List<String> args) {
204 this.args = args;
205 }
206
207 public void setSystemPropertiesFileProperty(
208 String systemPropertiesFilePropertyName) {
209 this.systemPropertiesFileProperty = systemPropertiesFilePropertyName;
210 }
211
212 public void setSystemPropertiesFileDir(String systemPropertiesFileDir) {
213 this.systemPropertiesFileDir = systemPropertiesFileDir;
214 }
215
216 public void setSystemPropertiesFileName(String systemPropertiesFileName) {
217 this.systemPropertiesFileName = systemPropertiesFileName;
218 }
219
220 }