1 package org
.argeo
.slc
.cli
;
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
.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
;
26 public class DefaultSlcRuntime
{
27 private final static Log log
= LogFactory
.getLog(DefaultSlcRuntime
.class);
29 public final static String SLC_ROOT_FILE_NAME
= "slcRoot.properties";
32 * Simplified execution with default runtime, default target, and no
33 * properties/reference arguments.
37 * @param executionOutput
40 * @see #executeScript(String, String, String, Properties, Map,
43 public void executeScript(String script
,
44 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
45 executeScript(null, script
, null, null, null, executionOutput
);
49 * Simplified execution with default runtime, and no properties/reference
55 * comma separated list of targets
56 * @param executionOutput
58 * @see #executeScript(String, String, String, Properties, Map,
61 public void executeScript(String script
, String targets
,
62 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
63 executeScript(null, script
, targets
, null, null, executionOutput
);
66 public void executeScript(String runtime
, String script
, String targets
,
67 Properties properties
, Map
<String
, Object
> references
,
68 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
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.");
77 SlcExecution slcExecution
= createSlcExecution(runtime
, slcRootFile
,
80 AntSlcApplication application
= getApplication(slcRootFile
);
81 application
.execute(slcExecution
, properties
, references
,
85 protected Resource
findScript(String scriptStr
) {
87 if (new File(scriptStr
).exists()) {
88 scriptRes
= new FileSystemResource(scriptStr
);
90 scriptRes
= new DefaultResourceLoader(SlcMain
.class
91 .getClassLoader()).getResource(scriptStr
);
96 protected SlcExecution
createSlcExecution(String runtimeStr
,
97 Resource slcRootFile
, Resource script
, String targets
) {
98 SlcExecution slcExecution
= new SlcExecution();
99 slcExecution
.setUuid(UUID
.randomUUID().toString());
101 slcExecution
.setHost(InetAddress
.getLocalHost().getHostName());
102 } catch (UnknownHostException e
) {
103 slcExecution
.setHost(SlcExecution
.UNKOWN_HOST
);
106 slcExecution
.setType(AntConstants
.EXECTYPE_SLC_ANT
);
108 slcExecution
.setUser(System
.getProperty("user.name"));
110 if (runtimeStr
!= null)
111 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_RUNTIME
,
113 String scriptRelativePath
= SpringUtils
.extractRelativePath(SpringUtils
114 .getParent(slcRootFile
), script
);
116 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_ANT_FILE
,
119 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_ANT_TARGETS
,
122 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
126 protected AntSlcApplication
getApplication(Resource slcRootFile
) {
127 AntSlcApplication application
= new AntSlcApplication();
128 InputStream inRootFile
= null;
130 // Remove basedir property in order to avoid conflict with Maven
131 // if (all.containsKey("basedir"))
132 // all.remove("basedir");
134 inRootFile
= slcRootFile
.getInputStream();
135 Properties rootProps
= loadFile(inRootFile
);
137 Resource confDir
= null;
140 final Resource rootDir
= SpringUtils
.getParent(slcRootFile
);
143 String confDirStr
= rootProps
144 .getProperty(AntConstants
.CONF_DIR_PROPERTY
);
145 if (confDirStr
!= null)
146 confDir
= new DefaultResourceLoader(application
.getClass()
147 .getClassLoader()).getResource(confDirStr
);
149 if (confDir
== null || !confDir
.exists()) {
150 // confDir = rootDir.createRelative("../conf");
151 confDir
= SpringUtils
.getParent(rootDir
)
152 .createRelative("conf/");
156 String workDirStr
= rootProps
157 .getProperty(AntConstants
.WORK_DIR_PROPERTY
);
158 if (workDirStr
!= null) {
159 workDir
= new File(workDirStr
);
162 if (workDir
== null || !workDir
.exists()) {
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
);
177 application
.setConfDir(confDir
);
178 application
.setRootDir(rootDir
);
179 application
.setWorkDir(workDir
);
182 } catch (IOException e
) {
183 throw new SlcException(
184 "Could not prepare SLC application for root file "
187 IOUtils
.closeQuietly(inRootFile
);
192 * Recursively scans directories downwards until it find a file name as
193 * defined by {@link #SLC_ROOT_FILE_NAME}.
195 protected Resource
findSlcRootFile(Resource currDir
) {
196 if (log
.isTraceEnabled())
197 log
.trace("Look for SLC root file in " + currDir
);
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
);
206 String currPath
= currDir
.getURL().getPath();
207 if (currPath
.equals("/") || currPath
.equals("")) {
210 return findSlcRootFile(SpringUtils
.getParent(currDir
));
213 } catch (IOException e
) {
214 throw new SlcException("Problem when looking in SLC root file in "
219 /** Loads the content of a file as <code>Properties</code>. */
220 private Properties
loadFile(InputStream in
) {
221 Properties p
= new Properties();
224 } catch (IOException e
) {
225 throw new SlcException("Cannot read SLC root file", e
);