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
.springframework
.util
.Log4jConfigurer
;
12 import org
.apache
.tools
.ant
.Project
;
14 import org
.argeo
.slc
.core
.test
.WritableTestRun
;
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 public final static String ROOT_FILE_PROPERTY
= "slc.rootFile";
110 public final static String ROOT_DIR_PROPERTY
= "slc.rootDir";
111 public final static String CONF_DIR_PROPERTY
= "slc.confDir";
112 public final static String WORK_DIR_PROPERTY
= "slc.workDir";
114 * Comma-separated list of property file names to load from the conf dir and
115 * add to project user properties
117 public final static String PROPERTY_FILE_NAMES_PROPERTY
= "slc.propertyFileNames";
119 // SLC CONF PROPERTIES
120 /** Path to the root Spring application context */
121 public static String APPLICATION_CONTEXT_PROPERTY
= "slc.applicationContext";
122 /** Name of the Spring bean used by default */
123 public static String DEFAULT_TEST_RUN_PROPERTY
= "slc.defaultTestRun";
125 // SLC LOCAL PROPERTIES
126 public static String DIR_DESCRIPTION_PROPERTY
= "slc.dirDescription";
128 private String slcRootFileName
= "slcRoot.properties";
129 private String slcLocalFileName
= "slcLocal.properties";
132 * Retrieves or infers all properties and set them as project user
133 * properties. All these properties will be set as project properties <b>if
134 * they had not been set as project properties before</b> (like by
135 * overriding through the standard Ant mechanisms).
138 * the Ant <code>Project</code> being run.
139 * @return whether the project could be initialized for SLC usage (e.g.
140 * presence of an SLC root file)
142 public boolean initProject(Project project
) {
143 File projectBaseDir
= project
.getBaseDir();
144 File slcRootFile
= findSlcRootFile(projectBaseDir
);
145 if (slcRootFile
== null) {
149 // pass the project properties through the System properties
150 System
.getProperties().putAll((Map
<?
, ?
>) project
.getUserProperties());
151 Properties all
= prepareAllProperties(slcRootFile
);
152 for (Object o
: all
.keySet()) {
153 String key
= o
.toString();
154 if (project
.getUserProperty(key
) == null) {// not already set
155 project
.setUserProperty(key
, all
.getProperty(key
));
162 * Retrieves or infers all required properties.
165 * the location of the SLC root file
167 * @return the prepared properties. Note that it also contains the System
168 * and Ant properties which had previously been set.
170 protected Properties
prepareAllProperties(File slcRootFile
) {
172 final String fileUrlPrefix
= "";
174 Properties all
= new Properties();
175 all
.putAll(System
.getProperties());
176 all
.put(ROOT_FILE_PROPERTY
, slcRootFile
.getCanonicalPath());
178 Properties rootProps
= loadFile(slcRootFile
.getCanonicalPath());
183 final File rootDir
= slcRootFile
.getParentFile();
184 all
.setProperty(ROOT_DIR_PROPERTY
, fileUrlPrefix
185 + rootDir
.getCanonicalPath());
188 if (all
.getProperty(CONF_DIR_PROPERTY
) == null) {
189 confDir
= new File(rootProps
.getProperty(CONF_DIR_PROPERTY
,
190 rootDir
.getAbsolutePath() + "/../conf"))
192 all
.setProperty(CONF_DIR_PROPERTY
, fileUrlPrefix
193 + confDir
.getAbsolutePath());
195 confDir
= new File(all
.getProperty(CONF_DIR_PROPERTY
))
200 if (all
.getProperty(WORK_DIR_PROPERTY
) == null) {
201 workDir
= new File(rootProps
.getProperty(WORK_DIR_PROPERTY
,
202 rootDir
.getAbsolutePath() + "/../work"))
204 all
.setProperty(WORK_DIR_PROPERTY
, fileUrlPrefix
205 + workDir
.getAbsolutePath());
207 workDir
= new File(all
.getProperty(WORK_DIR_PROPERTY
))
211 // Properties from the conf dir files
212 Properties properties
= new Properties();
213 StringTokenizer st
= new StringTokenizer(rootProps
.getProperty(
214 PROPERTY_FILE_NAMES_PROPERTY
, "slc.properties"), ",");
215 while (st
.hasMoreTokens()) {
216 String fileName
= st
.nextToken();
217 properties
.putAll(loadFile(confDir
.getAbsolutePath()
218 + File
.separator
+ fileName
));
221 for (Object o
: properties
.keySet()) {
222 String key
= o
.toString();
223 if (all
.getProperty(key
) == null) {// not already set
224 all
.setProperty(key
, properties
.getProperty(key
));
228 // Default application context
229 if (all
.getProperty(APPLICATION_CONTEXT_PROPERTY
) == null) {
230 all
.setProperty(APPLICATION_CONTEXT_PROPERTY
, confDir
232 + "/applicationContext.xml");
235 if (all
.getProperty(DEFAULT_TEST_RUN_PROPERTY
) == null) {
236 all
.setProperty(DEFAULT_TEST_RUN_PROPERTY
, "defaultTestRun");
240 if (all
.getProperty("log4j.configuration") == null) {
241 System
.setProperty("log4j.configuration", confDir
243 + File
.separator
+ "log4j.properties");
244 // TODO: fix dependency to log4j
245 Log4jConfigurer
.initLogging(confDir
.getCanonicalPath()
246 + File
.separator
+ "log4j.properties");
250 } catch (Exception e
) {
251 throw new SlcAntException("Unexpected exception while configuring",
256 /** Loads the content of a file as <code>Properties</code>. */
257 private Properties
loadFile(String path
) {
258 Properties p
= new Properties();
260 FileInputStream in
= new FileInputStream(path
);
263 } catch (IOException e
) {
264 throw new SlcAntException("Cannot read SLC root file", e
);
270 * Looks for a file named {@link #getSlcLocalFileName()} in the directory,
271 * loads it as properties file and return the value of the property
272 * {@link #DIR_DESCRIPTION_PROPERTY}.
274 public String
getDescriptionForDir(File dir
) {
275 String description
= dir
.getName();
276 File slcLocal
= new File(dir
.getPath() + File
.separator
277 + getSlcLocalFileName());
278 if (slcLocal
.exists()) {
279 Properties properties
= loadFile(slcLocal
.getAbsolutePath());
280 description
= properties
.getProperty(
281 SlcAntConfig
.DIR_DESCRIPTION_PROPERTY
, description
);
287 * Recursively scans directories downwards until it find a file names as
288 * defined by {@link #getSlcRootFileName()}.
290 public File
findSlcRootFile(File dir
) {
291 for (File file
: dir
.listFiles()) {
292 if (!file
.isDirectory()
293 && file
.getName().equals(getSlcRootFileName())) {
298 File parentDir
= dir
.getParentFile();
299 if (parentDir
== null) {
300 return null;// stop condition: not found
302 return findSlcRootFile(parentDir
);
307 * Gets the file name of the file marking the root directory, default being
308 * <i>slcRoot.properties</i>.
310 public String
getSlcRootFileName() {
311 return slcRootFileName
;
314 /** Sets the file name of the file marking the root directory. */
315 public void setSlcRootFileName(String slcRootFileName
) {
316 this.slcRootFileName
= slcRootFileName
;
320 * Gets the file name of the file containing directory specific properties,
321 * default being <i>slcLocal.properties</i>.
323 public String
getSlcLocalFileName() {
324 return slcLocalFileName
;
327 /** Sets the file name of the file containing directory specific properties. */
328 public void setSlcLocalFileName(String slcLocalFileName
) {
329 this.slcLocalFileName
= slcLocalFileName
;