]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/SlcAntConfig.java
New runtime end to end (not yet working)
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / SlcAntConfig.java
1 package org.argeo.slc.ant;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.Map;
7 import java.util.Properties;
8 import java.util.StringTokenizer;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.tools.ant.Project;
13 import org.argeo.slc.core.test.WritableTestRun;
14 import org.springframework.util.Log4jConfigurer;
15
16 /**
17 * <p>
18 * Manager and initializer of the properties required by SLC Ant.
19 * </p>
20 *
21 * <p>
22 * All properties described here will get a value one way or another (see below
23 * for details)/ Each property will be accessible via Ant or Spring properties.
24 * </p>
25 *
26 * <p>
27 * The property <i>slc.rootFile</i> is set based on the location of the SLC
28 * root property file found in the directory structure of a called Ant file. The
29 * default name of this file is <b>slcRoot.properties</b> (can be set by
30 * {@link #setSlcRootFileName(String)}). <br>
31 * This property provides the absolute path to the unique SLC root property file
32 * which marks the root of an Ant SLC tree structure.
33 * </p>
34 *
35 * <p>
36 * The property <i>slc.rootDir</i> is inferred from <i>slc.rootFile</i> and
37 * provides a convenient shortcut to the root directory of the Ant files
38 * directory structure.
39 * </p>
40 *
41 * <p>
42 * A few directory and file related properties can be set in the SLC root
43 * property file (if they are not explicitly set their default values will be
44 * used):
45 *
46 * <table border="1" cellspacing="0">
47 * <tr>
48 * <th>Property</th>
49 * <th>Description</th>
50 * <th>Default</th>
51 * </tr>
52 * <tr>
53 * <td><i>slc.confDir</i></td>
54 * <td>Directory where to find the various configuration files of a given SLC
55 * Ant deployment</td>
56 * <td>${slc.rootDir}/../conf</td>
57 * </tr>
58 * <tr>
59 * <td><i>slc.workDir</i></td>
60 * <td>Directory where data can be retrieved or generated: build outputs, test
61 * inputs/outputs, test results, etc. The underlying directory structure is
62 * specified by the specific SLC application.</td>
63 * <td>${slc.rootDir}/../work</td>
64 * </tr>
65 * <tr>
66 * <td><i>slc.propertyFileNames</i></td>
67 * <td>Comma-separated list of the files names of the property files to load
68 * from the conf directory. Having various files allows to separate between SLC
69 * framework properties and properties specific to a given application built on
70 * top of SLC. All will be available across Ant and Spring.</td>
71 * <td>slc.properties</td>
72 * </tr>
73 * </table> <b>Note:</b> Only the properties above can be set in the SLC root
74 * properties file. All other properties should be defined in the registered
75 * conf files.
76 * </p>
77 *
78 * <p>
79 * Any property can be defined in the conf files defined in the SLC root
80 * properties file (see above). SLC expects some which will have defaults but
81 * can be overriden there. By convention they should be defined in the
82 * <b>slc.properties</b> file, while application specific properties should be
83 * defined in other conf files. This allows for a clean spearation between SLC
84 * and the applications built on top of it:
85 *
86 * <table border="1" cellspacing="0">
87 * <tr>
88 * <th>Property</th>
89 * <th>Description</th>
90 * <th>Default</th>
91 * </tr>
92 * <tr>
93 * <td><i>slc.applicationContext</i></td>
94 * <td>Path to the root Spring application context file used by SLC Ant.</td>
95 * <td>${slc.confDir}/applicationContext.xml</td>
96 * </tr>
97 * <tr>
98 * <td><i>slc.defaultTestRun</i></td>
99 * <td>Name of the {@link WritableTestRun} Spring bean that the
100 * <code>slc.test</code> task will use by default. This can be overridden when
101 * calling the task from Ant.</td>
102 * <td>defaultTestRun</td>
103 * </tr>
104 * </table>
105 * </p>
106 */
107 public class SlcAntConfig {
108 // SLC ROOT PROPERTIES
109 /** Property for the root file (SLC root property file). */
110 public final static String ROOT_FILE_PROPERTY = "slc.rootFile";
111 /**
112 * Comma-separated list of property file names to load from the conf dir and
113 * add to project user properties
114 */
115 public final static String PROPERTY_FILE_NAMES_PROPERTY = "slc.propertyFileNames";
116
117 // SLC CONF PROPERTIES
118 /** Path to the root Spring application context */
119 public static String APPLICATION_CONTEXT_PROPERTY = "slc.applicationContext";
120 /** Name of the Spring bean used by default */
121 public static String DEFAULT_TEST_RUN_PROPERTY = "slc.defaultTestRun";
122
123 // SLC LOCAL PROPERTIES
124 /** Property for the dir label (SLC local property file). */
125 public static String DIR_LABEL_PROPERTY = "slc.dirLabel";
126
127 private String slcRootFileName = "slcRoot.properties";
128 private String slcLocalFileName = "slcLocal.properties";
129
130 /**
131 * Retrieves or infers all properties and set them as project user
132 * properties. All these properties will be set as project properties <b>if
133 * they had not been set as project properties before</b> (like by
134 * overriding through the standard Ant mechanisms).
135 *
136 * @param project
137 * the Ant <code>Project</code> being run.
138 * @return whether the project could be initialized for SLC usage (e.g.
139 * presence of an SLC root file)
140 */
141 public boolean initProject(Project project) {
142 File projectBaseDir = project.getBaseDir();
143 File slcRootFile = findSlcRootFile(projectBaseDir);
144 if (slcRootFile == null) {
145 return false;
146 }
147
148 // pass the project properties through the System properties
149 System.getProperties().putAll((Map<?, ?>) project.getUserProperties());
150 Properties all = new Properties();
151 all.putAll(System.getProperties());
152 prepareAllProperties(slcRootFile, all);
153
154 Log log = LogFactory.getLog(this.getClass());
155 for (Object o : all.keySet()) {
156 String key = o.toString();
157 // System.out.println(key+"="+all.getProperty(key));
158 if (project.getUserProperty(key) == null) {// not already set
159 // if (log.isDebugEnabled())
160 // log.debug(key + "=" + all.getProperty(key));
161 project.setUserProperty(key, all.getProperty(key));
162 }
163 }
164 return true;
165 }
166
167 /**
168 * Retrieves or infers all required properties.
169 *
170 * @param slcRootFile
171 * the location of the SLC root file
172 *
173 * @return the prepared properties. Note that it also contains the System
174 * and Ant properties which had previously been set.
175 */
176 public void prepareAllProperties(File slcRootFile, Properties all) {
177 try {
178 final String fileUrlPrefix = "";
179
180 all.put(ROOT_FILE_PROPERTY, slcRootFile.getCanonicalPath());
181 // Remove basedir property in order to avoid conflict with Maven
182 if (all.containsKey("basedir"))
183 all.remove("basedir");
184
185 Properties rootProps = loadFile(slcRootFile.getCanonicalPath());
186
187 final File confDir;
188 final File workDir;
189 // Root dir
190 final File rootDir = slcRootFile.getParentFile();
191 all.setProperty(SlcAntConstants.ROOT_DIR_PROPERTY, fileUrlPrefix
192 + rootDir.getCanonicalPath());
193
194 // Conf dir
195 if (all.getProperty(SlcAntConstants.CONF_DIR_PROPERTY) == null) {
196 confDir = new File(rootProps.getProperty(SlcAntConstants.CONF_DIR_PROPERTY,
197 rootDir.getAbsolutePath() + "/../conf"))
198 .getCanonicalFile();
199 all.setProperty(SlcAntConstants.CONF_DIR_PROPERTY, fileUrlPrefix
200 + confDir.getAbsolutePath());
201 } else {
202 confDir = new File(all.getProperty(SlcAntConstants.CONF_DIR_PROPERTY))
203 .getCanonicalFile();
204 }
205
206 // Work dir
207 if (all.getProperty(SlcAntConstants.WORK_DIR_PROPERTY) == null) {
208 workDir = new File(rootProps.getProperty(SlcAntConstants.WORK_DIR_PROPERTY,
209 rootDir.getAbsolutePath() + "/../work"))
210 .getCanonicalFile();
211 all.setProperty(SlcAntConstants.WORK_DIR_PROPERTY, fileUrlPrefix
212 + workDir.getAbsolutePath());
213 } else {
214 workDir = new File(all.getProperty(SlcAntConstants.WORK_DIR_PROPERTY))
215 .getCanonicalFile();
216 }
217
218 // Properties from the conf dir files
219 Properties properties = new Properties();
220 StringTokenizer st = new StringTokenizer(rootProps.getProperty(
221 PROPERTY_FILE_NAMES_PROPERTY, "slc.properties"), ",");
222 while (st.hasMoreTokens()) {
223 String fileName = st.nextToken();
224 properties.putAll(loadFile(confDir.getAbsolutePath()
225 + File.separator + fileName));
226 }
227
228 for (Object o : properties.keySet()) {
229 String key = o.toString();
230 if (all.getProperty(key) == null) {// not already set
231 all.setProperty(key, properties.getProperty(key));
232 }
233 }
234
235 // Default application context
236 if (all.getProperty(APPLICATION_CONTEXT_PROPERTY) == null) {
237 all.setProperty(APPLICATION_CONTEXT_PROPERTY, confDir
238 .getAbsolutePath()
239 + "/applicationContext.xml");
240 }
241 // Default test run
242 if (all.getProperty(DEFAULT_TEST_RUN_PROPERTY) == null) {
243 all.setProperty(DEFAULT_TEST_RUN_PROPERTY, "defaultTestRun");
244 }
245
246 // Default log4j
247 if (all.getProperty("log4j.configuration") == null) {
248 System.setProperty("log4j.configuration", confDir
249 .getCanonicalPath()
250 + File.separator + "log4j.properties");
251 // TODO: fix dependency to log4j
252 Log4jConfigurer.initLogging(confDir.getCanonicalPath()
253 + File.separator + "log4j.properties");
254 }
255 } catch (Exception e) {
256 throw new SlcAntException("Unexpected exception while configuring",
257 e);
258 }
259 }
260
261 /** Loads the content of a file as <code>Properties</code>. */
262 private Properties loadFile(String path) {
263 Properties p = new Properties();
264 try {
265 FileInputStream in = new FileInputStream(path);
266 p.load(in);
267 in.close();
268 } catch (IOException e) {
269 throw new SlcAntException("Cannot read SLC root file", e);
270 }
271 return p;
272 }
273
274 /**
275 * Looks for a file named {@link #getSlcLocalFileName()} in the directory,
276 * loads it as properties file and return the value of the property
277 * {@link #DIR_LABEL_PROPERTY}.
278 */
279 public String getDescriptionForDir(File dir) {
280 String description = dir.getName();
281 File slcLocal = new File(dir.getPath() + File.separator
282 + getSlcLocalFileName());
283 if (slcLocal.exists()) {
284 Properties properties = loadFile(slcLocal.getAbsolutePath());
285 description = properties.getProperty(
286 SlcAntConfig.DIR_LABEL_PROPERTY, description);
287 }
288 return description;
289 }
290
291 /**
292 * Recursively scans directories downwards until it find a file names as
293 * defined by {@link #getSlcRootFileName()}.
294 */
295 public File findSlcRootFile(File dir) {
296 for (File file : dir.listFiles()) {
297 if (!file.isDirectory()
298 && file.getName().equals(getSlcRootFileName())) {
299 return file;
300 }
301 }
302
303 File parentDir = dir.getParentFile();
304 if (parentDir == null) {
305 return null;// stop condition: not found
306 } else {
307 return findSlcRootFile(parentDir);
308 }
309 }
310
311 /**
312 * Gets the file name of the file marking the root directory, default being
313 * <i>slcRoot.properties</i>.
314 */
315 public String getSlcRootFileName() {
316 return slcRootFileName;
317 }
318
319 /** Sets the file name of the file marking the root directory. */
320 public void setSlcRootFileName(String slcRootFileName) {
321 this.slcRootFileName = slcRootFileName;
322 }
323
324 /**
325 * Gets the file name of the file containing directory specific properties,
326 * default being <i>slcLocal.properties</i>.
327 */
328 public String getSlcLocalFileName() {
329 return slcLocalFileName;
330 }
331
332 /** Sets the file name of the file containing directory specific properties. */
333 public void setSlcLocalFileName(String slcLocalFileName) {
334 this.slcLocalFileName = slcLocalFileName;
335 }
336
337 }