]> git.argeo.org Git - gpl/argeo-slc.git/blob - SlcAntConfig.java
124aa0d216744d5fadb345dd3fe1583904ffb0c3
[gpl/argeo-slc.git] / 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.Properties;
7 import java.util.StringTokenizer;
8
9 import org.springframework.util.Log4jConfigurer;
10
11 import org.apache.tools.ant.Project;
12
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";
20 /**
21 * Comma-separated list of property file names to load from the conf dir and
22 * add to project user properties
23 */
24 public final static String PROPERTY_FILE_NAMES_PROPERTY = "slc.propertyFileNames";
25
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";
31
32 // SLC LOCAL PROPERTIES
33 public static String DIR_DESCRIPTION_PROPERTY = "slc.dirDescription";
34
35 /**
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)
39 */
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));
48 }
49 }
50 }
51
52 public static Properties prepareAllProperties() {
53 try {
54 Properties all = new Properties();
55 all.putAll(System.getProperties());
56
57 if (all.getProperty(ROOT_FILE_PROPERTY) == null) {
58 throw new RuntimeException("System Property "
59 + ROOT_FILE_PROPERTY + " has to be set.");
60 }
61
62 File slcRootFile = new File(all.getProperty(ROOT_FILE_PROPERTY))
63 .getAbsoluteFile();
64 Properties rootProps = loadFile(slcRootFile.getAbsolutePath());
65
66 final File confDir;
67 final File workDir;
68 // Root dir
69 final File rootDir = slcRootFile.getParentFile();
70 all.setProperty(ROOT_DIR_PROPERTY, rootDir.getCanonicalPath());
71
72 // Conf dir
73 if (all.getProperty(CONF_DIR_PROPERTY) == null) {
74 confDir = new File(rootProps.getProperty(CONF_DIR_PROPERTY,
75 rootDir.getAbsolutePath() + "/../conf"))
76 .getCanonicalFile();
77 all.setProperty(CONF_DIR_PROPERTY, confDir.getAbsolutePath());
78 } else {
79 confDir = new File(all.getProperty(CONF_DIR_PROPERTY))
80 .getCanonicalFile();
81 }
82
83 // Work dir
84 if (all.getProperty(WORK_DIR_PROPERTY) == null) {
85 workDir = new File(rootProps.getProperty(WORK_DIR_PROPERTY,
86 rootDir.getAbsolutePath() + "/../work"))
87 .getCanonicalFile();
88 all.setProperty(WORK_DIR_PROPERTY, workDir.getAbsolutePath());
89 } else {
90 workDir = new File(all.getProperty(WORK_DIR_PROPERTY))
91 .getCanonicalFile();
92 }
93
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
101 + fileName));
102 }
103
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));
108 }
109 }
110
111 // Default application context
112 if (all.getProperty(APPLICATION_CONTEXT_PROPERTY) == null) {
113 all.setProperty(APPLICATION_CONTEXT_PROPERTY, confDir
114 .getAbsolutePath()
115 + "/applicationContext.xml");
116 }
117 // Default test run
118 if (all.getProperty(DEFAULT_TEST_RUN_PROPERTY) == null) {
119 all.setProperty(DEFAULT_TEST_RUN_PROPERTY, "defaultTestRun");
120 }
121
122 // Default log4j
123 if (all.getProperty("log4j.configuration") == null) {
124 System.setProperty("log4j.configuration",confDir
125 .getCanonicalPath()
126 + File.separator + "log4j.properties" );
127 // TODO: fix dependency to log4j
128 Log4jConfigurer.initLogging(confDir
129 .getCanonicalPath()
130 + File.separator + "log4j.properties");
131 }
132
133 return all;
134 } catch (Exception e) {
135 throw new SlcAntException("Unexpected exception while configuring",
136 e);
137 }
138 }
139
140 public static Properties loadFile(String path) {
141 Properties p = new Properties();
142 try {
143 FileInputStream in = new FileInputStream(path);
144 p.load(in);
145 in.close();
146 } catch (IOException e) {
147 throw new SlcAntException("Cannot read SLC root file", e);
148 }
149 return p;
150 }
151
152 }