]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/DefaultSlcRuntime.java
Integrate Slc Execution notification in the new runtime
[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.IOException;
5 import java.io.InputStream;
6 import java.net.InetAddress;
7 import java.net.UnknownHostException;
8 import java.util.Map;
9 import java.util.Properties;
10 import java.util.UUID;
11
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;
24
25 public class DefaultSlcRuntime {
26 private final static Log log = LogFactory.getLog(DefaultSlcRuntime.class);
27
28 public final static String SLC_ROOT_FILE_NAME = "slcRoot.properties";
29
30 public SlcExecutionContext executeScript(String runtimeStr,
31 Resource script, String targets, Properties properties,
32 Map<String, Object> references) {
33
34 Resource slcRootFile = findSlcRootFile(script);
35 String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
36 .getParent(slcRootFile), script);
37
38 SlcExecution slcExecution = createSlcExecution();
39 slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
40 slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_RUNTIME,
41 runtimeStr);
42 slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,
43 scriptRelativePath);
44 if (targets != null)
45 slcExecution.getAttributes().put(
46 SlcAntConstants.EXECATTR_ANT_TARGETS, targets);
47
48 AntSlcApplication application = getApplication(slcRootFile);
49 return application.execute(slcExecution, properties, references);
50 }
51
52 protected SlcExecution createSlcExecution() {
53 SlcExecution slcExecution = new SlcExecution();
54 slcExecution.setUuid(UUID.randomUUID().toString());
55 try {
56 slcExecution.setHost(InetAddress.getLocalHost().getHostName());
57 } catch (UnknownHostException e) {
58 slcExecution.setHost(SlcExecution.UNKOWN_HOST);
59 }
60
61 slcExecution.setType(SlcAntConstants.EXECTYPE_SLC_ANT);
62
63 slcExecution.setUser(System.getProperty("user.name"));
64 return slcExecution;
65 }
66
67 protected AntSlcApplication getApplication(Resource slcRootFile) {
68 AntSlcApplication application = new AntSlcApplication();
69 InputStream inRootFile = null;
70 try {
71 // Remove basedir property in order to avoid conflict with Maven
72 // if (all.containsKey("basedir"))
73 // all.remove("basedir");
74
75 inRootFile = slcRootFile.getInputStream();
76 Properties rootProps = loadFile(inRootFile);
77
78 Resource confDir = null;
79 File workDir = null;
80 // Root dir
81 final Resource rootDir = SpringUtils.getParent(slcRootFile);
82
83 // Conf dir
84 String confDirStr = rootProps
85 .getProperty(SlcAntConstants.CONF_DIR_PROPERTY);
86 if (confDirStr != null)
87 confDir = new DefaultResourceLoader(application.getClass()
88 .getClassLoader()).getResource(confDirStr);
89
90 if (confDir == null || !confDir.exists()) {
91 // confDir = rootDir.createRelative("../conf");
92 confDir = SpringUtils.getParent(rootDir)
93 .createRelative("conf/");
94 }
95
96 // Work dir
97 String workDirStr = rootProps
98 .getProperty(SlcAntConstants.WORK_DIR_PROPERTY);
99 if (workDirStr != null) {
100 workDir = new File(workDirStr);
101 }
102
103 if (workDir == null || !workDir.exists()) {
104 try {
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);
114 }
115 workDir.mkdirs();
116 }
117
118 application.setConfDir(confDir);
119 application.setRootDir(rootDir);
120 application.setWorkDir(workDir);
121
122 return application;
123 } catch (IOException e) {
124 throw new SlcException(
125 "Could not prepare SLC application for root file "
126 + slcRootFile, e);
127 } finally {
128 IOUtils.closeQuietly(inRootFile);
129 }
130 }
131
132 /**
133 * Recursively scans directories downwards until it find a file name as
134 * defined by {@link #SLC_ROOT_FILE_NAME}.
135 */
136 protected Resource findSlcRootFile(Resource currDir) {
137 if (log.isDebugEnabled())
138 log.debug("Look for SLC root file in " + currDir);
139
140 try {
141 Resource slcRootFile = currDir.createRelative(SLC_ROOT_FILE_NAME);
142 if (slcRootFile.exists()) {
143 return slcRootFile;
144 } else {
145 String currPath = currDir.getURL().getPath();
146 if (currPath.equals("/") || currPath.equals("")) {
147 return null;
148 } else {
149 return findSlcRootFile(SpringUtils.getParent(currDir));
150 }
151 }
152 } catch (IOException e) {
153 throw new SlcException("Problem when looking in SLC root file in "
154 + currDir, e);
155 }
156 }
157
158 /** Loads the content of a file as <code>Properties</code>. */
159 private Properties loadFile(InputStream in) {
160 Properties p = new Properties();
161 try {
162 p.load(in);
163 } catch (IOException e) {
164 throw new SlcAntException("Cannot read SLC root file", e);
165 }
166 return p;
167 }
168 }