From: idfx Date: Sat, 25 Jan 2003 17:40:27 +0000 (+0000) Subject: the new config-class based on apache.commons.config X-Git-Tag: BEFORE_MERGE_1_1~278 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=a1a1d7f1d09f0b1e61f55f944b2477c98baf7ad1;p=mir.git the new config-class based on apache.commons.config --- diff --git a/source/mir/config/ConfigChecker.java b/source/mir/config/ConfigChecker.java index e8be4ea6..d74474b9 100755 --- a/source/mir/config/ConfigChecker.java +++ b/source/mir/config/ConfigChecker.java @@ -31,9 +31,12 @@ package mir.config; -import java.util.*; - -import mir.config.exceptions.*; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +import mir.config.exceptions.ConfigFailure; public class ConfigChecker { public final static int STRING = 0; diff --git a/source/mir/config/ConfigNode.java b/source/mir/config/ConfigNode.java index 762a5d03..40a615e5 100755 --- a/source/mir/config/ConfigNode.java +++ b/source/mir/config/ConfigNode.java @@ -31,9 +31,7 @@ package mir.config; -import java.util.*; - -import mir.config.exceptions.*; +import mir.config.exceptions.ConfigFailure; public interface ConfigNode { public String getLocationDescription(); diff --git a/source/mir/config/ConfigNodeBuilder.java b/source/mir/config/ConfigNodeBuilder.java index 295d556f..94e0f101 100755 --- a/source/mir/config/ConfigNodeBuilder.java +++ b/source/mir/config/ConfigNodeBuilder.java @@ -31,9 +31,6 @@ package mir.config; -import java.util.*; - -import mir.config.exceptions.*; public interface ConfigNodeBuilder { public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription); diff --git a/source/mir/config/ConfigReader.java b/source/mir/config/ConfigReader.java index 6d585ee0..e1e69da4 100755 --- a/source/mir/config/ConfigReader.java +++ b/source/mir/config/ConfigReader.java @@ -31,17 +31,27 @@ package mir.config; -import java.io.*; -import java.util.*; -import java.lang.System; -import org.xml.sax.helpers.DefaultHandler; -import org.xml.sax.*; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import mir.config.exceptions.*; -import mir.misc.Location; +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; public class ConfigReader { final static String propertyTagName="property"; diff --git a/source/mir/config/ConfigSimpleNode.java b/source/mir/config/ConfigSimpleNode.java index 7e6e3c11..70f20adf 100755 --- a/source/mir/config/ConfigSimpleNode.java +++ b/source/mir/config/ConfigSimpleNode.java @@ -31,9 +31,11 @@ package mir.config; -import java.util.*; - -import mir.config.exceptions.*; +import java.util.HashMap; +import java.util.Map; + +import mir.config.exceptions.ConfigInvalidPropertyTypeException; +import mir.config.exceptions.ConfigMissingPropertyException; public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder { private Map properties; diff --git a/source/mir/config/MirConfiguration.java b/source/mir/config/MirConfiguration.java index ba558667..769e5943 100755 --- a/source/mir/config/MirConfiguration.java +++ b/source/mir/config/MirConfiguration.java @@ -36,7 +36,7 @@ package mir.config; //import java.util.*; //import java.lang.*; -import mir.config.exceptions.*; +import mir.config.exceptions.ConfigFailure; public class MirConfiguration { private ConfigNode rootNode; diff --git a/source/mir/config/MirPropertiesConfiguration.java b/source/mir/config/MirPropertiesConfiguration.java new file mode 100755 index 00000000..54415450 --- /dev/null +++ b/source/mir/config/MirPropertiesConfiguration.java @@ -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); + } + } + +} diff --git a/source/mir/config/exceptions/ConfigFailure.java b/source/mir/config/exceptions/ConfigFailure.java index 5c91255b..f1576501 100755 --- a/source/mir/config/exceptions/ConfigFailure.java +++ b/source/mir/config/exceptions/ConfigFailure.java @@ -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;