]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcRuntime.java
Properly pass properties
[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 // Create SlcExecution from arguments
76 SlcExecution slcExecution = createSlcExecution(runtime, slcRootFile,
77 scriptRes, targets);
78
79 // Init application
80 AntSlcApplication application = new AntSlcApplication();
81 application.setSlcRootFile(slcRootFile);
82 application.initFromSlcRootFile();
83
84 // Execute test
85 application.execute(slcExecution, properties, references,
86 executionOutput);
87 }
88
89 protected Resource findScript(String scriptStr) {
90 Resource scriptRes;
91 if (new File(scriptStr).exists()) {
92 scriptRes = new FileSystemResource(scriptStr);
93 } else {
94 scriptRes = new DefaultResourceLoader(Thread.currentThread()
95 .getContextClassLoader()).getResource(scriptStr);
96 }
97 return scriptRes;
98 }
99
100 protected SlcExecution createSlcExecution(String runtimeStr,
101 Resource slcRootFile, Resource script, String targets) {
102 SlcExecution slcExecution = new SlcExecution();
103 slcExecution.setUuid(UUID.randomUUID().toString());
104 try {
105 slcExecution.setHost(InetAddress.getLocalHost().getHostName());
106 } catch (UnknownHostException e) {
107 slcExecution.setHost(SlcExecution.UNKOWN_HOST);
108 }
109
110 slcExecution.setType(AntConstants.EXECTYPE_SLC_ANT);
111
112 slcExecution.setUser(System.getProperty("user.name"));
113
114 if (runtimeStr != null)
115 slcExecution.getAttributes().put(AntConstants.EXECATTR_RUNTIME,
116 runtimeStr);
117 String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
118 .getParent(slcRootFile), script);
119
120 slcExecution.getAttributes().put(AntConstants.EXECATTR_ANT_FILE,
121 scriptRelativePath);
122 if (targets != null)
123 slcExecution.getAttributes().put(AntConstants.EXECATTR_ANT_TARGETS,
124 targets);
125
126 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
127 return slcExecution;
128 }
129
130 /**
131 * Recursively scans directories downwards until it find a file name as
132 * defined by {@link #SLC_ROOT_FILE_NAME}.
133 */
134 protected Resource findSlcRootFile(Resource currDir) {
135 if (log.isTraceEnabled())
136 log.trace("Look for SLC root file in " + currDir);
137
138 try {
139 Resource slcRootFile = currDir.createRelative(SLC_ROOT_FILE_NAME);
140 if (slcRootFile.exists()) {
141 if (log.isDebugEnabled())
142 log.debug("Found SLC root file: " + slcRootFile);
143 return slcRootFile;
144 } else {
145 String currPath = currDir.getURL().getPath();
146 if (currPath.equals("/") || currPath.equals("")) {
147 return null;
148 } else {
149 return findSlcRootFile(SpringUtils.getParent(currDir));
150 }
151 }
152 } catch (IOException e) {
153 throw new SlcException("Problem when looking in SLC root file in "
154 + currDir, e);
155 }
156 }
157 }