]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/DefaultSlcRuntime.java
git-svn-id: https://svn.argeo.org/slc/trunk@1284 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / DefaultSlcRuntime.java
1 package org.argeo.slc.cli;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.InetAddress;
7 import java.net.UnknownHostException;
8 import java.util.Map;
9 import java.util.Properties;
10 import java.util.UUID;
11
12 import org.apache.commons.io.IOUtils;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.slc.ant.AntExecutionContext;
16 import org.argeo.slc.ant.AntSlcApplication;
17 import org.argeo.slc.ant.AntConstants;
18 import org.argeo.slc.core.SlcException;
19 import org.argeo.slc.core.process.SlcExecution;
20 import org.argeo.slc.runtime.SlcExecutionOutput;
21 import org.argeo.slc.spring.SpringUtils;
22 import org.springframework.core.io.DefaultResourceLoader;
23 import org.springframework.core.io.FileSystemResource;
24 import org.springframework.core.io.Resource;
25
26 public class DefaultSlcRuntime {
27 private final static Log log = LogFactory.getLog(DefaultSlcRuntime.class);
28
29 public final static String SLC_ROOT_FILE_NAME = "slcRoot.properties";
30
31 /**
32 * Simplified execution with default runtime, default target, and no
33 * properties/reference arguments.
34 *
35 * @param script
36 * path to the script
37 * @param executionOutput
38 * output
39 *
40 * @see #executeScript(String, String, String, Properties, Map,
41 * SlcExecutionOutput)
42 */
43 public void executeScript(String script,
44 SlcExecutionOutput<AntExecutionContext> executionOutput) {
45 executeScript(null, script, null, null, null, executionOutput);
46 }
47
48 /**
49 * Simplified execution with default runtime, and no properties/reference
50 * arguments.
51 *
52 * @param script
53 * path to the script
54 * @param targets
55 * comma separated list of targets
56 * @param executionOutput
57 * output
58 * @see #executeScript(String, String, String, Properties, Map,
59 * SlcExecutionOutput)
60 */
61 public void executeScript(String script, String targets,
62 SlcExecutionOutput<AntExecutionContext> executionOutput) {
63 executeScript(null, script, targets, null, null, executionOutput);
64 }
65
66 public void executeScript(String runtime, String script, String targets,
67 Properties properties, Map<String, Object> references,
68 SlcExecutionOutput<AntExecutionContext> executionOutput) {
69
70 Resource scriptRes = findScript(script);
71 Resource slcRootFile = findSlcRootFile(scriptRes);
72
73 SlcExecution slcExecution = createSlcExecution(runtime, slcRootFile,
74 scriptRes, targets);
75
76 AntSlcApplication application = getApplication(slcRootFile);
77 application.execute(slcExecution, properties, references,
78 executionOutput);
79 }
80
81 protected Resource findScript(String scriptStr) {
82 Resource scriptRes;
83 if (new File(scriptStr).exists()) {
84 scriptRes = new FileSystemResource(scriptStr);
85 } else {
86 scriptRes = new DefaultResourceLoader(SlcMain.class
87 .getClassLoader()).getResource(scriptStr);
88 }
89 return scriptRes;
90 }
91
92 protected SlcExecution createSlcExecution(String runtimeStr,
93 Resource slcRootFile, Resource script, String targets) {
94 SlcExecution slcExecution = new SlcExecution();
95 slcExecution.setUuid(UUID.randomUUID().toString());
96 try {
97 slcExecution.setHost(InetAddress.getLocalHost().getHostName());
98 } catch (UnknownHostException e) {
99 slcExecution.setHost(SlcExecution.UNKOWN_HOST);
100 }
101
102 slcExecution.setType(AntConstants.EXECTYPE_SLC_ANT);
103
104 slcExecution.setUser(System.getProperty("user.name"));
105
106 if (runtimeStr != null)
107 slcExecution.getAttributes().put(AntConstants.EXECATTR_RUNTIME,
108 runtimeStr);
109 String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
110 .getParent(slcRootFile), script);
111
112 slcExecution.getAttributes().put(AntConstants.EXECATTR_ANT_FILE,
113 scriptRelativePath);
114 if (targets != null)
115 slcExecution.getAttributes().put(
116 AntConstants.EXECATTR_ANT_TARGETS, targets);
117
118 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
119 return slcExecution;
120 }
121
122 protected AntSlcApplication getApplication(Resource slcRootFile) {
123 AntSlcApplication application = new AntSlcApplication();
124 InputStream inRootFile = null;
125 try {
126 // Remove basedir property in order to avoid conflict with Maven
127 // if (all.containsKey("basedir"))
128 // all.remove("basedir");
129
130 inRootFile = slcRootFile.getInputStream();
131 Properties rootProps = loadFile(inRootFile);
132
133 Resource confDir = null;
134 File workDir = null;
135 // Root dir
136 final Resource rootDir = SpringUtils.getParent(slcRootFile);
137
138 // Conf dir
139 String confDirStr = rootProps
140 .getProperty(AntConstants.CONF_DIR_PROPERTY);
141 if (confDirStr != null)
142 confDir = new DefaultResourceLoader(application.getClass()
143 .getClassLoader()).getResource(confDirStr);
144
145 if (confDir == null || !confDir.exists()) {
146 // confDir = rootDir.createRelative("../conf");
147 confDir = SpringUtils.getParent(rootDir)
148 .createRelative("conf/");
149 }
150
151 // Work dir
152 String workDirStr = rootProps
153 .getProperty(AntConstants.WORK_DIR_PROPERTY);
154 if (workDirStr != null) {
155 workDir = new File(workDirStr);
156 }
157
158 if (workDir == null || !workDir.exists()) {
159 try {
160 File rootDirAsFile = rootDir.getFile();
161 workDir = new File(rootDirAsFile.getParent()
162 + File.separator + "work").getCanonicalFile();
163 } catch (IOException e) {
164 workDir = new File(System.getProperty("java.io.tmpdir")
165 + File.separator + "slcExecutions" + File.separator
166 + slcRootFile.getURL().getPath());
167 log.debug("Root dir is not a file: " + e.getMessage()
168 + ", creating work dir in temp: " + workDir);
169 }
170 workDir.mkdirs();
171 }
172
173 application.setConfDir(confDir);
174 application.setRootDir(rootDir);
175 application.setWorkDir(workDir);
176
177 return application;
178 } catch (IOException e) {
179 throw new SlcException(
180 "Could not prepare SLC application for root file "
181 + slcRootFile, e);
182 } finally {
183 IOUtils.closeQuietly(inRootFile);
184 }
185 }
186
187 /**
188 * Recursively scans directories downwards until it find a file name as
189 * defined by {@link #SLC_ROOT_FILE_NAME}.
190 */
191 protected Resource findSlcRootFile(Resource currDir) {
192 if (log.isDebugEnabled())
193 log.debug("Look for SLC root file in " + currDir);
194
195 try {
196 Resource slcRootFile = currDir.createRelative(SLC_ROOT_FILE_NAME);
197 if (slcRootFile.exists()) {
198 return slcRootFile;
199 } else {
200 String currPath = currDir.getURL().getPath();
201 if (currPath.equals("/") || currPath.equals("")) {
202 return null;
203 } else {
204 return findSlcRootFile(SpringUtils.getParent(currDir));
205 }
206 }
207 } catch (IOException e) {
208 throw new SlcException("Problem when looking in SLC root file in "
209 + currDir, e);
210 }
211 }
212
213 /** Loads the content of a file as <code>Properties</code>. */
214 private Properties loadFile(InputStream in) {
215 Properties p = new Properties();
216 try {
217 p.load(in);
218 } catch (IOException e) {
219 throw new SlcException("Cannot read SLC root file", e);
220 }
221 return p;
222 }
223 }