X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fconfig%2FMirPropertiesConfiguration.java;fp=source%2Fmir%2Fconfig%2FMirPropertiesConfiguration.java;h=80ece21690339b49c775075f01c4de8bfdfc4f76;hb=2b0e1c1d91eea7e201af61e1065ad12bf966d1ba;hp=2d5c038a8807458c55e5033e81cd2e1955277a63;hpb=44425d18d2bb421e09e4add11c20f2c8d2129d26;p=mir.git diff --git a/source/mir/config/MirPropertiesConfiguration.java b/source/mir/config/MirPropertiesConfiguration.java index 2d5c038a..80ece216 100755 --- a/source/mir/config/MirPropertiesConfiguration.java +++ b/source/mir/config/MirPropertiesConfiguration.java @@ -30,69 +30,53 @@ 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()); } } @@ -101,6 +85,7 @@ public class MirPropertiesConfiguration extends ExtendedProperties { /** * Sets the context. + * * @param context The context to set */ public static void setContext(ServletContext context) { @@ -109,6 +94,7 @@ public class MirPropertiesConfiguration extends ExtendedProperties { /** * Returns the context. + * * @return ServletContext */ public static ServletContext getContext() { @@ -126,58 +112,51 @@ public class MirPropertiesConfiguration extends ExtendedProperties { 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 WEB-INF 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 WEB-INF 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, ""); @@ -204,11 +183,26 @@ public class MirPropertiesConfiguration extends ExtendedProperties { 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) { @@ -224,7 +218,6 @@ public class MirPropertiesConfiguration extends ExtendedProperties { public static class PropertiesConfigExc extends Exc { /** * Constructor for PropertiesConfigExc. - * @param arg0 */ public PropertiesConfigExc(String msg) { super(msg); @@ -237,32 +230,9 @@ public class MirPropertiesConfiguration extends ExtendedProperties { 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; - } - } }