You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ep...@apache.org on 2004/01/30 15:46:37 UTC

cvs commit: jakarta-commons/configuration/src/java/org/apache/commons/configuration ConfigurationException.java HierarchicalDOM4JConfiguration.java PropertiesConfiguration.java DOM4JConfiguration.java BasePropertiesConfiguration.java ConfigurationFactory.java ClassPropertiesConfiguration.java

epugh       2004/01/30 06:46:37

  Modified:    configuration/src/test/org/apache/commons/configuration
                        TestConfigurationFactory.java
               configuration/src/java/org/apache/commons/configuration
                        HierarchicalDOM4JConfiguration.java
                        PropertiesConfiguration.java
                        DOM4JConfiguration.java
                        BasePropertiesConfiguration.java
                        ConfigurationFactory.java
                        ClassPropertiesConfiguration.java
  Added:       configuration/src/java/org/apache/commons/configuration
                        ConfigurationException.java
  Removed:     configuration/src/java/org/apache/commons/configuration/exception
                        ConfigurationLoadException.java
  Log:
  Throw ConfigurationException instead of Exception or IOException on public methods
  
  Revision  Changes    Path
  1.5       +2 -3      jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
  
  Index: TestConfigurationFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationFactory.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestConfigurationFactory.java	23 Jan 2004 11:52:36 -0000	1.4
  +++ TestConfigurationFactory.java	30 Jan 2004 14:46:37 -0000	1.5
  @@ -59,7 +59,6 @@
   
   import junit.framework.TestCase;
   
  -import org.apache.commons.configuration.exception.ConfigurationLoadException;
   import org.xml.sax.SAXParseException;
   
   /**
  @@ -187,7 +186,7 @@
       		Configuration c = configurationFactory.getConfiguration();
       		fail("Should have throw an Exception");
       	}
  -    	catch (ConfigurationLoadException cle){
  +    	catch (ConfigurationException cle){
       		assertTrue(cle.getCause() instanceof SAXParseException);
       	}
       }
  
  
  
  1.2       +17 -5     jakarta-commons/configuration/src/java/org/apache/commons/configuration/HierarchicalDOM4JConfiguration.java
  
  Index: HierarchicalDOM4JConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/HierarchicalDOM4JConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HierarchicalDOM4JConfiguration.java	23 Dec 2003 15:09:05 -0000	1.1
  +++ HierarchicalDOM4JConfiguration.java	30 Jan 2004 14:46:37 -0000	1.2
  @@ -54,11 +54,13 @@
    * <http://www.apache.org/>.
    */
   
  +import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Iterator;
   
   import org.dom4j.Attribute;
   import org.dom4j.Document;
  +import org.dom4j.DocumentException;
   import org.dom4j.Element;
   import org.dom4j.io.SAXReader;
   
  @@ -142,9 +144,14 @@
        * been specified before.
        * @throws Exception if an error occurs
        */
  -    public void load() throws Exception
  +    public void load() throws ConfigurationException
       {
  -        load(ConfigurationUtils.getURL(getBasePath(), getFileName()));
  +    	try {
  +    		load(ConfigurationUtils.getURL(getBasePath(), getFileName()));
  +    	}
  +    	catch (MalformedURLException mue){
  +    		throw new ConfigurationException("Could not load from " + getBasePath() + ", " + getFileName());
  +    	}
       }
   
       /**
  @@ -152,9 +159,14 @@
        * @param url the URL to the XML document
        * @throws Exception if an error occurs
        */
  -    public void load(URL url) throws Exception
  +    public void load(URL url) throws ConfigurationException
       {
  -        initProperties(new SAXReader().read(url));
  +    	try {
  +    		initProperties(new SAXReader().read(url));
  +    	}
  +    	catch (DocumentException de){
  +    		throw new ConfigurationException("Could not load from " + url);
  +    	}
       }
   
       /**
  
  
  
  1.2       +14 -9     jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java
  
  Index: PropertiesConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PropertiesConfiguration.java	23 Dec 2003 15:09:05 -0000	1.1
  +++ PropertiesConfiguration.java	30 Jan 2004 14:46:37 -0000	1.2
  @@ -127,7 +127,7 @@
        * @param defaults Configuration defaults to use if key not in file
        * @throws IOException Error while loading the properties file
        */
  -    public PropertiesConfiguration(Configuration defaults) throws IOException
  +    public PropertiesConfiguration(Configuration defaults)
       {
           this();
           this.defaults = defaults;
  @@ -141,7 +141,7 @@
        * @param fileName The name of the Properties File to load.
        * @throws IOException Error while loading the properties file
        */
  -    public PropertiesConfiguration(String fileName) throws IOException
  +    public PropertiesConfiguration(String fileName) throws ConfigurationException
       {
   
           load(fileName);
  @@ -152,7 +152,7 @@
        *
        * @throws IOException
        */
  -    public void load() throws IOException
  +    public void load() throws ConfigurationException
       {
           load(getFileName());
       }
  @@ -163,9 +163,14 @@
        * @param fileName A properties file to load
        * @throws IOException
        */
  -    public void load(String fileName) throws IOException
  +    public void load(String fileName) throws ConfigurationException
       {
  -        load(getPropertyStream(fileName));
  +    	try {
  +    		load(getPropertyStream(fileName));
  +    	}
  +    	catch (IOException ioe){
  +    		throw new ConfigurationException("Could not load from file " + fileName,ioe);
  +    	}
       }
   
       /**
  @@ -175,7 +180,7 @@
        * @param defaults Configuration defaults to use if key not in file
        * @throws IOException Error while loading the properties file
        */
  -    public PropertiesConfiguration(String file, Configuration defaults) throws IOException
  +    public PropertiesConfiguration(String file, Configuration defaults) throws ConfigurationException
       {
           this(file);
           this.defaults = defaults;
  @@ -189,7 +194,7 @@
        *                    should be used if a key is not in the file.
        * @throws IOException Error while loading the properties file
        */
  -    public PropertiesConfiguration(String file, String defaultFile) throws IOException
  +    public PropertiesConfiguration(String file, String defaultFile) throws ConfigurationException
       {
           this(file);
           if (StringUtils.isNotEmpty(defaultFile))
  @@ -206,7 +211,7 @@
        * @return An Input Stream
        * @throws IOException Error while loading the properties file
        */
  -    public InputStream getPropertyStream(String resourceName) throws IOException
  +    protected InputStream getPropertyStream(String resourceName) throws IOException
       {
           InputStream resource = null;
           URL url = null;
  
  
  
  1.2       +26 -8     jakarta-commons/configuration/src/java/org/apache/commons/configuration/DOM4JConfiguration.java
  
  Index: DOM4JConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/DOM4JConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DOM4JConfiguration.java	23 Dec 2003 15:09:05 -0000	1.1
  +++ DOM4JConfiguration.java	30 Jan 2004 14:46:37 -0000	1.2
  @@ -59,12 +59,14 @@
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.OutputStream;
  +import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Iterator;
   import java.util.List;
   
   import org.dom4j.Attribute;
   import org.dom4j.Document;
  +import org.dom4j.DocumentException;
   import org.dom4j.Element;
   import org.dom4j.io.OutputFormat;
   import org.dom4j.io.SAXReader;
  @@ -132,7 +134,7 @@
        * @exception Exception If error reading data source.
        * @see DOM4JConfiguration(File)
        */
  -    public DOM4JConfiguration(String resource) throws Exception
  +    public DOM4JConfiguration(String resource) throws ConfigurationException
       {
           setFile(resourceURLToFile(resource));
           load();
  @@ -144,17 +146,25 @@
        * @param file File object representing the XML file.
        * @exception Exception If error reading data source.
        */
  -    public DOM4JConfiguration(File file) throws Exception
  +    public DOM4JConfiguration(File file) throws ConfigurationException
       {
           setFile(file);
           load();
       }
   
  -    public void load() throws Exception
  +    public void load() throws ConfigurationException
       {
   
  -        document = new SAXReader().read(
  -        ConfigurationUtils.getURL(getBasePath(), getFileName()));
  +    	try {
  +    		document = new SAXReader().read(
  +    				ConfigurationUtils.getURL(getBasePath(), getFileName()));
  +    	}
  +    	catch (MalformedURLException mue){
  +    		throw new ConfigurationException("Could not load from " + getBasePath() + ", " + getFileName());
  +    	}    	
  +    	catch (DocumentException de){
  +    		throw new ConfigurationException("Could not load from " + getBasePath() + ", " + getFileName());
  +    	}
           initProperties(document.getRootElement(), new StringBuffer());
   
       }
  @@ -327,9 +337,9 @@
               {
                   save();
               }
  -            catch (IOException e)
  +            catch (ConfigurationException ce)
               {
  -                throw new NestableRuntimeException("Failed to auto-save", e);
  +                throw new NestableRuntimeException("Failed to auto-save", ce);
               }
           }
       }
  @@ -343,7 +353,7 @@
           this.autoSave = autoSave;
       }
   
  -    public synchronized void save() throws IOException
  +    public synchronized void save() throws ConfigurationException
       {
           XMLWriter writer = null;
           OutputStream out = null;
  @@ -354,8 +364,12 @@
               writer = new XMLWriter(out, outputter);
               writer.write(document);
           }
  +        catch (IOException ioe){
  +        	throw new ConfigurationException("Could not save to " + getFile());
  +        }
           finally
           {
  +        	try {
               if (out != null)
               {
                   out.close();
  @@ -365,6 +379,10 @@
               {
                   writer.close();
               }
  +        	}
  +        	catch (IOException ioe){
  +        		throw new ConfigurationException(ioe);
  +        	}
           }
       }
       /**
  
  
  
  1.3       +24 -15    jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java
  
  Index: BasePropertiesConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- BasePropertiesConfiguration.java	30 Jan 2004 14:05:01 -0000	1.2
  +++ BasePropertiesConfiguration.java	30 Jan 2004 14:46:37 -0000	1.3
  @@ -190,7 +190,7 @@
        * @throws IOException
        */
       public void load(InputStream input)
  -        throws IOException
  +        throws ConfigurationException
       {
           load(input, null);
       }
  @@ -204,7 +204,7 @@
        * @exception IOException
        */
       public synchronized void load(InputStream input, String enc)
  -        throws IOException
  +        throws ConfigurationException
       {
           PropertiesReader reader = null;
           if (enc != null)
  @@ -224,7 +224,7 @@
           {
               reader = new PropertiesReader(new InputStreamReader(input));
           }
  -
  +        try {
           while (true)
           {
               String line = reader.readProperty();
  @@ -263,6 +263,10 @@
                   }
               }
           }
  +        }
  +        catch (IOException ioe){
  +        	throw new ConfigurationException("Could not load configuration from input stream.",ioe);
  +        }
       }
   
       /**
  @@ -273,22 +277,27 @@
        * @throws IOException
        */
       public void save(String filename)
  -        throws IOException
  +        throws ConfigurationException
       {
           File file = new File(filename);
  -        PropertiesWriter out = new PropertiesWriter(file);
  +        try {
  +        	PropertiesWriter out = new PropertiesWriter(file);
   
  -        out.writeComment("written by PropertiesConfiguration");
  -        out.writeComment(new Date().toString());
  +        	out.writeComment("written by PropertiesConfiguration");
  +        	out.writeComment(new Date().toString());
   
  -        for (Iterator i = this.getKeys(); i.hasNext();)
  -        {
  -            String key = (String) i.next();
  -            String value = StringUtils.join(this.getStringArray(key), ", ");
  -            out.writeProperty(key, value);
  +        	for (Iterator i = this.getKeys(); i.hasNext();)
  +        	{
  +        		String key = (String) i.next();
  +        		String value = StringUtils.join(this.getStringArray(key), ", ");
  +        		out.writeProperty(key, value);
  +        	}
  +        	out.flush();
  +        	out.close();
  +        }
  +        catch (IOException ioe){
  +        	throw new ConfigurationException("Could not save to file " + filename,ioe);
           }
  -        out.flush();
  -        out.close();
       }
   
       /**
  
  
  
  1.6       +5 -6      jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationFactory.java
  
  Index: ConfigurationFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ConfigurationFactory.java	23 Jan 2004 11:52:36 -0000	1.5
  +++ ConfigurationFactory.java	30 Jan 2004 14:46:37 -0000	1.6
  @@ -61,7 +61,6 @@
   import java.util.LinkedList;
   import java.util.Stack;
   
  -import org.apache.commons.configuration.exception.ConfigurationLoadException;
   import org.apache.commons.digester.AbstractObjectCreationFactory;
   import org.apache.commons.digester.Digester;
   import org.apache.commons.digester.ObjectCreationFactory;
  @@ -146,7 +145,7 @@
        * @throws Exception A generic exception that we had trouble during the
        * loading of the configuration data.
        */
  -    public Configuration getConfiguration() throws ConfigurationLoadException
  +    public Configuration getConfiguration() throws ConfigurationException
       {
           Digester digester;
           InputStream input = null;
  @@ -163,7 +162,7 @@
           catch (Exception e)
           {
           	log.error("Exception caught opening stream to URL", e);
  -        	throw new ConfigurationLoadException("Exception caught opening stream to URL",e);
  +        	throw new ConfigurationException("Exception caught opening stream to URL",e);
           }
   
           if (getDigesterRules() == null)
  @@ -190,12 +189,12 @@
           catch (SAXException saxe)
           {
               log.error("SAX Exception caught", saxe);
  -            throw new ConfigurationLoadException("SAX Exception caught",saxe);
  +            throw new ConfigurationException("SAX Exception caught",saxe);
           }
           catch (IOException ioe)
           {
           	log.error("IO Exception caught", ioe);
  -        	throw new ConfigurationLoadException("IO Exception caught",ioe);
  +        	throw new ConfigurationException("IO Exception caught",ioe);
           }		
           return builder.getConfiguration();
       }
  
  
  
  1.2       +11 -7     jakarta-commons/configuration/src/java/org/apache/commons/configuration/ClassPropertiesConfiguration.java
  
  Index: ClassPropertiesConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/ClassPropertiesConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ClassPropertiesConfiguration.java	23 Dec 2003 15:09:05 -0000	1.1
  +++ ClassPropertiesConfiguration.java	30 Jan 2004 14:46:37 -0000	1.2
  @@ -93,7 +93,7 @@
        * @throws IOException Error while loading the properties file
        */
       public ClassPropertiesConfiguration(Class baseClass, String resource)
  -        throws IOException
  +        throws ConfigurationException
       {
           this.baseClass = baseClass;
           // According to javadocs, getClassLoader() might return null
  @@ -104,8 +104,12 @@
               : baseClass.getClassLoader();
           
           setIncludesAllowed(true);
  -
  -        load(getPropertyStream(resource));
  +        try {
  +        	load(getPropertyStream(resource));
  +        }
  +        catch (IOException ioe){
  +        	throw new ConfigurationException("Could not load input stream from resource " + resource,ioe);
  +        }
       }
   
       /**
  @@ -119,7 +123,7 @@
        */
       public ClassPropertiesConfiguration(Class baseClass, String resource, 
                                           Configuration defaults)
  -        throws IOException
  +        throws ConfigurationException
       {
           this(baseClass, resource);
           this.defaults = defaults;
  @@ -136,7 +140,7 @@
        */
       public ClassPropertiesConfiguration(Class baseClass, String resource,
                                           String defaultFile)
  -        throws IOException
  +        throws ConfigurationException
       {
           this(baseClass, resource);
   
  @@ -155,7 +159,7 @@
        * @return An Input Stream
        * @throws IOException Error while loading the properties file
        */
  -    public InputStream getPropertyStream(String resourceName)
  +    protected InputStream getPropertyStream(String resourceName)
           throws IOException
       {
           InputStream resource = null;
  
  
  
  1.1                  jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationException.java
  
  Index: ConfigurationException.java
  ===================================================================
  package org.apache.commons.configuration;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.commons.lang.exception.NestableException;;
  
  /**
   * Any exception that occurs while initializing a Configuration
   * object.
   *
   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
   *
   * @version $Id: ConfigurationException.java,v 1.1 2004/01/30 14:46:37 epugh Exp $
   */
  
  public class ConfigurationException extends NestableException {
  
  	public ConfigurationException(Throwable root) {
  		super(root);
  	}
  
  
  	public ConfigurationException(String string, Throwable root) {
  		super(string, root);
  	}
  
  	public ConfigurationException(String s) {
  		super(s);
  	}
  }
  
  
  
  
  
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org