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