1 package org
.argeo
.slc
.ant
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.util
.Properties
;
7 import java
.util
.StringTokenizer
;
9 import org
.springframework
.util
.Log4jConfigurer
;
11 import org
.apache
.tools
.ant
.Project
;
13 /** Load reference to directories from an slcRoot.properties file */
14 public class SlcAntConfig
{
15 // SLC ROOT PROPERTIES
16 public final static String ROOT_FILE_PROPERTY
= "slc.rootFile";
17 public final static String ROOT_DIR_PROPERTY
= "slc.rootDir";
18 public final static String CONF_DIR_PROPERTY
= "slc.confDir";
19 public final static String WORK_DIR_PROPERTY
= "slc.workDir";
21 * Comma-separated list of property file names to load from the conf dir and
22 * add to project user properties
24 public final static String PROPERTY_FILE_NAMES_PROPERTY
= "slc.propertyFileNames";
26 // SLC CONF PROPERTIES
27 /** Path to the root Spring application context */
28 public static String APPLICATION_CONTEXT_PROPERTY
= "slc.applicationContext";
29 /** Name of the Spring bean used by default */
30 public static String DEFAULT_TEST_RUN_PROPERTY
= "slc.defaultTestRun";
32 // SLC LOCAL PROPERTIES
33 public static String DIR_DESCRIPTION_PROPERTY
= "slc.dirDescription";
36 * Retrieve all properties and set them as project user properties. Root
37 * properties (that is from slcRoot file) are added to System properties
38 * (e.g. in order to be used by Spring)
40 public static void initProject(Project project
, File slcRootFile
) {
41 System
.getProperties().putAll(project
.getUserProperties());
42 System
.setProperty(ROOT_FILE_PROPERTY
, slcRootFile
.getAbsolutePath());
43 Properties all
= prepareAllProperties();
44 for (Object o
: all
.keySet()) {
45 String key
= o
.toString();
46 if (project
.getUserProperty(key
) == null) {// not already set
47 project
.setUserProperty(key
, all
.getProperty(key
));
52 public static Properties
prepareAllProperties() {
54 Properties all
= new Properties();
55 all
.putAll(System
.getProperties());
57 if (all
.getProperty(ROOT_FILE_PROPERTY
) == null) {
58 throw new RuntimeException("System Property "
59 + ROOT_FILE_PROPERTY
+ " has to be set.");
62 File slcRootFile
= new File(all
.getProperty(ROOT_FILE_PROPERTY
))
64 Properties rootProps
= loadFile(slcRootFile
.getAbsolutePath());
69 final File rootDir
= slcRootFile
.getParentFile();
70 all
.setProperty(ROOT_DIR_PROPERTY
, rootDir
.getCanonicalPath());
73 if (all
.getProperty(CONF_DIR_PROPERTY
) == null) {
74 confDir
= new File(rootProps
.getProperty(CONF_DIR_PROPERTY
,
75 rootDir
.getAbsolutePath() + "/../conf"))
77 all
.setProperty(CONF_DIR_PROPERTY
, confDir
.getAbsolutePath());
79 confDir
= new File(all
.getProperty(CONF_DIR_PROPERTY
))
84 if (all
.getProperty(WORK_DIR_PROPERTY
) == null) {
85 workDir
= new File(rootProps
.getProperty(WORK_DIR_PROPERTY
,
86 rootDir
.getAbsolutePath() + "/../work"))
88 all
.setProperty(WORK_DIR_PROPERTY
, workDir
.getAbsolutePath());
90 workDir
= new File(all
.getProperty(WORK_DIR_PROPERTY
))
94 // Properties from the conf dir files
95 Properties properties
= new Properties();
96 StringTokenizer st
= new StringTokenizer(rootProps
.getProperty(
97 PROPERTY_FILE_NAMES_PROPERTY
, "slc.properties"), ",");
98 while (st
.hasMoreTokens()) {
99 String fileName
= st
.nextToken();
100 properties
.putAll(loadFile(confDir
.getAbsolutePath() + File
.separator
104 for (Object o
: properties
.keySet()) {
105 String key
= o
.toString();
106 if (all
.getProperty(key
) == null) {// not already set
107 all
.setProperty(key
, properties
.getProperty(key
));
111 // Default application context
112 if (all
.getProperty(APPLICATION_CONTEXT_PROPERTY
) == null) {
113 all
.setProperty(APPLICATION_CONTEXT_PROPERTY
, confDir
115 + "/applicationContext.xml");
118 if (all
.getProperty(DEFAULT_TEST_RUN_PROPERTY
) == null) {
119 all
.setProperty(DEFAULT_TEST_RUN_PROPERTY
, "defaultTestRun");
123 if (all
.getProperty("log4j.configuration") == null) {
124 System
.setProperty("log4j.configuration",confDir
126 + File
.separator
+ "log4j.properties" );
127 // TODO: fix dependency to log4j
128 Log4jConfigurer
.initLogging(confDir
130 + File
.separator
+ "log4j.properties");
134 } catch (Exception e
) {
135 throw new SlcAntException("Unexpected exception while configuring",
140 public static Properties
loadFile(String path
) {
141 Properties p
= new Properties();
143 FileInputStream in
= new FileInputStream(path
);
146 } catch (IOException e
) {
147 throw new SlcAntException("Cannot read SLC root file", e
);