the new config-class based on apache.commons.config
authoridfx <idfx>
Sat, 25 Jan 2003 17:40:27 +0000 (17:40 +0000)
committeridfx <idfx>
Sat, 25 Jan 2003 17:40:27 +0000 (17:40 +0000)
source/mir/config/ConfigChecker.java
source/mir/config/ConfigNode.java
source/mir/config/ConfigNodeBuilder.java
source/mir/config/ConfigReader.java
source/mir/config/ConfigSimpleNode.java
source/mir/config/MirConfiguration.java
source/mir/config/MirPropertiesConfiguration.java [new file with mode: 0755]
source/mir/config/exceptions/ConfigFailure.java

index e8be4ea..d74474b 100755 (executable)
 
 package  mir.config;\r
 \r
-import java.util.*;\r
-\r
-import  mir.config.exceptions.*;\r
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Vector;
+
+import mir.config.exceptions.ConfigFailure;
 \r
 public class ConfigChecker {\r
   public final static int STRING = 0;\r
index 762a5d0..40a615e 100755 (executable)
@@ -31,9 +31,7 @@
 
 package  mir.config;\r
 \r
-import java.util.*;\r
-\r
-import  mir.config.exceptions.*;\r
+import mir.config.exceptions.ConfigFailure;
 \r
 public interface ConfigNode {\r
   public String getLocationDescription();\r
index 295d556..94e0f10 100755 (executable)
@@ -31,9 +31,6 @@
 
 package  mir.config;\r
 \r
-import java.util.*;\r
-\r
-import  mir.config.exceptions.*;\r
 \r
 public interface ConfigNodeBuilder {\r
   public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription);\r
index 6d585ee..e1e69da 100755 (executable)
 
 package  mir.config;\r
 \r
-import java.io.*;\r
-import java.util.*;\r
-import java.lang.System;\r
-import org.xml.sax.helpers.DefaultHandler;\r
-import org.xml.sax.*;\r
-import javax.xml.parsers.ParserConfigurationException;\r
-import javax.xml.parsers.SAXParser;\r
-import javax.xml.parsers.SAXParserFactory;\r
-\r
-import mir.config.exceptions.*;\r
-import mir.misc.Location;\r
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Stack;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import mir.config.exceptions.ConfigDefineNotKnownException;
+import mir.config.exceptions.ConfigFailure;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.DefaultHandler;
 \r
 public class ConfigReader {\r
   final static String propertyTagName="property";\r
index 7e6e3c1..70f20ad 100755 (executable)
 
 package  mir.config;\r
 \r
-import java.util.*;\r
-\r
-import mir.config.exceptions.*;\r
+import java.util.HashMap;
+import java.util.Map;
+
+import mir.config.exceptions.ConfigInvalidPropertyTypeException;
+import mir.config.exceptions.ConfigMissingPropertyException;
 \r
 public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder {\r
   private Map properties;\r
index ba55866..769e594 100755 (executable)
@@ -36,7 +36,7 @@ package  mir.config;
 //import  java.util.*;\r
 //import  java.lang.*;\r
 \r
-import mir.config.exceptions.*;\r
+import mir.config.exceptions.ConfigFailure;
 \r
 public class MirConfiguration {\r
   private ConfigNode rootNode;\r
diff --git a/source/mir/config/MirPropertiesConfiguration.java b/source/mir/config/MirPropertiesConfiguration.java
new file mode 100755 (executable)
index 0000000..5441545
--- /dev/null
@@ -0,0 +1,174 @@
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * 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.
+ */
+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.configuration.PropertiesConfiguration;
+
+
+/**
+ * @author idefix
+ */
+public class MirPropertiesConfiguration extends PropertiesConfiguration {
+  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");
+      }
+      try {
+        instance = new MirPropertiesConfiguration(context,contextPath);
+      } catch (IOException e) {
+        e.printStackTrace();
+      }                                     
+    }
+    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
+   */
+  public static void setContextPath(String contextPath) {
+    MirPropertiesConfiguration.contextPath = contextPath;
+  }
+  
+  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));                       
+               }
+    return returnMap;
+  }
+
+
+  /**
+   * Returns the context.
+   * @return ServletContext
+   */
+  public static ServletContext getContext() {
+    return context;
+  }
+       
+       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)
+   */
+  public String getString(String key) {
+    if(super.getString(key) == null){
+      return new String();
+    }
+    return super.getString(key);
+  }
+  
+  /**
+   * @author idefix
+   */
+  public static class PropertiesConfigExc extends Exc {
+
+    /**
+     * Constructor for PropertiesConfigExc.
+     * @param arg0
+     */
+    public PropertiesConfigExc(String msg) {
+      super(msg);
+    }
+  }
+
+  /**
+   * @author idefix
+   */
+  public static class PropertiesConfigFailure extends Failure {
+
+    /**
+     * Constructor for PropertiesConfigExc.
+     * @param arg0
+     */
+    public PropertiesConfigFailure(String msg, Throwable cause) {
+      super(msg,cause);
+    }
+  }
+
+}
index 5c91255..f157650 100755 (executable)
@@ -33,8 +33,6 @@ package mir.config.exceptions;
 
 import multex.Failure;
 
-import java.io.*;
-
 public class ConfigFailure extends Failure {
   private String locationDescription;
   private Throwable cause;