2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.core
.execution
.tasks
;
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
;
25 import java
.util
.Properties
;
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
;
32 /** A Java Virtual Machine process. */
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 String mainJar
;
40 private List
<String
> jvmArgs
= new ArrayList
<String
>();
41 private List
<String
> args
= new ArrayList
<String
>();
43 private String systemPropertiesFileProperty
= null;
44 private String systemPropertiesFileDir
= null;
45 private String systemPropertiesFileName
= null;
47 public void afterPropertiesSet() throws Exception
{
48 List
<Object
> command
= new ArrayList
<Object
>();
50 command
.add(asFile(jvm
).getPath());
54 if (pBootClasspath
.size() > 0) {
55 StringBuffer buf
= new StringBuffer("-Xbootclasspath/p:");
57 for (Resource res
: pBootClasspath
) {
61 buf
.append(File
.pathSeparatorChar
);
63 buf
.append(asFile(res
));
65 command
.add(buf
.toString());
68 for (String jvmArg
: jvmArgs
) {
72 if (classpath
.size() > 0) {
74 StringBuffer buf
= new StringBuffer("");
75 for (Resource res
: classpath
) {
76 if (buf
.length() != 0)
77 buf
.append(File
.pathSeparatorChar
);
78 buf
.append(asFile(res
));
80 command
.add(buf
.toString());
83 if (systemPropertiesFileProperty
== null) {
84 // pass system properties as argument
85 for (Map
.Entry
<Object
, Object
> entry
: systemProperties
.entrySet()) {
86 command
.add("-D" + entry
.getKey() + "=" + entry
.getValue());
89 // write system properties in a file to work around OS limitations
90 // with command line (e.g. Win XP)
91 String dir
= systemPropertiesFileDir
;
93 dir
= getExecDirToUse();
94 String fileName
= systemPropertiesFileName
;
96 fileName
= systemPropertiesFileProperty
+ ".properties";
99 FileOutputStream fos
= null;
100 File file
= new File(dir
+ File
.separator
+ fileName
);
103 if (!file
.getParentFile().exists())
104 file
.getParentFile().mkdirs();
105 fos
= new FileOutputStream(file
);
106 systemProperties
.store(fos
, "Automatically generated by "
108 command
.add("-D" + systemPropertiesFileProperty
+ "="
109 + file
.getCanonicalPath());
110 } catch (IOException e
) {
111 throw new SlcException("Cannot write to system properties to "
114 IOUtils
.closeQuietly(fos
);
119 if (mainClass
!= null) {
120 command
.add(mainClass
);
121 } else if (mainJar
!= null) {
123 command
.add(mainJar
);
125 throw new SlcException("No main class or jar defined");
128 for (String arg
: args
) {
135 protected File
asFile(Resource res
) {
137 return res
.getFile();
138 } catch (FileNotFoundException e
) {
139 return copyToTempFile(res
);
140 } catch (IOException e
) {
141 throw new SlcException("Cannot convert resource to file", e
);
146 protected File
copyToTempFile(Resource res
) {
148 FileOutputStream fos
;
150 tempFile
= File
.createTempFile("slcJvmProcess-", res
.getFilename());
151 tempFile
.deleteOnExit();
152 fos
= new FileOutputStream(tempFile
);
153 IOUtils
.copy(res
.getInputStream(), fos
);
154 } catch (IOException e
) {
155 throw new SlcException("Cannot copy " + res
+ " to temp file.", e
);
157 IOUtils
.closeQuietly(fos
);
161 /** Append the argument (for chaining) */
163 public SystemCall
arg(String arg
) {
168 /** Append the argument (for chaining) */
170 public SystemCall
arg(String arg
, String value
) {
176 public Properties
getSystemProperties() {
177 return systemProperties
;
180 public void setSystemProperties(Properties systemProperties
) {
181 this.systemProperties
= systemProperties
;
184 public List
<Resource
> getClasspath() {
188 public void setClasspath(List
<Resource
> classpath
) {
189 this.classpath
= classpath
;
192 public List
<Resource
> getPBootClasspath() {
193 return pBootClasspath
;
196 public void setPBootClasspath(List
<Resource
> bootClasspath
) {
197 pBootClasspath
= bootClasspath
;
200 public Resource
getJvm() {
204 public void setJvm(Resource jvm
) {
208 public String
getMainClass() {
212 public void setMainClass(String mainClass
) {
213 this.mainClass
= mainClass
;
216 public String
getMainJar() {
220 public void setMainJar(String mainJar
) {
221 this.mainJar
= mainJar
;
224 public List
<String
> getJvmArgs() {
228 public void setJvmArgs(List
<String
> jvmArgs
) {
229 this.jvmArgs
= jvmArgs
;
232 public List
<String
> getArgs() {
236 public void setArgs(List
<String
> args
) {
240 public void setSystemPropertiesFileProperty(
241 String systemPropertiesFilePropertyName
) {
242 this.systemPropertiesFileProperty
= systemPropertiesFilePropertyName
;
245 public void setSystemPropertiesFileDir(String systemPropertiesFileDir
) {
246 this.systemPropertiesFileDir
= systemPropertiesFileDir
;
249 public void setSystemPropertiesFileName(String systemPropertiesFileName
) {
250 this.systemPropertiesFileName
= systemPropertiesFileName
;