]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.support/src/org/argeo/slc/lib/linux/ScriptCall.java
Make core runtime features independent of Spring.
[gpl/argeo-slc.git] / org.argeo.slc.support / src / org / argeo / slc / lib / linux / ScriptCall.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.lib.linux;
17
18 import java.io.File;
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;
24
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;
31
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>();
36
37 private Boolean localScriptCopy = false;
38
39 /** For use in Spring. */
40 public ScriptCall() {
41 super();
42 }
43
44 /** For use in code ({@link #init()} is called). */
45 public ScriptCall(Resource script) {
46 this.script = script;
47 init();
48 }
49
50 public void init() {
51 initInterpreter();
52 for (Object obj : scriptArgs) {
53 arg(obj.toString());
54 }
55 }
56
57 public void afterPropertiesSet() throws Exception {
58 init();
59 }
60
61 protected void initInterpreter() {
62 String ext = FilenameUtils.getExtension(script.getFilename());
63
64 if (localScriptCopy) {
65 File scriptFile = copyScript();
66 if ("sh".equals(ext))
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());
72 else
73 throw new SlcException(
74 "Cannot initialize script intepreter for " + script);
75 } else {
76 setStdInFile(script);
77 if ("sh".equals(ext))
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("-");
83 else
84 throw new SlcException(
85 "Cannot initialize script intepreter for " + script);
86 }
87 }
88
89 private File copyScript() {
90 InputStream in = null;
91 OutputStream out = null;
92 try {
93 File scriptFile = File.createTempFile("script", ".sh");
94 scriptFile.deleteOnExit();
95 in = script.getInputStream();
96 out = new FileOutputStream(scriptFile);
97 IOUtils.copy(in, out);
98 return scriptFile;
99 } catch (Exception e) {
100 throw new SlcException("Cannot copy " + script, e);
101 } finally {
102 IOUtils.closeQuietly(in);
103 IOUtils.closeQuietly(out);
104 }
105 }
106
107 public void setScript(Resource script) {
108 this.script = script;
109 }
110
111 public void setScriptArgs(List<Object> scriptArgs) {
112 this.scriptArgs = scriptArgs;
113 }
114
115 public void setLocalScriptCopy(Boolean localScriptCopy) {
116 this.localScriptCopy = localScriptCopy;
117 }
118
119 }