]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/DefaultSlcRuntime.java
Introduce org.argeo.slc.lib.detached
[gpl/argeo-slc.git] / runtime / org.argeo.slc.launcher / 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 if (slcRootFile == null)
73 throw new SlcException(
74 "Could not find any SLC root file, "
75 + "please configure one at the root of your scripts hierarchy.");
76
77 SlcExecution slcExecution = createSlcExecution(runtime, slcRootFile,
78 scriptRes, targets);
79
80 AntSlcApplication application = getApplication(slcRootFile);
81 application.execute(slcExecution, properties, references,
82 executionOutput);
83 }
84
85 protected Resource findScript(String scriptStr) {
86 Resource scriptRes;
87 if (new File(scriptStr).exists()) {
88 scriptRes = new FileSystemResource(scriptStr);
89 } else {
90 scriptRes = new DefaultResourceLoader(SlcMain.class
91 .getClassLoader()).getResource(scriptStr);
92 }
93 return scriptRes;
94 }
95
96 protected SlcExecution createSlcExecution(String runtimeStr,
97 Resource slcRootFile, Resource script, String targets) {
98 SlcExecution slcExecution = new SlcExecution();
99 slcExecution.setUuid(UUID.randomUUID().toString());
100 try {
101 slcExecution.setHost(InetAddress.getLocalHost().getHostName());
102 } catch (UnknownHostException e) {
103 slcExecution.setHost(SlcExecution.UNKOWN_HOST);
104 }
105
106 slcExecution.setType(AntConstants.EXECTYPE_SLC_ANT);
107
108 slcExecution.setUser(System.getProperty("user.name"));
109
110 if (runtimeStr != null)
111 slcExecution.getAttributes().put(AntConstants.EXECATTR_RUNTIME,
112 runtimeStr);
113 String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
114 .getParent(slcRootFile), script);
115
116 slcExecution.getAttributes().put(AntConstants.EXECATTR_ANT_FILE,
117 scriptRelativePath);
118 if (targets != null)
119 slcExecution.getAttributes().put(AntConstants.EXECATTR_ANT_TARGETS,
120 targets);
121
122 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
123 return slcExecution;
124 }
125
126 protected AntSlcApplication getApplication(Resource slcRootFile) {
127 AntSlcApplication application = new AntSlcApplication();
128 InputStream inRootFile = null;
129 try {
130 // Remove basedir property in order to avoid conflict with Maven
131 // if (all.containsKey("basedir"))
132 // all.remove("basedir");
133
134 inRootFile = slcRootFile.getInputStream();
135 Properties rootProps = loadFile(inRootFile);
136
137 Resource confDir = null;
138 File workDir = null;
139 // Root dir
140 final Resource rootDir = SpringUtils.getParent(slcRootFile);
141
142 // Conf dir
143 String confDirStr = rootProps
144 .getProperty(AntConstants.CONF_DIR_PROPERTY);
145 if (confDirStr != null)
146 confDir = new DefaultResourceLoader(application.getClass()
147 .getClassLoader()).getResource(confDirStr);
148
149 if (confDir == null || !confDir.exists()) {
150 // confDir = rootDir.createRelative("../conf");
151 confDir = SpringUtils.getParent(rootDir)
152 .createRelative("conf/");
153 }
154
155 // Work dir
156 String workDirStr = rootProps
157 .getProperty(AntConstants.WORK_DIR_PROPERTY);
158 if (workDirStr != null) {
159 workDir = new File(workDirStr);
160 }
161
162 if (workDir == null || !workDir.exists()) {
163 try {
164 File rootDirAsFile = rootDir.getFile();
165 workDir = new File(rootDirAsFile.getParent()
166 + File.separator + "work").getCanonicalFile();
167 } catch (IOException e) {
168 workDir = new File(System.getProperty("java.io.tmpdir")
169 + File.separator + "slcExecutions" + File.separator
170 + slcRootFile.getURL().getPath());
171 log.debug("Root dir is not a file: " + e.getMessage()
172 + ", creating work dir in temp: " + workDir);
173 }
174 workDir.mkdirs();
175 }
176
177 application.setConfDir(confDir);
178 application.setRootDir(rootDir);
179 application.setWorkDir(workDir);
180
181 return application;
182 } catch (IOException e) {
183 throw new SlcException(
184 "Could not prepare SLC application for root file "
185 + slcRootFile, e);
186 } finally {
187 IOUtils.closeQuietly(inRootFile);
188 }
189 }
190
191 /**
192 * Recursively scans directories downwards until it find a file name as
193 * defined by {@link #SLC_ROOT_FILE_NAME}.
194 */
195 protected Resource findSlcRootFile(Resource currDir) {
196 if (log.isTraceEnabled())
197 log.trace("Look for SLC root file in " + currDir);
198
199 try {
200 Resource slcRootFile = currDir.createRelative(SLC_ROOT_FILE_NAME);
201 if (slcRootFile.exists()) {
202 if (log.isDebugEnabled())
203 log.debug("Found SLC root file: " + slcRootFile);
204 return slcRootFile;
205 } else {
206 String currPath = currDir.getURL().getPath();
207 if (currPath.equals("/") || currPath.equals("")) {
208 return null;
209 } else {
210 return findSlcRootFile(SpringUtils.getParent(currDir));
211 }
212 }
213 } catch (IOException e) {
214 throw new SlcException("Problem when looking in SLC root file in "
215 + currDir, e);
216 }
217 }
218
219 /** Loads the content of a file as <code>Properties</code>. */
220 private Properties loadFile(InputStream in) {
221 Properties p = new Properties();
222 try {
223 p.load(in);
224 } catch (IOException e) {
225 throw new SlcException("Cannot read SLC root file", e);
226 }
227 return p;
228 }
229 }