2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.core
.execution
.tasks
;
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
;
26 import java
.util
.Properties
;
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
;
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
>();
42 private String systemPropertiesFileProperty
= null;
43 private String systemPropertiesFileDir
= null;
44 private String systemPropertiesFileName
= null;
46 public void afterPropertiesSet() throws Exception
{
47 List
<Object
> command
= new ArrayList
<Object
>();
49 command
.add(asFile(jvm
).getPath());
53 if (pBootClasspath
.size() > 0) {
54 StringBuffer buf
= new StringBuffer("-Xbootclasspath/p:");
56 for (Resource res
: pBootClasspath
) {
60 buf
.append(File
.pathSeparatorChar
);
62 buf
.append(asFile(res
));
64 command
.add(buf
.toString());
67 for (String jvmArg
: jvmArgs
) {
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
));
78 command
.add(buf
.toString());
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());
86 // write system properties in a file to work around OS limitations
87 // with command line (e.g. Win XP)
88 String dir
= systemPropertiesFileDir
;
90 dir
= getExecDirToUse();
91 String fileName
= systemPropertiesFileName
;
93 fileName
= systemPropertiesFileProperty
+ ".properties";
96 FileOutputStream fos
= null;
97 File file
= new File(dir
+ File
.separator
+ fileName
);
100 if (!file
.getParentFile().exists())
101 file
.getParentFile().mkdirs();
102 fos
= new FileOutputStream(file
);
103 systemProperties
.store(fos
, "Automatically generated by "
105 command
.add("-D" + systemPropertiesFileProperty
+ "="
106 + file
.getCanonicalPath());
107 } catch (IOException e
) {
108 throw new SlcException("Cannot write to system properties to "
111 IOUtils
.closeQuietly(fos
);
116 command
.add(mainClass
);
118 for (String arg
: args
) {
125 protected File
asFile(Resource res
) {
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
);
136 protected File
copyToTempFile(Resource res
) {
138 FileOutputStream fos
;
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
);
147 IOUtils
.closeQuietly(fos
);
151 public Properties
getSystemProperties() {
152 return systemProperties
;
155 public void setSystemProperties(Properties systemProperties
) {
156 this.systemProperties
= systemProperties
;
159 public List
<Resource
> getClasspath() {
163 public void setClasspath(List
<Resource
> classpath
) {
164 this.classpath
= classpath
;
167 public List
<Resource
> getPBootClasspath() {
168 return pBootClasspath
;
171 public void setPBootClasspath(List
<Resource
> bootClasspath
) {
172 pBootClasspath
= bootClasspath
;
175 public Resource
getJvm() {
179 public void setJvm(Resource jvm
) {
183 public String
getMainClass() {
187 public void setMainClass(String mainClass
) {
188 this.mainClass
= mainClass
;
191 public List
<String
> getJvmArgs() {
195 public void setJvmArgs(List
<String
> jvmArgs
) {
196 this.jvmArgs
= jvmArgs
;
199 public List
<String
> getArgs() {
203 public void setArgs(List
<String
> args
) {
207 public void setSystemPropertiesFileProperty(
208 String systemPropertiesFilePropertyName
) {
209 this.systemPropertiesFileProperty
= systemPropertiesFilePropertyName
;
212 public void setSystemPropertiesFileDir(String systemPropertiesFileDir
) {
213 this.systemPropertiesFileDir
= systemPropertiesFileDir
;
216 public void setSystemPropertiesFileName(String systemPropertiesFileName
) {
217 this.systemPropertiesFileName
= systemPropertiesFileName
;