]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/linux/ScriptCall.java
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
.lib
.linux
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.InputStream
;
21 import java
.io
.OutputStream
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 import org
.apache
.commons
.io
.FilenameUtils
;
26 import org
.apache
.commons
.io
.IOUtils
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.argeo
.slc
.core
.execution
.tasks
.SystemCall
;
29 import org
.springframework
.beans
.factory
.InitializingBean
;
30 import org
.springframework
.core
.io
.Resource
;
32 /** Call to the interpreter of a script language. */
33 public class ScriptCall
extends SystemCall
implements InitializingBean
{
34 private Resource script
;
35 private List
<Object
> scriptArgs
= new ArrayList
<Object
>();
37 private Boolean localScriptCopy
= false;
39 /** For use in Spring. */
44 /** For use in code ({@link #init()} is called). */
45 public ScriptCall(Resource script
) {
52 for (Object obj
: scriptArgs
) {
57 public void afterPropertiesSet() throws Exception
{
61 protected void initInterpreter() {
62 String ext
= FilenameUtils
.getExtension(script
.getFilename());
64 if (localScriptCopy
) {
65 File scriptFile
= copyScript();
67 arg("/bin/sh").arg(scriptFile
.getAbsolutePath());
68 else if ("pl".equals(ext
))
69 arg("/usr/bin/perl").arg(scriptFile
.getAbsolutePath());
70 else if ("py".equals(ext
))
71 arg("/usr/bin/python").arg(scriptFile
.getAbsolutePath());
73 throw new SlcException(
74 "Cannot initialize script intepreter for " + script
);
78 arg("/bin/sh").arg("-s");
79 else if ("pl".equals(ext
))
80 arg("/usr/bin/perl").arg("/dev/stdin");
81 else if ("py".equals(ext
))
82 arg("/usr/bin/python").arg("-");
84 throw new SlcException(
85 "Cannot initialize script intepreter for " + script
);
89 private File
copyScript() {
90 InputStream in
= null;
91 OutputStream out
= null;
93 File scriptFile
= File
.createTempFile("script", ".sh");
94 scriptFile
.deleteOnExit();
95 in
= script
.getInputStream();
96 out
= new FileOutputStream(scriptFile
);
97 IOUtils
.copy(in
, out
);
99 } catch (Exception e
) {
100 throw new SlcException("Cannot copy " + script
, e
);
102 IOUtils
.closeQuietly(in
);
103 IOUtils
.closeQuietly(out
);
107 public void setScript(Resource script
) {
108 this.script
= script
;
111 public void setScriptArgs(List
<Object
> scriptArgs
) {
112 this.scriptArgs
= scriptArgs
;
115 public void setLocalScriptCopy(Boolean localScriptCopy
) {
116 this.localScriptCopy
= localScriptCopy
;