package mir.config;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
-
import javax.servlet.ServletContext;
import multex.Exc;
import multex.Failure;
-
import org.apache.commons.collections.ExtendedProperties;
-
/**
* @author idefix
*/
public class MirPropertiesConfiguration extends ExtendedProperties {
private static MirPropertiesConfiguration instance;
private static ServletContext context;
- private static String contextPath;
- //if one of these properties is not present a new
- //property is added with its default value;
- private static NeededProperty[] neededWithValue =
- {
- new NeededProperty("Producer.DocRoot", ""),
- new NeededProperty("Producer.ImageRoot", ""),
- new NeededProperty("Producer.Image.Path", ""),
- new NeededProperty("Producer.Media.Path", ""),
- new NeededProperty("Producer.RealMedia.Path", ""),
- new NeededProperty("Producer.Image.IconPath", "")
- };
+ private File home;
/**
* Constructor for MirPropertiesConfiguration.
*/
- private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
- throws IOException {
+ private MirPropertiesConfiguration(ServletContext ctx) throws IOException {
//loading the defaults-config
super(ctx.getRealPath("/WEB-INF/") + "/default.properties");
+
//loading the user-config
ExtendedProperties userConfig =
new ExtendedProperties(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties");
+
//merging them to one config while overriding the defaults
this.combine(userConfig);
- addProperty("Home", ctx.getRealPath("/WEB-INF/") + "/");
- checkMissing();
+
+ home = new File(ctx.getRealPath("/WEB-INF/")+"/");
}
- public static synchronized MirPropertiesConfiguration instance()
- throws PropertiesConfigExc {
+ public static synchronized MirPropertiesConfiguration instance() {
if (instance == null) {
if (context == null) {
- throw new MirPropertiesConfiguration.PropertiesConfigExc(
- "Context was not set");
+ throw new Error("Context was not set");
}
try {
- instance = new MirPropertiesConfiguration(context, contextPath);
+ instance = new MirPropertiesConfiguration(context);
}
catch (IOException e) {
- throw new RuntimeException(e.toString());
+ throw new Error("cannot load configuration: " + e.toString());
}
}
/**
* Sets the context.
+ *
* @param context The context to set
*/
public static void setContext(ServletContext context) {
/**
* Returns the context.
+ *
* @return ServletContext
*/
public static ServletContext getContext() {
String key = (String) iterator.next();
Object o = this.getProperty(key);
- if (o == null) {
- o = new Object();
- }
-
returnMap.put(key, o);
}
+ // ML: hack for now
+ if (!returnMap.containsKey("Producer.DocRoot")) {
+ returnMap.put("Producer.DocRoot", "");
+ }
+
return returnMap;
}
/**
- * Returns a String-property concatenated with the home-dir of the
- * installation
- * @param key
- * @return String
+ * Return mir's home directory.
+ * Normally this is the <code>WEB-INF</code> dir of the
+ * deployed mir servlets.
*/
- public String getStringWithHome(String key) {
- String returnString = getString(key);
-
- if (returnString == null) {
- returnString = new String();
- }
-
- return getString("Home") + returnString;
+ public File getHome() {
+ return home;
}
/**
- * Checks if one property is missing and adds a default value
+ * Returns a file based on a configuration setting.
+ *
+ * The file may be configured with an absolute path, or
+ * it may be a relative path.
+ *
+ * Relative paths work relative to {@link #home} :
+ * normally this is the <code>WEB-INF</code> dir in a
+ * deployed java servlet.
*/
- private void checkMissing() {
- for (int i = 0; i < neededWithValue.length; i++) {
- if (super.getProperty(neededWithValue[i].getKey()) == null) {
- addProperty(neededWithValue[i].getKey(), neededWithValue[i].getValue());
- }
- }
- }
+ public File getFile(String aKey) {
+ String path = getString(aKey);
- public File getFile(String key) throws FileNotFoundException {
- String path = getStringWithHome(key);
- File returnFile = new File(path);
-
- if (returnFile.exists()) {
- return returnFile;
- } else {
- throw new FileNotFoundException();
+ File result = new File(path);
+ if (result.isAbsolute()) {
+ return result;
+ }
+ else {
+ return new File(home, path);
}
}
/**
* @return the vlaue of this property as String
* @param key the key of this property
- * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
*/
public String getString(String key) {
return getString(key, "");
return object.toString();
}
+ public boolean getBoolean(String aKey, boolean aDefaultValue) {
+ try {
+ return getBoolean(aKey);
+ }
+ catch (Throwable t) {
+ return aDefaultValue;
+ }
+ }
+
+ public boolean getBoolean(String aKey) {
+ String value = getString(aKey).trim();
+
+ return "1".equals(value) || "y".equalsIgnoreCase(value) ||
+ "yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value);
+ }
+
/**
* Returns a property according to the given key
* @param key the key of the property
* @return the value of the property as Object, if no property available it returns a empty String
- * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
*/
public Object getProperty(String key) {
if (super.getProperty(key) == null) {
public static class PropertiesConfigExc extends Exc {
/**
* Constructor for PropertiesConfigExc.
- * @param arg0
*/
public PropertiesConfigExc(String msg) {
super(msg);
public static class PropertiesConfigFailure extends Failure {
/**
* Constructor for PropertiesConfigExc.
- * @param arg0
*/
public PropertiesConfigFailure(String msg, Throwable cause) {
super(msg, cause);
}
}
-
- /**
- * A Class for properties to be checked
- * @author idefix
- */
- private static class NeededProperty {
- private String _key;
- private String _value;
-
- public NeededProperty(String key, String value) {
- _key = key;
- _value = value;
- }
-
- public String getKey() {
- return _key;
- }
-
- public String getValue() {
- return _value;
- }
- }
}