1 package org
.argeo
.slc
.cli
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.net
.InetAddress
;
8 import java
.net
.UnknownHostException
;
10 import java
.util
.Properties
;
11 import java
.util
.StringTokenizer
;
12 import java
.util
.UUID
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.slc
.ant
.AntSlcApplication
;
17 import org
.argeo
.slc
.ant
.SlcAntConstants
;
18 import org
.argeo
.slc
.ant
.SlcAntException
;
19 import org
.argeo
.slc
.core
.SlcException
;
20 import org
.argeo
.slc
.core
.process
.SlcExecution
;
21 import org
.argeo
.slc
.runtime
.SimpleSlcRuntime
;
22 import org
.argeo
.slc
.runtime
.SlcExecutionContext
;
23 import org
.springframework
.beans
.BeansException
;
24 import org
.springframework
.beans
.factory
.BeanFactory
;
25 import org
.springframework
.beans
.factory
.BeanFactoryAware
;
26 import org
.springframework
.context
.ApplicationContext
;
27 import org
.springframework
.context
.support
.GenericApplicationContext
;
28 import org
.springframework
.core
.io
.Resource
;
30 public class DefaultSlcRuntime
extends SimpleSlcRuntime
implements
32 private final static Log log
= LogFactory
.getLog(DefaultSlcRuntime
.class);
34 public final static String SLC_ROOT_FILE_NAME
= "slcRoot.properties";
36 public SlcExecutionContext
executeScript(Resource script
,
37 Properties properties
, Map
<String
, Object
> references
) {
39 if (runtimeContext
== null)
40 runtimeContext
= new GenericApplicationContext();
42 SlcExecution slcExecution
= new SlcExecution();
43 slcExecution
.setUuid(UUID
.randomUUID().toString());
45 slcExecution
.setHost(InetAddress
.getLocalHost().getHostName());
46 } catch (UnknownHostException e
) {
47 slcExecution
.setHost(SlcExecution
.UNKOWN_HOST
);
50 slcExecution
.setType(SlcAntConstants
.EXECTYPE_SLC_ANT
);
52 slcExecution
.setUser(System
.getProperty("user.name"));
53 slcExecution
.setStatus(SlcExecution
.STATUS_RUNNING
);
54 slcExecution
.getAttributes().put(SlcAntConstants
.EXECATTR_ANT_FILE
,
57 AntSlcApplication application
= new AntSlcApplication();
58 prepareApplication(slcExecution
, application
, script
);
59 return application
.execute(slcExecution
, properties
, references
);
62 public void setBeanFactory(BeanFactory beanFactory
) throws BeansException
{
63 runtimeContext
= (ApplicationContext
) beanFactory
;
66 protected void prepareApplication(SlcExecution slcExecution
,
67 AntSlcApplication application
, Resource script
) {
69 final String fileUrlPrefix
= "";
71 Resource slcRootFile
= findSlcRootFile(script
);
73 // Remove basedir property in order to avoid conflict with Maven
74 // if (all.containsKey("basedir"))
75 // all.remove("basedir");
77 InputStream inRootFile
= slcRootFile
.getInputStream();
78 Properties rootProps
= loadFile(inRootFile
);
80 Resource confDir
= null;
83 final Resource rootDir
= getParentOfFile(slcRootFile
);
86 String confDirStr
= rootProps
87 .getProperty(SlcAntConstants
.CONF_DIR_PROPERTY
);
88 if (confDirStr
!= null)
89 confDir
= runtimeContext
.getResource(confDirStr
);
91 if (confDir
== null || !confDir
.exists()) {
92 confDir
= rootDir
.createRelative("../conf");
96 String workDirStr
= rootProps
97 .getProperty(SlcAntConstants
.WORK_DIR_PROPERTY
);
98 if (workDirStr
!= null) {
99 workDir
= new File(workDirStr
);
102 if (workDir
== null || !workDir
.exists()) {
104 File rootDirAsFile
= rootDir
.getFile();
105 workDir
= new File(rootDirAsFile
.getParent()
106 + File
.separator
+ "work").getCanonicalFile();
107 } catch (IOException e
) {
108 workDir
= new File(System
.getProperty("java.io.tmpdir")
109 + File
.separator
+ "slcExecutions" + File
.separator
110 + slcExecution
.getUuid()).getCanonicalFile();
111 log
.debug("Root dir is not a file: " + e
.getMessage()
112 + ", creating work dir in temp: " + workDir
);
116 application
.setConfDir(confDir
);
117 application
.setRootDir(rootDir
);
118 application
.setWorkDir(workDir
);
120 application
.setSlcRuntime(this);
121 } catch (IOException e
) {
122 throw new SlcException(
123 "Could not prepare SLC application for SLC execution "
124 + slcExecution
.getUuid() + " and script " + script
,
128 // Properties from the conf dir files
129 // Properties properties = new Properties();
130 // StringTokenizer st = new StringTokenizer(rootProps.getProperty(
131 // PROPERTY_FILE_NAMES_PROPERTY, "slc.properties"), ",");
132 // while (st.hasMoreTokens()) {
133 // String fileName = st.nextToken();
134 // properties.putAll(loadFile(confDir.getAbsolutePath()
135 // + File.separator + fileName));
138 // for (Object o : properties.keySet()) {
139 // String key = o.toString();
140 // if (all.getProperty(key) == null) {// not already set
141 // all.setProperty(key, properties.getProperty(key));
148 * Recursively scans directories downwards until it find a file name as
149 * defined by {@link #SLC_ROOT_FILE_NAME}.
151 protected Resource
findSlcRootFile(Resource currDir
) {
152 if (log
.isDebugEnabled())
153 log
.debug("Look for SLC root file in " + currDir
);
156 Resource slcRootFile
= currDir
.createRelative(SLC_ROOT_FILE_NAME
);
157 if (slcRootFile
.exists()) {
160 String currPath
= currDir
.getURL().getPath();
161 if (currPath
.equals("/") || currPath
.equals("")) {
164 return findSlcRootFile(getParentOfDir(currDir
));
166 // int indx = currPath.lastIndexOf('/',currPath.length()-1);
169 } catch (IOException e
) {
170 throw new SlcException("Problem when looking in SLC root file in "
174 // for (File file : dir.listFiles()) {
175 // if (!file.isDirectory()
176 // && file.getName().equals(SLC_ROOT_FILE_NAME)) {
181 // File parentDir = dir.getParentFile();
182 // if (parentDir == null) {
183 // return null;// stop condition: not found
185 // return findSlcRootFile(parentDir);
189 /** Loads the content of a file as <code>Properties</code>. */
190 private Properties
loadFile(InputStream in
) {
191 Properties p
= new Properties();
194 } catch (IOException e
) {
195 throw new SlcAntException("Cannot read SLC root file", e
);
200 private Resource
getParentOfDir(Resource dir
) {
202 return dir
.createRelative("..");
203 } catch (IOException e
) {
204 throw new SlcException("Cannot get parent for resource " + dir
, e
);
208 private Resource
getParentOfFile(Resource file
) {
210 return file
.createRelative(".");
211 } catch (IOException e
) {
212 throw new SlcException("Cannot get parent for resource " + file
, e
);