tiny fixes here and there
[mir.git] / source / mir / config / MirPropertiesConfiguration.java
index 5441545..125dff0 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2005 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two.  You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries.  If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so.  If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with  any library licensed under the Apache Software License.
+ * You must obey the GNU General Public License in all respects for all of the code used
+ * other than the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
  */
 package mir.config;
 
+import multex.Exc;
+import multex.Failure;
+import org.apache.commons.collections.ExtendedProperties;
+
+import javax.servlet.ServletContext;
 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.configuration.PropertiesConfiguration;
-
-
 /**
  * @author idefix
  */
-public class MirPropertiesConfiguration extends PropertiesConfiguration {
+public class MirPropertiesConfiguration extends ExtendedProperties {
   private static MirPropertiesConfiguration instance;
   private static ServletContext context;
-  private static String contextPath;
-  
-       /**
-        * Constructor for MirPropertiesConfiguration.
-        */
-       private MirPropertiesConfiguration(ServletContext ctx, String ctxPath) 
-                                         throws IOException {
-               super(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties", 
-                     ctx.getRealPath("/WEB-INF/etc/") + "/default.properties");
-    this.addProperty("Home", ctx.getRealPath("/WEB-INF/")+"/");
-    this.addProperty("RootUri", ctxPath);
-       }
-
-  public static synchronized MirPropertiesConfiguration instance()
-       throws PropertiesConfigExc {
-    if(instance == null){
-      if(context == null || contextPath == null || contextPath.length() == 0){
-        throw new MirPropertiesConfiguration.PropertiesConfigExc("Context was not set");
+
+  private File home;
+
+  /**
+   * Constructor for MirPropertiesConfiguration.
+   */
+  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);
+
+    home = new File(ctx.getRealPath("/WEB-INF/")+"/");
+  }
+
+  public static synchronized MirPropertiesConfiguration instance() {
+    if (instance == null) {
+      if (context == null) {
+        throw new Error("Context was not set");
       }
+
       try {
-        instance = new MirPropertiesConfiguration(context,contextPath);
-      } catch (IOException e) {
-        e.printStackTrace();
-      }                                     
+        instance = new MirPropertiesConfiguration(context);
+      }
+      catch (IOException e) {
+        throw new Error("cannot load configuration: " + e.toString());
+      }
     }
+
     return instance;
   }
 
   /**
    * Sets the context.
+   *
    * @param context The context to set
    */
   public static void setContext(ServletContext context) {
     MirPropertiesConfiguration.context = context;
   }
-  
+
   /**
-   * Sets the contextPath.
-   * @param contextPath The contextPath to set
+   * Returns the context.
+   *
+   * @return ServletContext
    */
-  public static void setContextPath(String contextPath) {
-    MirPropertiesConfiguration.contextPath = contextPath;
+  public static ServletContext getContext() {
+    return context;
   }
-  
-  public Map allSettings(){
-               Iterator iterator = this.getKeys();
-               Map returnMap = new HashMap();
-               while(iterator.hasNext()){
-                 String key = (String)iterator.next();
-                       returnMap.put(key,this.getProperty(key));                       
-               }
+
+  /**
+   * Returns all properties in a Map
+   * @return Map
+   */
+  public Map allSettings() {
+    Iterator iterator = this.getKeys();
+    Map returnMap = new HashMap();
+    while (iterator.hasNext()) {
+      String key = (String) iterator.next();
+      Object o = this.getProperty(key);
+
+      returnMap.put(key, o);
+    }
+
+    // ML: hack for now
+    if (!returnMap.containsKey("Producer.DocRoot")) {
+      returnMap.put("Producer.DocRoot", "");
+    }
+
     return returnMap;
   }
 
+  /**
+   * Return mir's home directory.
+   * Normally this is the <code>WEB-INF</code> dir of the
+   * deployed mir servlets.
+   */
+  public File getHome() {
+    return home;
+  }
 
   /**
-   * Returns the context.
-   * @return ServletContext
+   * 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.
    */
-  public static ServletContext getContext() {
-    return context;
+  public File getFile(String aKey) {
+    String path = getString(aKey);
+
+    File result = new File(path);
+    if (result.isAbsolute()) {
+      return result;
+    }
+               return new File(home, path);
   }
-       
-       public String getStringWithHome(String key){
-         String returnString = getString(key);
-         if(returnString == null){
-           returnString = new String();
-         }
-         return getString("Home") + returnString;
-       }
-       
-       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();
-         }
-       }
-       
+
   /**
-   * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
+   * @return the vlaue of this property as String
+   * @param key the key of this property
    */
   public String getString(String key) {
-    if(super.getString(key) == null){
+    return getString(key, "");
+  }
+
+
+  /**
+   * @return the value of this property as String
+   * @param aKey the key of the property
+   * @param aDefaultValue the default value of this property if it is null
+   * @see org.apache.commons.collections.ExtendedProperties#getString(java.lang.String, java.lang.String)
+   */
+  public String getString(String aKey, String aDefaultValue) {
+    if (aDefaultValue == null) {
+      aDefaultValue = "";
+    }
+    Object result = getProperty(aKey);
+
+    if (result == null){
+      return aDefaultValue;
+    }
+
+    if (result instanceof String) {
+      return (String) result;
+    }
+
+    return result.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
+   */
+  public Object getProperty(String key) {
+    if (super.getProperty(key) == null) {
       return new String();
     }
-    return super.getString(key);
+
+    return super.getProperty(key);
   }
-  
+
   /**
    * @author idefix
    */
   public static class PropertiesConfigExc extends Exc {
-
     /**
      * Constructor for PropertiesConfigExc.
-     * @param arg0
      */
     public PropertiesConfigExc(String msg) {
       super(msg);
@@ -161,14 +226,11 @@ public class MirPropertiesConfiguration extends PropertiesConfiguration {
    * @author idefix
    */
   public static class PropertiesConfigFailure extends Failure {
-
     /**
      * Constructor for PropertiesConfigExc.
-     * @param arg0
      */
     public PropertiesConfigFailure(String msg, Throwable cause) {
-      super(msg,cause);
+      super(msg, cause);
     }
   }
-
 }