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 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
>();
41 private String systemPropertiesFileProperty
= null;
42 private String systemPropertiesFileDir
= null;
43 private String systemPropertiesFileName
= null;
45 public void afterPropertiesSet() throws Exception
{
46 List
<Object
> command
= new ArrayList
<Object
>();
48 command
.add(asFile(jvm
).getPath());
52 if (pBootClasspath
.size() > 0) {
53 StringBuffer buf
= new StringBuffer("-Xbootclasspath/p:");
55 for (Resource res
: pBootClasspath
) {
59 buf
.append(File
.pathSeparatorChar
);
61 buf
.append(asFile(res
));
63 command
.add(buf
.toString());
66 for (String jvmArg
: jvmArgs
) {
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
));
77 command
.add(buf
.toString());
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());
85 // write system properties in a file to work around OS limitations
86 // with command line (e.g. Win XP)
87 String dir
= systemPropertiesFileDir
;
89 dir
= getExecDirToUse();
90 String fileName
= systemPropertiesFileName
;
92 fileName
= systemPropertiesFileProperty
+ ".properties";
95 FileOutputStream fos
= null;
96 File file
= new File(dir
+ File
.separator
+ fileName
);
99 if (!file
.getParentFile().exists())
100 file
.getParentFile().mkdirs();
101 fos
= new FileOutputStream(file
);
102 systemProperties
.store(fos
, "Automatically generated by "
104 command
.add("-D" + systemPropertiesFileProperty
+ "="
105 + file
.getCanonicalPath());
106 } catch (IOException e
) {
107 throw new SlcException("Cannot write to system properties to "
110 IOUtils
.closeQuietly(fos
);
115 command
.add(mainClass
);
117 for (String arg
: args
) {
124 protected File
asFile(Resource res
) {
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
);
135 protected File
copyToTempFile(Resource res
) {
137 FileOutputStream fos
;
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
);
146 IOUtils
.closeQuietly(fos
);
150 public Properties
getSystemProperties() {
151 return systemProperties
;
154 public void setSystemProperties(Properties systemProperties
) {
155 this.systemProperties
= systemProperties
;
158 public List
<Resource
> getClasspath() {
162 public void setClasspath(List
<Resource
> classpath
) {
163 this.classpath
= classpath
;
166 public List
<Resource
> getPBootClasspath() {
167 return pBootClasspath
;
170 public void setPBootClasspath(List
<Resource
> bootClasspath
) {
171 pBootClasspath
= bootClasspath
;
174 public Resource
getJvm() {
178 public void setJvm(Resource jvm
) {
182 public String
getMainClass() {
186 public void setMainClass(String mainClass
) {
187 this.mainClass
= mainClass
;
190 public List
<String
> getJvmArgs() {
194 public void setJvmArgs(List
<String
> jvmArgs
) {
195 this.jvmArgs
= jvmArgs
;
198 public List
<String
> getArgs() {
202 public void setArgs(List
<String
> args
) {
206 public void setSystemPropertiesFileProperty(
207 String systemPropertiesFilePropertyName
) {
208 this.systemPropertiesFileProperty
= systemPropertiesFilePropertyName
;
211 public void setSystemPropertiesFileDir(String systemPropertiesFileDir
) {
212 this.systemPropertiesFileDir
= systemPropertiesFileDir
;
215 public void setSystemPropertiesFileName(String systemPropertiesFileName
) {
216 this.systemPropertiesFileName
= systemPropertiesFileName
;