1 package org
.argeo
.slc
.ant
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
7 import java
.util
.Properties
;
8 import java
.util
.StringTokenizer
;
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
;
18 * Manager and initializer of the properties required by SLC Ant.
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.
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.
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.
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
46 * <table border="1" cellspacing="0">
49 * <th>Description</th>
53 * <td><i>slc.confDir</i></td>
54 * <td>Directory where to find the various configuration files of a given SLC
56 * <td>${slc.rootDir}/../conf</td>
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>
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>
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
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:
86 * <table border="1" cellspacing="0">
89 * <th>Description</th>
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>
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>
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";
112 * Comma-separated list of property file names to load from the conf dir and
113 * add to project user properties
115 public final static String PROPERTY_FILE_NAMES_PROPERTY
= "slc.propertyFileNames";
117 // SLC CONF PROPERTIES
118 /** Path to the root Spring application context */
119 public static String APPLICATION_CONTEXT_PROPERTY
= "slc.applicationContext";
120 // SLC LOCAL PROPERTIES
121 /** Property for the dir label (SLC local property file). */
122 public static String DIR_LABEL_PROPERTY
= "slc.dirLabel";
124 private String slcRootFileName
= "slcRoot.properties";
125 private String slcLocalFileName
= "slcLocal.properties";
128 * Retrieves or infers all properties and set them as project user
129 * properties. All these properties will be set as project properties <b>if
130 * they had not been set as project properties before</b> (like by
131 * overriding through the standard Ant mechanisms).
134 * the Ant <code>Project</code> being run.
135 * @return whether the project could be initialized for SLC usage (e.g.
136 * presence of an SLC root file)
138 public boolean initProject(Project project
) {
139 File projectBaseDir
= project
.getBaseDir();
140 File slcRootFile
= findSlcRootFile(projectBaseDir
);
141 if (slcRootFile
== null) {
145 // pass the project properties through the System properties
146 System
.getProperties().putAll((Map
<?
, ?
>) project
.getUserProperties());
147 Properties all
= new Properties();
148 all
.putAll(System
.getProperties());
149 prepareAllProperties(slcRootFile
, all
);
151 Log log
= LogFactory
.getLog(this.getClass());
152 for (Object o
: all
.keySet()) {
153 String key
= o
.toString();
154 // System.out.println(key+"="+all.getProperty(key));
155 if (project
.getUserProperty(key
) == null) {// not already set
156 // if (log.isDebugEnabled())
157 // log.debug(key + "=" + all.getProperty(key));
158 project
.setUserProperty(key
, all
.getProperty(key
));
165 * Retrieves or infers all required properties.
168 * the location of the SLC root file
170 * @return the prepared properties. Note that it also contains the System
171 * and Ant properties which had previously been set.
173 public void prepareAllProperties(File slcRootFile
, Properties all
) {
175 final String fileUrlPrefix
= "";
177 all
.put(ROOT_FILE_PROPERTY
, slcRootFile
.getCanonicalPath());
178 // Remove basedir property in order to avoid conflict with Maven
179 if (all
.containsKey("basedir"))
180 all
.remove("basedir");
182 Properties rootProps
= loadFile(slcRootFile
.getCanonicalPath());
187 final File rootDir
= slcRootFile
.getParentFile();
188 all
.setProperty(SlcAntConstants
.ROOT_DIR_PROPERTY
, fileUrlPrefix
189 + rootDir
.getCanonicalPath());
192 if (all
.getProperty(SlcAntConstants
.CONF_DIR_PROPERTY
) == null) {
193 confDir
= new File(rootProps
.getProperty(SlcAntConstants
.CONF_DIR_PROPERTY
,
194 rootDir
.getAbsolutePath() + "/../conf"))
196 all
.setProperty(SlcAntConstants
.CONF_DIR_PROPERTY
, fileUrlPrefix
197 + confDir
.getAbsolutePath());
199 confDir
= new File(all
.getProperty(SlcAntConstants
.CONF_DIR_PROPERTY
))
204 if (all
.getProperty(SlcAntConstants
.WORK_DIR_PROPERTY
) == null) {
205 workDir
= new File(rootProps
.getProperty(SlcAntConstants
.WORK_DIR_PROPERTY
,
206 rootDir
.getAbsolutePath() + "/../work"))
208 all
.setProperty(SlcAntConstants
.WORK_DIR_PROPERTY
, fileUrlPrefix
209 + workDir
.getAbsolutePath());
211 workDir
= new File(all
.getProperty(SlcAntConstants
.WORK_DIR_PROPERTY
))
215 // Properties from the conf dir files
216 Properties properties
= new Properties();
217 StringTokenizer st
= new StringTokenizer(rootProps
.getProperty(
218 PROPERTY_FILE_NAMES_PROPERTY
, "slc.properties"), ",");
219 while (st
.hasMoreTokens()) {
220 String fileName
= st
.nextToken();
221 properties
.putAll(loadFile(confDir
.getAbsolutePath()
222 + File
.separator
+ fileName
));
225 for (Object o
: properties
.keySet()) {
226 String key
= o
.toString();
227 if (all
.getProperty(key
) == null) {// not already set
228 all
.setProperty(key
, properties
.getProperty(key
));
232 // Default application context
233 if (all
.getProperty(APPLICATION_CONTEXT_PROPERTY
) == null) {
234 all
.setProperty(APPLICATION_CONTEXT_PROPERTY
, confDir
236 + "/applicationContext.xml");
239 if (all
.getProperty(SlcAntConstants
.DEFAULT_TEST_RUN_PROPERTY
) == null) {
240 all
.setProperty(SlcAntConstants
.DEFAULT_TEST_RUN_PROPERTY
, "defaultTestRun");
244 if (all
.getProperty("log4j.configuration") == null) {
245 System
.setProperty("log4j.configuration", confDir
247 + File
.separator
+ "log4j.properties");
248 // TODO: fix dependency to log4j
249 Log4jConfigurer
.initLogging(confDir
.getCanonicalPath()
250 + File
.separator
+ "log4j.properties");
252 } catch (Exception e
) {
253 throw new SlcAntException("Unexpected exception while configuring",
258 /** Loads the content of a file as <code>Properties</code>. */
259 private Properties
loadFile(String path
) {
260 Properties p
= new Properties();
262 FileInputStream in
= new FileInputStream(path
);
265 } catch (IOException e
) {
266 throw new SlcAntException("Cannot read SLC root file", e
);
272 * Looks for a file named {@link #getSlcLocalFileName()} in the directory,
273 * loads it as properties file and return the value of the property
274 * {@link #DIR_LABEL_PROPERTY}.
276 public String
getDescriptionForDir(File dir
) {
277 String description
= dir
.getName();
278 File slcLocal
= new File(dir
.getPath() + File
.separator
279 + getSlcLocalFileName());
280 if (slcLocal
.exists()) {
281 Properties properties
= loadFile(slcLocal
.getAbsolutePath());
282 description
= properties
.getProperty(
283 SlcAntConfig
.DIR_LABEL_PROPERTY
, description
);
289 * Recursively scans directories downwards until it find a file names as
290 * defined by {@link #getSlcRootFileName()}.
292 public File
findSlcRootFile(File dir
) {
293 for (File file
: dir
.listFiles()) {
294 if (!file
.isDirectory()
295 && file
.getName().equals(getSlcRootFileName())) {
300 File parentDir
= dir
.getParentFile();
301 if (parentDir
== null) {
302 return null;// stop condition: not found
304 return findSlcRootFile(parentDir
);
309 * Gets the file name of the file marking the root directory, default being
310 * <i>slcRoot.properties</i>.
312 public String
getSlcRootFileName() {
313 return slcRootFileName
;
316 /** Sets the file name of the file marking the root directory. */
317 public void setSlcRootFileName(String slcRootFileName
) {
318 this.slcRootFileName
= slcRootFileName
;
322 * Gets the file name of the file containing directory specific properties,
323 * default being <i>slcLocal.properties</i>.
325 public String
getSlcLocalFileName() {
326 return slcLocalFileName
;
329 /** Sets the file name of the file containing directory specific properties. */
330 public void setSlcLocalFileName(String slcLocalFileName
) {
331 this.slcLocalFileName
= slcLocalFileName
;