]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/DefaultSlcRuntime.java
15aca3d86f57ebea423b7d8789e261d958a2a19a
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / DefaultSlcRuntime.java
1 package org.argeo.slc.cli;
2
3 import java.io.File;
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;
9 import java.util.Map;
10 import java.util.Properties;
11 import java.util.StringTokenizer;
12 import java.util.UUID;
13
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;
29
30 public class DefaultSlcRuntime extends SimpleSlcRuntime implements
31 BeanFactoryAware {
32 private final static Log log = LogFactory.getLog(DefaultSlcRuntime.class);
33
34 public final static String SLC_ROOT_FILE_NAME = "slcRoot.properties";
35
36 public SlcExecutionContext executeScript(Resource script,
37 Properties properties, Map<String, Object> references) {
38
39 if (runtimeContext == null)
40 runtimeContext = new GenericApplicationContext();
41
42 SlcExecution slcExecution = new SlcExecution();
43 slcExecution.setUuid(UUID.randomUUID().toString());
44 try {
45 slcExecution.setHost(InetAddress.getLocalHost().getHostName());
46 } catch (UnknownHostException e) {
47 slcExecution.setHost(SlcExecution.UNKOWN_HOST);
48 }
49
50 slcExecution.setType(SlcAntConstants.EXECTYPE_SLC_ANT);
51
52 slcExecution.setUser(System.getProperty("user.name"));
53 slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
54 slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,
55 script.toString());
56
57 AntSlcApplication application = new AntSlcApplication();
58 prepareApplication(slcExecution, application, script);
59 return application.execute(slcExecution, properties, references);
60 }
61
62 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
63 runtimeContext = (ApplicationContext) beanFactory;
64 }
65
66 protected void prepareApplication(SlcExecution slcExecution,
67 AntSlcApplication application, Resource script) {
68 try {
69 final String fileUrlPrefix = "";
70
71 Resource slcRootFile = findSlcRootFile(script);
72
73 // Remove basedir property in order to avoid conflict with Maven
74 // if (all.containsKey("basedir"))
75 // all.remove("basedir");
76
77 InputStream inRootFile = slcRootFile.getInputStream();
78 Properties rootProps = loadFile(inRootFile);
79
80 Resource confDir = null;
81 File workDir = null;
82 // Root dir
83 final Resource rootDir = getParentOfFile(slcRootFile);
84
85 // Conf dir
86 String confDirStr = rootProps
87 .getProperty(SlcAntConstants.CONF_DIR_PROPERTY);
88 if (confDirStr != null)
89 confDir = runtimeContext.getResource(confDirStr);
90
91 if (confDir == null || !confDir.exists()) {
92 confDir = rootDir.createRelative("../conf");
93 }
94
95 // Work dir
96 String workDirStr = rootProps
97 .getProperty(SlcAntConstants.WORK_DIR_PROPERTY);
98 if (workDirStr != null) {
99 workDir = new File(workDirStr);
100 }
101
102 if (workDir == null || !workDir.exists()) {
103 try {
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);
113 }
114 }
115
116 application.setConfDir(confDir);
117 application.setRootDir(rootDir);
118 application.setWorkDir(workDir);
119
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,
125 e);
126 }
127
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));
136 // }
137 //
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));
142 // }
143 // }
144 //
145 }
146
147 /**
148 * Recursively scans directories downwards until it find a file name as
149 * defined by {@link #SLC_ROOT_FILE_NAME}.
150 */
151 protected Resource findSlcRootFile(Resource currDir) {
152 if (log.isDebugEnabled())
153 log.debug("Look for SLC root file in " + currDir);
154
155 try {
156 Resource slcRootFile = currDir.createRelative(SLC_ROOT_FILE_NAME);
157 if (slcRootFile.exists()) {
158 return slcRootFile;
159 } else {
160 String currPath = currDir.getURL().getPath();
161 if (currPath.equals("/") || currPath.equals("")) {
162 return null;
163 } else {
164 return findSlcRootFile(getParentOfDir(currDir));
165 }
166 // int indx = currPath.lastIndexOf('/',currPath.length()-1);
167
168 }
169 } catch (IOException e) {
170 throw new SlcException("Problem when looking in SLC root file in "
171 + currDir, e);
172 }
173
174 // for (File file : dir.listFiles()) {
175 // if (!file.isDirectory()
176 // && file.getName().equals(SLC_ROOT_FILE_NAME)) {
177 // return file;
178 // }
179 // }
180 //
181 // File parentDir = dir.getParentFile();
182 // if (parentDir == null) {
183 // return null;// stop condition: not found
184 // } else {
185 // return findSlcRootFile(parentDir);
186 // }
187 }
188
189 /** Loads the content of a file as <code>Properties</code>. */
190 private Properties loadFile(InputStream in) {
191 Properties p = new Properties();
192 try {
193 p.load(in);
194 } catch (IOException e) {
195 throw new SlcAntException("Cannot read SLC root file", e);
196 }
197 return p;
198 }
199
200 private Resource getParentOfDir(Resource dir) {
201 try {
202 return dir.createRelative("..");
203 } catch (IOException e) {
204 throw new SlcException("Cannot get parent for resource " + dir, e);
205 }
206 }
207
208 private Resource getParentOfFile(Resource file) {
209 try {
210 return file.createRelative(".");
211 } catch (IOException e) {
212 throw new SlcException("Cannot get parent for resource " + file, e);
213 }
214 }
215 }