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
);
73 SlcExecution slcExecution
= createSlcExecution(runtime
, slcRootFile
,
76 AntSlcApplication application
= getApplication(slcRootFile
);
77 application
.execute(slcExecution
, properties
, references
,
81 protected Resource
findScript(String scriptStr
) {
83 if (new File(scriptStr
).exists()) {
84 scriptRes
= new FileSystemResource(scriptStr
);
86 scriptRes
= new DefaultResourceLoader(SlcMain
.class
87 .getClassLoader()).getResource(scriptStr
);
92 protected SlcExecution
createSlcExecution(String runtimeStr
,
93 Resource slcRootFile
, Resource script
, String targets
) {
94 SlcExecution slcExecution
= new SlcExecution();
95 slcExecution
.setUuid(UUID
.randomUUID().toString());
97 slcExecution
.setHost(InetAddress
.getLocalHost().getHostName());
98 } catch (UnknownHostException e
) {
99 slcExecution
.setHost(SlcExecution
.UNKOWN_HOST
);
102 slcExecution
.setType(AntConstants
.EXECTYPE_SLC_ANT
);
104 slcExecution
.setUser(System
.getProperty("user.name"));
106 if (runtimeStr
!= null)
107 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_RUNTIME
,
109 String scriptRelativePath
= SpringUtils
.extractRelativePath(SpringUtils
110 .getParent(slcRootFile
), script
);
112 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_ANT_FILE
,
115 slcExecution
.getAttributes().put(
116 AntConstants
.EXECATTR_ANT_TARGETS
, targets
);
118 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
122 protected AntSlcApplication
getApplication(Resource slcRootFile
) {
123 AntSlcApplication application
= new AntSlcApplication();
124 InputStream inRootFile
= null;
126 // Remove basedir property in order to avoid conflict with Maven
127 // if (all.containsKey("basedir"))
128 // all.remove("basedir");
130 inRootFile
= slcRootFile
.getInputStream();
131 Properties rootProps
= loadFile(inRootFile
);
133 Resource confDir
= null;
136 final Resource rootDir
= SpringUtils
.getParent(slcRootFile
);
139 String confDirStr
= rootProps
140 .getProperty(AntConstants
.CONF_DIR_PROPERTY
);
141 if (confDirStr
!= null)
142 confDir
= new DefaultResourceLoader(application
.getClass()
143 .getClassLoader()).getResource(confDirStr
);
145 if (confDir
== null || !confDir
.exists()) {
146 // confDir = rootDir.createRelative("../conf");
147 confDir
= SpringUtils
.getParent(rootDir
)
148 .createRelative("conf/");
152 String workDirStr
= rootProps
153 .getProperty(AntConstants
.WORK_DIR_PROPERTY
);
154 if (workDirStr
!= null) {
155 workDir
= new File(workDirStr
);
158 if (workDir
== null || !workDir
.exists()) {
160 File rootDirAsFile
= rootDir
.getFile();
161 workDir
= new File(rootDirAsFile
.getParent()
162 + File
.separator
+ "work").getCanonicalFile();
163 } catch (IOException e
) {
164 workDir
= new File(System
.getProperty("java.io.tmpdir")
165 + File
.separator
+ "slcExecutions" + File
.separator
166 + slcRootFile
.getURL().getPath());
167 log
.debug("Root dir is not a file: " + e
.getMessage()
168 + ", creating work dir in temp: " + workDir
);
173 application
.setConfDir(confDir
);
174 application
.setRootDir(rootDir
);
175 application
.setWorkDir(workDir
);
178 } catch (IOException e
) {
179 throw new SlcException(
180 "Could not prepare SLC application for root file "
183 IOUtils
.closeQuietly(inRootFile
);
188 * Recursively scans directories downwards until it find a file name as
189 * defined by {@link #SLC_ROOT_FILE_NAME}.
191 protected Resource
findSlcRootFile(Resource currDir
) {
192 if (log
.isDebugEnabled())
193 log
.debug("Look for SLC root file in " + currDir
);
196 Resource slcRootFile
= currDir
.createRelative(SLC_ROOT_FILE_NAME
);
197 if (slcRootFile
.exists()) {
200 String currPath
= currDir
.getURL().getPath();
201 if (currPath
.equals("/") || currPath
.equals("")) {
204 return findSlcRootFile(SpringUtils
.getParent(currDir
));
207 } catch (IOException e
) {
208 throw new SlcException("Problem when looking in SLC root file in "
213 /** Loads the content of a file as <code>Properties</code>. */
214 private Properties
loadFile(InputStream in
) {
215 Properties p
= new Properties();
218 } catch (IOException e
) {
219 throw new SlcException("Cannot read SLC root file", e
);