]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcAntConfig.java
Introduce directory based structure
[gpl/argeo-slc.git] / org.argeo.slc / 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.Properties;
7 import java.util.StringTokenizer;
8
9 import org.apache.tools.ant.Project;
10
11 /** Load reference to directories from an slcRoot.properties file */
12 public class SlcAntConfig {
13 // SLC ROOT PROPERTIES
14 public final static String ROOT_DIR_PROPERTY = "org.argeo.slc.ant.rootDir";
15 public final static String CONF_DIR_PROPERTY = "org.argeo.slc.ant.confDir";
16 public final static String WORK_DIR_PROPERTY = "org.argeo.slc.ant.workDir";
17 /**
18 * Comma-separated list of property file names to load from the conf dir and
19 * add to project user properties
20 */
21 public final static String PROPERTY_FILE_NAMES_PROPERTY = "org.argeo.slc.ant.propertyFileNames";
22
23 // SLC CONF PROPERTIES
24 /** Path to the root Spring application context */
25 public static String APPLICATION_CONTEXT_PROPERTY = "org.argeo.slc.ant.applicationContext";
26
27 // SLC LOCAL PROPERTIES
28 public static String DIR_DESCRIPTION_PROPERTY = "org.argeo.slc.ant.dirDescription";
29
30 /** Retrieve all properties and set them as project user properties */
31 public static void initProject(Project project, File slcRootFile) {
32 Properties p = loadFile(slcRootFile.getAbsolutePath());
33
34 final File confDir;
35 final File workDir;
36 // Root dir
37 final File rootDir = slcRootFile.getParentFile();
38 project.setUserProperty(ROOT_DIR_PROPERTY, rootDir.getAbsolutePath());
39
40 // Conf dir
41 if (project.getUserProperty(CONF_DIR_PROPERTY) == null) {
42 confDir = new File(p.getProperty(CONF_DIR_PROPERTY, rootDir
43 .getAbsolutePath()
44 + "/../conf")).getAbsoluteFile();
45 project.setUserProperty(CONF_DIR_PROPERTY, confDir
46 .getAbsolutePath());
47 } else {
48 confDir = new File(project.getUserProperty(CONF_DIR_PROPERTY))
49 .getAbsoluteFile();
50 }
51
52 // Work dir
53 if (project.getUserProperty(WORK_DIR_PROPERTY) == null) {
54 workDir = new File(p.getProperty(WORK_DIR_PROPERTY, rootDir
55 .getAbsolutePath()
56 + "/../work")).getAbsoluteFile();
57 project.setUserProperty(WORK_DIR_PROPERTY, workDir
58 .getAbsolutePath());
59 } else {
60 workDir = new File(project.getUserProperty(WORK_DIR_PROPERTY))
61 .getAbsoluteFile();
62 }
63
64 // Properties from the conf dir files
65 Properties properties = new Properties();
66 StringTokenizer st = new StringTokenizer(p.getProperty(
67 PROPERTY_FILE_NAMES_PROPERTY, "slc.properties"), ",");
68 while (st.hasMoreTokens()) {
69 String fileName = st.nextToken();
70 properties.putAll(loadFile(confDir.getAbsolutePath() + "/"
71 + fileName));
72 }
73
74 for (Object o : properties.keySet()) {
75 String key = o.toString();
76 if (project.getUserProperty(key) == null) {// not already set
77 project.setUserProperty(key, properties.getProperty(key));
78 }
79 }
80
81 // Default application context
82 if (project.getUserProperty(APPLICATION_CONTEXT_PROPERTY) == null) {
83 project.setUserProperty(APPLICATION_CONTEXT_PROPERTY, confDir
84 .getAbsolutePath()
85 + "/applicationContext.xml");
86 }
87 }
88
89 public static Properties loadFile(String path) {
90 Properties p = new Properties();
91 try {
92 FileInputStream in = new FileInputStream(path);
93 p.load(in);
94 in.close();
95 } catch (IOException e) {
96 throw new SlcAntException("Cannot read SLC root file", e);
97 }
98 return p;
99 }
100
101 }