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