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
.AntSlcApplication
;
16 import org
.argeo
.slc
.ant
.SlcAntConstants
;
17 import org
.argeo
.slc
.ant
.SlcAntException
;
18 import org
.argeo
.slc
.core
.SlcException
;
19 import org
.argeo
.slc
.core
.process
.SlcExecution
;
20 import org
.argeo
.slc
.runtime
.SlcExecutionContext
;
21 import org
.argeo
.slc
.spring
.SpringUtils
;
22 import org
.springframework
.core
.io
.DefaultResourceLoader
;
23 import org
.springframework
.core
.io
.Resource
;
25 public class DefaultSlcRuntime
{
26 private final static Log log
= LogFactory
.getLog(DefaultSlcRuntime
.class);
28 public final static String SLC_ROOT_FILE_NAME
= "slcRoot.properties";
30 public SlcExecutionContext
executeScript(String runtimeStr
,
31 Resource script
, String targets
, Properties properties
,
32 Map
<String
, Object
> references
) {
34 Resource slcRootFile
= findSlcRootFile(script
);
35 String scriptRelativePath
= SpringUtils
.extractRelativePath(SpringUtils
36 .getParent(slcRootFile
), script
);
38 SlcExecution slcExecution
= createSlcExecution();
39 slcExecution
.setStatus(SlcExecution
.STATUS_RUNNING
);
40 slcExecution
.getAttributes().put(SlcAntConstants
.EXECATTR_RUNTIME
,
42 slcExecution
.getAttributes().put(SlcAntConstants
.EXECATTR_ANT_FILE
,
45 slcExecution
.getAttributes().put(
46 SlcAntConstants
.EXECATTR_ANT_TARGETS
, targets
);
48 AntSlcApplication application
= getApplication(slcRootFile
);
49 return application
.execute(slcExecution
, properties
, references
);
52 protected SlcExecution
createSlcExecution() {
53 SlcExecution slcExecution
= new SlcExecution();
54 slcExecution
.setUuid(UUID
.randomUUID().toString());
56 slcExecution
.setHost(InetAddress
.getLocalHost().getHostName());
57 } catch (UnknownHostException e
) {
58 slcExecution
.setHost(SlcExecution
.UNKOWN_HOST
);
61 slcExecution
.setType(SlcAntConstants
.EXECTYPE_SLC_ANT
);
63 slcExecution
.setUser(System
.getProperty("user.name"));
67 protected AntSlcApplication
getApplication(Resource slcRootFile
) {
68 AntSlcApplication application
= new AntSlcApplication();
69 InputStream inRootFile
= null;
71 // Remove basedir property in order to avoid conflict with Maven
72 // if (all.containsKey("basedir"))
73 // all.remove("basedir");
75 inRootFile
= slcRootFile
.getInputStream();
76 Properties rootProps
= loadFile(inRootFile
);
78 Resource confDir
= null;
81 final Resource rootDir
= SpringUtils
.getParent(slcRootFile
);
84 String confDirStr
= rootProps
85 .getProperty(SlcAntConstants
.CONF_DIR_PROPERTY
);
86 if (confDirStr
!= null)
87 confDir
= new DefaultResourceLoader(application
.getClass()
88 .getClassLoader()).getResource(confDirStr
);
90 if (confDir
== null || !confDir
.exists()) {
91 // confDir = rootDir.createRelative("../conf");
92 confDir
= SpringUtils
.getParent(rootDir
)
93 .createRelative("conf/");
97 String workDirStr
= rootProps
98 .getProperty(SlcAntConstants
.WORK_DIR_PROPERTY
);
99 if (workDirStr
!= null) {
100 workDir
= new File(workDirStr
);
103 if (workDir
== null || !workDir
.exists()) {
105 File rootDirAsFile
= rootDir
.getFile();
106 workDir
= new File(rootDirAsFile
.getParent()
107 + File
.separator
+ "work").getCanonicalFile();
108 } catch (IOException e
) {
109 workDir
= new File(System
.getProperty("java.io.tmpdir")
110 + File
.separator
+ "slcExecutions" + File
.separator
111 + slcRootFile
.getURL().getPath());
112 log
.debug("Root dir is not a file: " + e
.getMessage()
113 + ", creating work dir in temp: " + workDir
);
118 application
.setConfDir(confDir
);
119 application
.setRootDir(rootDir
);
120 application
.setWorkDir(workDir
);
123 } catch (IOException e
) {
124 throw new SlcException(
125 "Could not prepare SLC application for root file "
128 IOUtils
.closeQuietly(inRootFile
);
133 * Recursively scans directories downwards until it find a file name as
134 * defined by {@link #SLC_ROOT_FILE_NAME}.
136 protected Resource
findSlcRootFile(Resource currDir
) {
137 if (log
.isDebugEnabled())
138 log
.debug("Look for SLC root file in " + currDir
);
141 Resource slcRootFile
= currDir
.createRelative(SLC_ROOT_FILE_NAME
);
142 if (slcRootFile
.exists()) {
145 String currPath
= currDir
.getURL().getPath();
146 if (currPath
.equals("/") || currPath
.equals("")) {
149 return findSlcRootFile(SpringUtils
.getParent(currDir
));
152 } catch (IOException e
) {
153 throw new SlcException("Problem when looking in SLC root file in "
158 /** Loads the content of a file as <code>Properties</code>. */
159 private Properties
loadFile(InputStream in
) {
160 Properties p
= new Properties();
163 } catch (IOException e
) {
164 throw new SlcAntException("Cannot read SLC root file", e
);