X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fant%2FSlcAntConfig.java;h=6085c663531082223c70947cc2c11852170e44e1;hb=ed93fb9db5f9008a78d9ab892806ae398ed65b85;hp=124aa0d216744d5fadb345dd3fe1583904ffb0c3;hpb=a29c28266fef35c1620f6582ec182bad7f7f85ae;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcAntConfig.java b/org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcAntConfig.java index 124aa0d21..6085c6635 100644 --- a/org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcAntConfig.java +++ b/org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcAntConfig.java @@ -3,6 +3,7 @@ package org.argeo.slc.ant; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; @@ -10,12 +11,108 @@ import org.springframework.util.Log4jConfigurer; import org.apache.tools.ant.Project; -/** Load reference to directories from an slcRoot.properties file */ +import org.argeo.slc.core.test.WritableTestRun; + +/** + *
+ * Manager and initializer of the properties required by SLC Ant. + *
+ * + *+ * All properties described here will get a value one way or another (see below + * for details)/ Each property will be accessible via Ant or Spring properties. + *
+ * + *
+ * The property slc.rootFile is set based on the location of the SLC
+ * root property file found in the directory structure of a called Ant file. The
+ * default name of this file is slcRoot.properties (can be set by
+ * {@link #setSlcRootFileName(String)}).
+ * This property provides the absolute path to the unique SLC root property file
+ * which marks the root of an Ant SLC tree structure.
+ *
+ * The property slc.rootDir is inferred from slc.rootFile and + * provides a convenient shortcut to the root directory of the Ant files + * directory structure. + *
+ * + *+ * A few directory and file related properties can be set in the SLC root + * property file (if they are not explicitly set their default values will be + * used): + * + *
Property | + *Description | + *Default | + *
---|---|---|
slc.confDir | + *Directory where to find the various configuration files of a given SLC + * Ant deployment | + *${slc.rootDir}/../conf | + *
slc.workDir | + *Directory where data can be retrieved or generated: build outputs, test + * inputs/outputs, test results, etc. The underlying directory structure is + * specified by the specific SLC application. | + *${slc.rootDir}/../work | + *
slc.propertyFileNames | + *Comma-separated list of the files names of the property files to load + * from the conf directory. Having various files allows to separate between SLC + * framework properties and properties specific to a given application built on + * top of SLC. All will be available across Ant and Spring. | + *slc.properties | + *
+ * Any property can be defined in the conf files defined in the SLC root + * properties file (see above). SLC expects some which will have defaults but + * can be overriden there. By convention they should be defined in the + * slc.properties file, while application specific properties should be + * defined in other conf files. This allows for a clean spearation between SLC + * and the applications built on top of it: + * + *
Property | + *Description | + *Default | + *
---|---|---|
slc.applicationContext | + *Path to the root Spring application context file used by SLC Ant. | + *${slc.confDir}/applicationContext.xml | + *
slc.defaultTestRun | + *Name of the {@link WritableTestRun} Spring bean that the
+ * slc.test task will use by default. This can be overridden when
+ * calling the task from Ant. |
+ * defaultTestRun | + *
Project
being run.
+ * @return whether the project could be initialized for SLC usage (e.g.
+ * presence of an SLC root file)
*/
- public static void initProject(Project project, File slcRootFile) {
- System.getProperties().putAll(project.getUserProperties());
- System.setProperty(ROOT_FILE_PROPERTY, slcRootFile.getAbsolutePath());
- Properties all = prepareAllProperties();
+ public boolean initProject(Project project) {
+ File projectBaseDir = project.getBaseDir();
+ File slcRootFile = findSlcRootFile(projectBaseDir);
+ if (slcRootFile == null) {
+ return false;
+ }
+
+ // pass the project properties through the System properties
+ System.getProperties().putAll((Map, ?>) project.getUserProperties());
+ Properties all = prepareAllProperties(slcRootFile);
for (Object o : all.keySet()) {
String key = o.toString();
if (project.getUserProperty(key) == null) {// not already set
project.setUserProperty(key, all.getProperty(key));
}
}
+ return true;
}
- public static Properties prepareAllProperties() {
+ /**
+ * Retrieves or infers all required properties.
+ *
+ * @param slcRootFile
+ * the location of the SLC root file
+ *
+ * @return the prepared properties. Note that it also contains the System
+ * and Ant properties which had previously been set.
+ */
+ protected Properties prepareAllProperties(File slcRootFile) {
try {
+ final String fileUrlPrefix = "";
+
Properties all = new Properties();
all.putAll(System.getProperties());
+ all.put(ROOT_FILE_PROPERTY, slcRootFile.getCanonicalPath());
- if (all.getProperty(ROOT_FILE_PROPERTY) == null) {
- throw new RuntimeException("System Property "
- + ROOT_FILE_PROPERTY + " has to be set.");
- }
-
- File slcRootFile = new File(all.getProperty(ROOT_FILE_PROPERTY))
- .getAbsoluteFile();
- Properties rootProps = loadFile(slcRootFile.getAbsolutePath());
+ Properties rootProps = loadFile(slcRootFile.getCanonicalPath());
final File confDir;
final File workDir;
// Root dir
final File rootDir = slcRootFile.getParentFile();
- all.setProperty(ROOT_DIR_PROPERTY, rootDir.getCanonicalPath());
+ all.setProperty(ROOT_DIR_PROPERTY, fileUrlPrefix
+ + rootDir.getCanonicalPath());
// Conf dir
if (all.getProperty(CONF_DIR_PROPERTY) == null) {
confDir = new File(rootProps.getProperty(CONF_DIR_PROPERTY,
rootDir.getAbsolutePath() + "/../conf"))
.getCanonicalFile();
- all.setProperty(CONF_DIR_PROPERTY, confDir.getAbsolutePath());
+ all.setProperty(CONF_DIR_PROPERTY, fileUrlPrefix
+ + confDir.getAbsolutePath());
} else {
confDir = new File(all.getProperty(CONF_DIR_PROPERTY))
.getCanonicalFile();
@@ -85,7 +206,8 @@ public class SlcAntConfig {
workDir = new File(rootProps.getProperty(WORK_DIR_PROPERTY,
rootDir.getAbsolutePath() + "/../work"))
.getCanonicalFile();
- all.setProperty(WORK_DIR_PROPERTY, workDir.getAbsolutePath());
+ all.setProperty(WORK_DIR_PROPERTY, fileUrlPrefix
+ + workDir.getAbsolutePath());
} else {
workDir = new File(all.getProperty(WORK_DIR_PROPERTY))
.getCanonicalFile();
@@ -97,8 +219,8 @@ public class SlcAntConfig {
PROPERTY_FILE_NAMES_PROPERTY, "slc.properties"), ",");
while (st.hasMoreTokens()) {
String fileName = st.nextToken();
- properties.putAll(loadFile(confDir.getAbsolutePath() + File.separator
- + fileName));
+ properties.putAll(loadFile(confDir.getAbsolutePath()
+ + File.separator + fileName));
}
for (Object o : properties.keySet()) {
@@ -121,12 +243,11 @@ public class SlcAntConfig {
// Default log4j
if (all.getProperty("log4j.configuration") == null) {
- System.setProperty("log4j.configuration",confDir
+ System.setProperty("log4j.configuration", confDir
.getCanonicalPath()
- + File.separator + "log4j.properties" );
+ + File.separator + "log4j.properties");
// TODO: fix dependency to log4j
- Log4jConfigurer.initLogging(confDir
- .getCanonicalPath()
+ Log4jConfigurer.initLogging(confDir.getCanonicalPath()
+ File.separator + "log4j.properties");
}
@@ -137,7 +258,8 @@ public class SlcAntConfig {
}
}
- public static Properties loadFile(String path) {
+ /** Loads the content of a file as Properties
. */
+ private Properties loadFile(String path) {
Properties p = new Properties();
try {
FileInputStream in = new FileInputStream(path);
@@ -149,4 +271,67 @@ public class SlcAntConfig {
return p;
}
+ /**
+ * Looks for a file named {@link #getSlcLocalFileName()} in the directory,
+ * loads it as properties file and return the value of the property
+ * {@link #DIR_DESCRIPTION_PROPERTY}.
+ */
+ public String getDescriptionForDir(File dir) {
+ String description = dir.getName();
+ File slcLocal = new File(dir.getPath() + File.separator
+ + getSlcLocalFileName());
+ if (slcLocal.exists()) {
+ Properties properties = loadFile(slcLocal.getAbsolutePath());
+ description = properties.getProperty(
+ SlcAntConfig.DIR_DESCRIPTION_PROPERTY, description);
+ }
+ return description;
+ }
+
+ /**
+ * Recursively scans directories downwards until it find a file names as
+ * defined by {@link #getSlcRootFileName()}.
+ */
+ public File findSlcRootFile(File dir) {
+ for (File file : dir.listFiles()) {
+ if (!file.isDirectory()
+ && file.getName().equals(getSlcRootFileName())) {
+ return file;
+ }
+ }
+
+ File parentDir = dir.getParentFile();
+ if (parentDir == null) {
+ return null;// stop condition: not found
+ } else {
+ return findSlcRootFile(parentDir);
+ }
+ }
+
+ /**
+ * Gets the file name of the file marking the root directory, default being
+ * slcRoot.properties.
+ */
+ public String getSlcRootFileName() {
+ return slcRootFileName;
+ }
+
+ /** Sets the file name of the file marking the root directory. */
+ public void setSlcRootFileName(String slcRootFileName) {
+ this.slcRootFileName = slcRootFileName;
+ }
+
+ /**
+ * Gets the file name of the file containing directory specific properties,
+ * default being slcLocal.properties.
+ */
+ public String getSlcLocalFileName() {
+ return slcLocalFileName;
+ }
+
+ /** Sets the file name of the file containing directory specific properties. */
+ public void setSlcLocalFileName(String slcLocalFileName) {
+ this.slcLocalFileName = slcLocalFileName;
+ }
+
}