1 package org
.argeo
.slc
.ant
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.InetAddress
;
7 import java
.net
.UnknownHostException
;
9 import java
.util
.Properties
;
10 import java
.util
.UUID
;
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
.core
.SlcException
;
16 import org
.argeo
.slc
.core
.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
;
24 public class AntSlcRuntime
implements SlcRuntime
<AntExecutionContext
> {
25 private final static Log log
= LogFactory
.getLog(AntSlcRuntime
.class);
27 public final static String SLC_ROOT_FILE_NAME
= "slcRoot.properties";
30 * Simplified execution with default runtime, default target, and no
31 * properties/reference arguments.
35 * @param executionOutput
38 * @see #executeScript(String, String, String, Properties, Map,
41 public void executeScript(String script
,
42 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
43 executeScript(null, script
, null, null, null, executionOutput
);
47 * Simplified execution with default runtime, and no properties/reference
53 * comma separated list of targets
54 * @param executionOutput
56 * @see #executeScript(String, String, String, Properties, Map,
59 public void executeScript(String script
, String targets
,
60 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
61 executeScript(null, script
, targets
, null, null, executionOutput
);
64 public void executeScript(String runtime
, String script
, String targets
,
65 Properties properties
, Map
<String
, Object
> references
,
66 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
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.");
75 SlcExecution slcExecution
= createSlcExecution(runtime
, slcRootFile
,
78 AntSlcApplication application
= getApplication(slcRootFile
);
79 application
.execute(slcExecution
, properties
, references
,
83 protected Resource
findScript(String scriptStr
) {
85 if (new File(scriptStr
).exists()) {
86 scriptRes
= new FileSystemResource(scriptStr
);
88 scriptRes
= new DefaultResourceLoader(Thread
.currentThread()
89 .getContextClassLoader()).getResource(scriptStr
);
94 protected SlcExecution
createSlcExecution(String runtimeStr
,
95 Resource slcRootFile
, Resource script
, String targets
) {
96 SlcExecution slcExecution
= new SlcExecution();
97 slcExecution
.setUuid(UUID
.randomUUID().toString());
99 slcExecution
.setHost(InetAddress
.getLocalHost().getHostName());
100 } catch (UnknownHostException e
) {
101 slcExecution
.setHost(SlcExecution
.UNKOWN_HOST
);
104 slcExecution
.setType(AntConstants
.EXECTYPE_SLC_ANT
);
106 slcExecution
.setUser(System
.getProperty("user.name"));
108 if (runtimeStr
!= null)
109 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_RUNTIME
,
111 String scriptRelativePath
= SpringUtils
.extractRelativePath(SpringUtils
112 .getParent(slcRootFile
), script
);
114 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_ANT_FILE
,
117 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_ANT_TARGETS
,
120 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
124 protected AntSlcApplication
getApplication(Resource slcRootFile
) {
125 AntSlcApplication application
= new AntSlcApplication();
126 InputStream inRootFile
= null;
128 // Remove basedir property in order to avoid conflict with Maven
129 // if (all.containsKey("basedir"))
130 // all.remove("basedir");
132 inRootFile
= slcRootFile
.getInputStream();
133 Properties rootProps
= loadFile(inRootFile
);
135 Resource confDir
= null;
138 final Resource rootDir
= SpringUtils
.getParent(slcRootFile
);
141 String confDirStr
= rootProps
142 .getProperty(AntConstants
.CONF_DIR_PROPERTY
);
143 if (confDirStr
!= null)
144 confDir
= new DefaultResourceLoader(application
.getClass()
145 .getClassLoader()).getResource(confDirStr
);
147 if (confDir
== null || !confDir
.exists()) {
148 // confDir = rootDir.createRelative("../conf");
149 confDir
= SpringUtils
.getParent(rootDir
)
150 .createRelative("conf/");
154 String workDirStr
= rootProps
155 .getProperty(AntConstants
.WORK_DIR_PROPERTY
);
156 if (workDirStr
!= null) {
157 workDir
= new File(workDirStr
);
160 if (workDir
== null || !workDir
.exists()) {
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
);
175 application
.setConfDir(confDir
);
176 application
.setRootDir(rootDir
);
177 application
.setWorkDir(workDir
);
180 } catch (IOException e
) {
181 throw new SlcException(
182 "Could not prepare SLC application for root file "
185 IOUtils
.closeQuietly(inRootFile
);
190 * Recursively scans directories downwards until it find a file name as
191 * defined by {@link #SLC_ROOT_FILE_NAME}.
193 protected Resource
findSlcRootFile(Resource currDir
) {
194 if (log
.isTraceEnabled())
195 log
.trace("Look for SLC root file in " + currDir
);
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
);
204 String currPath
= currDir
.getURL().getPath();
205 if (currPath
.equals("/") || currPath
.equals("")) {
208 return findSlcRootFile(SpringUtils
.getParent(currDir
));
211 } catch (IOException e
) {
212 throw new SlcException("Problem when looking in SLC root file in "
217 /** Loads the content of a file as <code>Properties</code>. */
218 private Properties
loadFile(InputStream in
) {
219 Properties p
= new Properties();
222 } catch (IOException e
) {
223 throw new SlcException("Cannot read SLC root file", e
);