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 2003/02/28 21:23:41 UTC

cvs commit: jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration CompositeConfiguration.java JNDIConfiguration.java

epugh       2003/02/28 12:23:41

  Added:       configuration/src/java/org/apache/commons/configuration
                        CompositeConfiguration.java JNDIConfiguration.java
  Log:
  New Configuration types, JNDI and Composite!
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java
  
  Index: CompositeConfiguration.java
  ===================================================================
  package org.apache.commons.configuration;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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 Group.
   *
   * 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 java.util.ArrayList;
  import java.util.*;
  
  /**
   * This Configuration class allows you to add multiple different types of Configuration
   * to this CompositeConfiguration.  If you add Configuration1, and then Configuration2, 
   * any properties shared will mean that Configuration1 will be returned.
   * You can add multiple different types or the same type of properties file.
   * If Configuration1 doesn't have the property, then Configuration2 will be checked.
   * 
   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
   * @version $Id: CompositeConfiguration.java,v 1.1 2003/02/28 20:23:41 epugh Exp $
   */
  public class CompositeConfiguration implements Configuration {
  	private ArrayList configList = new ArrayList();
  
  	/**
  	 * Creates an empty CompositeConfiguration object which can then
  	 * be added some other Configuration files
  	 */
  	public CompositeConfiguration() {
  
  	}
  
  	public void addConfiguration(Configuration config) {
  		if (!configList.contains(config)) {
  			configList.add(config);
  		}
  	}
  
  	public void removeConfiguration(Configuration config) {
  		configList.remove(config);
  	}
  
  	public int getNumberOfConfigurations() {
  		return configList.size();
  	}
  	
  	public void clear(){
  		configList.clear();
  	}
  
  	/**
  	 * CompositeConfigurations can not be added to
  	 *
  	 * @param key The Key to add the property to.
  	 * @param token The Value to add.
  	 */
  	public void addProperty(String key, Object token) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get the list of the keys contained in the configuration
  	 * repository.
  	 *
  	 * @return An Iterator.
  	 */
  	public Iterator getKeys() {
  		HashSet keys = new HashSet();
  		for (ListIterator i = configList.listIterator(); i.hasNext();) {
  			Configuration config = (Configuration) i.next();
  			for (Iterator j = config.getKeys(); j.hasNext();) {
  				String key = (String) j.next();
  				keys.add(key);
  			}
  		}
  		return keys.iterator();
  	}
  
  	/**
  	 * Get the list of the keys contained in the configuration
  	 * repository.
  	 *
  	 * @return An Iterator.
  	 */
  	public Iterator getKeys(String key) {
  		HashSet keys = new HashSet();
  		for (ListIterator i = configList.listIterator(); i.hasNext();) {
  			Configuration config = (Configuration) i.next();
  			for (Iterator j = config.getKeys(key); j.hasNext();) {
  				String newKey = (String) j.next();
  				keys.add(newKey);
  			}
  		}
  		return keys.iterator();
  	}
  	/**
  	 * Get a list of properties associated with the given
  	 * configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated properties if key is found.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a String/Vector.
  	 * @exception IllegalArgumentException if one of the tokens is
  	 * malformed (does not contain an equals sign).
  	 * @see #getProperties(String, Properties)
  	 */
  	public Properties getProperties(String key) {
  		return getFirstMatchingConfig(key).getProperties(key);
  	}
  
  	public boolean isEmpty() {
  		boolean isEmpty = true;
  		for (ListIterator i = configList.listIterator(); i.hasNext();) {
  			Configuration config = (Configuration) i.next();
  			if (!config.isEmpty()) {
  				return false;
  			}
  		}
  		return isEmpty;
  
  	}
  
  	/**
  	 *  Gets a property from the configuration.
  	 *
  	 *  @param key property to retrieve
  	 *  @return value as object. Will return user value if exists,
  	 *          if not then default value if exists, otherwise null
  	 */
  	public Object getProperty(String key) {
  		return getFirstMatchingConfig(key).getProperty(key);
  	}
  
  	/**
  	 * Set a property, this will replace any previously
  	 * set values. Set values is implicitly a call
  	 * to clearProperty(key), addProperty(key,value).
  	 *
  	 * @param key
  	 * @param value
  	 */
  	public void setProperty(String key, Object value) {
  		getFirstMatchingConfig(key).setProperty(key, value);
  	}
  
  	/**
  	 * Clear a property in the configuration.
  	 *
  	 * @param key the key to remove along with corresponding value.
  	 */
  	public void clearProperty(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * check if the configuration contains the key
  	 */
  	public boolean containsKey(String key) {
  		for (ListIterator i = configList.listIterator(); i.hasNext();) {
  			Configuration config = (Configuration) i.next();
  			if (config.containsKey(key)) {
  				return true;
  			}
  		}
  		return false;
  
  	}
  
  	/**
  	 * Create an ExtendedProperties object that is a subset
  	 * of this one. Take into account duplicate keys
  	 * by using the setProperty() in ExtendedProperties.
  	 *
  	 * @param prefix
  	 */
  	public Configuration subset(String prefix) {
  		CompositeConfiguration subsetConfig = new CompositeConfiguration();
  
  		for (ListIterator i = configList.listIterator(); i.hasNext();) {
  			Configuration config = (Configuration) i.next();
  			subsetConfig.addConfiguration(config.subset(prefix));
  		}
  		return subsetConfig;
  
  	}
  
  	/**
  	* Get a float associated with the given configuration key.
  	* 
  	* @ param key The configuration key.
  	* @ return The associated float.
  	* @ exception NoSuchElementException is thrown if the key doesn 't
  	* map to an existing object.
  	* @ exception ClassCastException is thrown if the key maps to an
  	* object that is not a Float.
  	* @ exception NumberFormatException is thrown if the value mapped
  	* by the key has not a valid number format.
  	 */
  	public float getFloat(String key) {
  		return getFirstMatchingConfig(key).getFloat(key);
  	}
  
  	/**
  	 * Get a boolean associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated boolean.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Boolean.
  	 */
  	public boolean getBoolean(String key) {
  		return getFirstMatchingConfig(key).getBoolean(key);
  	}
  
  	/**
  	 * Get a boolean associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated boolean.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Boolean.
  	 */
  	public boolean getBoolean(String key, boolean defaultValue) {
  		return getFirstMatchingConfig(key).getBoolean(key, defaultValue);
  
  	}
  
  	/**
  	 * Get a boolean associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated boolean if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Boolean.
  	 */
  	public Boolean getBoolean(String key, Boolean defaultValue) {
  		return getFirstMatchingConfig(key).getBoolean(key, defaultValue);
  	}
  
  	/**
  	 * Get a byte associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated byte.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Byte.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public byte getByte(String key) {
  		return getFirstMatchingConfig(key).getByte(key);
  	}
  
  	/**
  	 * Get a byte associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated byte.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Byte.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public byte getByte(String key, byte defaultValue) {
  		return getFirstMatchingConfig(key).getByte(key, defaultValue);
  	}
  
  	/**
  	 * Get a byte associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated byte if key is found and has valid format, default
  	 *         value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *            is not a Byte.
  	 * @exception NumberFormatException is thrown if the value mapped by the key
  	 *            has not a valid number format.
  	 */
  	public Byte getByte(String key, Byte defaultValue) {
  		return getFirstMatchingConfig(key).getByte(key, defaultValue);
  	}
  
  	/**
  	 * Get a double associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated double.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Double.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public double getDouble(String key) {
  		return getFirstMatchingConfig(key).getDouble(key);
  	}
  
  	/**
  	 * Get a double associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated double.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Double.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public double getDouble(String key, double defaultValue) {
  		return getDouble(key, new Double(defaultValue)).doubleValue();
  	}
  
  	/**
  	 * Get a double associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated double if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Double.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Double getDouble(String key, Double defaultValue) {
  		return getFirstMatchingConfig(key).getDouble(key, defaultValue);
  	}
  
  	/**
  	 * Get a float associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated float.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Float.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public float getFloat(String key, float defaultValue) {
  		return getFirstMatchingConfig(key).getFloat(key, defaultValue);
  	}
  
  	/**
  	 * Get a float associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated float if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Float.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Float getFloat(String key, Float defaultValue) {
  		return getFirstMatchingConfig(key).getFloat(key, defaultValue);
  	}
  
  	/**
  	 * Get a int associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated int.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Integer.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public int getInt(String key) {
  		return getFirstMatchingConfig(key).getInt(key);
  	}
  
  	/**
  	 * Get a int associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated int.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Integer.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public int getInt(String key, int defaultValue) {
  		return getFirstMatchingConfig(key).getInt(key, defaultValue);
  	}
  
  	/**
  	 * Get a int associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated int if key is found and has valid format, default
  	 *         value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *         is not a Integer.
  	 * @exception NumberFormatException is thrown if the value mapped by the key
  	 *         has not a valid number format.
  	 */
  	public Integer getInteger(String key, Integer defaultValue) {
  		return getFirstMatchingConfig(key).getInteger(key, defaultValue);
  	}
  
  	/**
  	 * Get a long associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated long.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Long.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public long getLong(String key) {
  		return getFirstMatchingConfig(key).getLong(key);
  	}
  
  	/**
  	 * Get a long associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated long.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Long.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public long getLong(String key, long defaultValue) {
  		return getFirstMatchingConfig(key).getLong(key, defaultValue);
  	}
  
  	/**
  	 * Get a long associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated long if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Long.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Long getLong(String key, Long defaultValue) {
  		return getFirstMatchingConfig(key).getLong(key, defaultValue);
  	}
  
  	/**
  	 * Get a short associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated short.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Short.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public short getShort(String key) {
  		return getFirstMatchingConfig(key).getShort(key);
  	}
  
  	/**
  	 * Get a short associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated short.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Short.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public short getShort(String key, short defaultValue) {
  		return getFirstMatchingConfig(key).getShort(key, defaultValue);
  	}
  
  	/**
  	 * Get a short associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated short if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Short.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Short getShort(String key, Short defaultValue) {
  		return getFirstMatchingConfig(key).getShort(key, defaultValue);
  	}
  
  	/**
  	 * Get a string associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated string.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *            is not a String.
  	 */
  	public String getString(String key) {
  		return getFirstMatchingConfig(key).getString(key);
  	}
  
  	/**
  	 * Get a string associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated string if key is found, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *            is not a String.
  	 */
  	public String getString(String key, String defaultValue) {
  		return getFirstMatchingConfig(key).getString(key, defaultValue);
  	}
  
  	/**
  	 * Get an array of strings associated with the given configuration
  	 * key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated string array if key is found.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a String/Vector of Strings.
  	 */
  	public String[] getStringArray(String key) {
  		return getFirstMatchingConfig(key).getStringArray(key);
  	}
  
  	/**
  	 * Get a Vector of strings associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated Vector.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Vector.
  	 */
  	public Vector getVector(String key) {
  		return getFirstMatchingConfig(key).getVector(key);
  	}
  
  	/**
  	 * Get a Vector of strings associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated Vector.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Vector.
  	 */
  	public Vector getVector(String key, Vector defaultValue) {
  		return getFirstMatchingConfig(key).getVector(key, defaultValue);
  	}
  
  	private Configuration getFirstMatchingConfig(String key) {
  		for (ListIterator i = configList.listIterator(); i.hasNext();) {
  			Configuration config = (Configuration) i.next();
  			if (config.containsKey(key)) {
  				return config;
  			}
  		}
  
  		throw new NoSuchElementException(
  			'\'' + key + "' doesn't map to an existing object");
  	}
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/JNDIConfiguration.java
  
  Index: JNDIConfiguration.java
  ===================================================================
  package org.apache.commons.configuration;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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 Group.
   *
   * 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 java.util.Iterator;
  import java.util.NoSuchElementException;
  import java.util.Properties;
  import java.util.Vector;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  
  import org.apache.commons.lang.StringUtils;
  
  /**
   * This Configuration class allows you to interface with a JNDI datasource.
   * 
   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
   * @version $Id: JNDIConfiguration.java,v 1.1 2003/02/28 20:23:41 epugh Exp $
   */
  public class JNDIConfiguration implements Configuration {
  
  	private String prefix;
  	private Context envCtx;
  
  	/**
  	 * Creates an empty JNDIConfiguration object which can then
  	 * be added some other Configuration files
  	 */
  	public JNDIConfiguration() {
  
  	}
  
  	/**
  	 * JNDIConfigurations can not be added to
  	 *
  	 * @param key The Key to add the property to.
  	 * @param token The Value to add.
  	 */
  	public void addProperty(String key, Object token) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get the list of the keys contained in the configuration
  	 * repository.
  	 *
  	 * @return An Iterator.
  	 */
  	public Iterator getKeys() {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get the list of the keys contained in the configuration
  	 * repository.
  	 *
  	 * @return An Iterator.
  	 */
  	public Iterator getKeys(String key) {
  
  		throw new Error("This operation is not supported");
  	}
  	/**
  	 * Get a list of properties associated with the given
  	 * configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated properties if key is found.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a String/Vector.
  	 * @exception IllegalArgumentException if one of the tokens is
  	 * malformed (does not contain an equals sign).
  	 * @see #getProperties(String, Properties)
  	 */
  	public Properties getProperties(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	public boolean isEmpty() {
  		throw new Error("This operation is not supported");
  
  	}
  
  	/**
  	 *  Gets a property from the configuration.
  	 *
  	 *  @param key property to retrieve
  	 *  @return value as object. Will return user value if exists,
  	 *          if not then default value if exists, otherwise null
  	 */
  	public Object getProperty(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Set a property, this will replace any previously
  	 * set values. Set values is implicitly a call
  	 * to clearProperty(key), addProperty(key,value).
  	 *
  	 * @param key
  	 * @param value
  	 */
  	public void setProperty(String key, Object value) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Clear a property in the configuration.
  	 *
  	 * @param key the key to remove along with corresponding value.
  	 */
  	public void clearProperty(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * check if the configuration contains the key
  	 */
  	public boolean containsKey(String key) {
  		key = StringUtils.replace(key, ".", "/");
  		try {
  
  			if (envCtx == null) {
  				Context initCtx = new InitialContext();
  				envCtx = (Context) initCtx.lookup(getPrefix());
  			}
  			String value = (String) envCtx.lookup(key);
  			return true;
  		} catch (javax.naming.NamingException ne) {
  			return false;
  		}
  
  	}
  
  	/**
  	 * Create an ExtendedProperties object that is a subset
  	 * of this one. Take into account duplicate keys
  	 * by using the setProperty() in ExtendedProperties.
  	 *
  	 * @param prefix
  	 */
  	public Configuration subset(String prefix) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	* Get a float associated with the given configuration key.
  	* 
  	* @ param key The configuration key.
  	* @ return The associated float.
  	* @ exception NoSuchElementException is thrown if the key doesn 't
  	* map to an existing object.
  	* @ exception ClassCastException is thrown if the key maps to an
  	* object that is not a Float.
  	* @ exception NumberFormatException is thrown if the value mapped
  	* by the key has not a valid number format.
  	 */
  	public float getFloat(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a boolean associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated boolean.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Boolean.
  	 */
  	public boolean getBoolean(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a boolean associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated boolean.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Boolean.
  	 */
  	public boolean getBoolean(String key, boolean defaultValue) {
  		throw new Error("This operation is not supported");
  
  	}
  
  	/**
  	 * Get a boolean associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated boolean if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Boolean.
  	 */
  	public Boolean getBoolean(String key, Boolean defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a byte associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated byte.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Byte.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public byte getByte(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a byte associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated byte.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Byte.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public byte getByte(String key, byte defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a byte associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated byte if key is found and has valid format, default
  	 *         value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *            is not a Byte.
  	 * @exception NumberFormatException is thrown if the value mapped by the key
  	 *            has not a valid number format.
  	 */
  	public Byte getByte(String key, Byte defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a double associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated double.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Double.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public double getDouble(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a double associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated double.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Double.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public double getDouble(String key, double defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a double associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated double if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Double.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Double getDouble(String key, Double defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a float associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated float.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Float.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public float getFloat(String key, float defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a float associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated float if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Float.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Float getFloat(String key, Float defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a int associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated int.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Integer.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public int getInt(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a int associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated int.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Integer.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public int getInt(String key, int defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a int associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated int if key is found and has valid format, default
  	 *         value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *         is not a Integer.
  	 * @exception NumberFormatException is thrown if the value mapped by the key
  	 *         has not a valid number format.
  	 */
  	public Integer getInteger(String key, Integer defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a long associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated long.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Long.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public long getLong(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a long associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated long.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Long.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public long getLong(String key, long defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a long associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated long if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Long.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Long getLong(String key, Long defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a short associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated short.
  	 * @exception NoSuchElementException is thrown if the key doesn't
  	 * map to an existing object.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Short.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public short getShort(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a short associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated short.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Short.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public short getShort(String key, short defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a short associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated short if key is found and has valid
  	 * format, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Short.
  	 * @exception NumberFormatException is thrown if the value mapped
  	 * by the key has not a valid number format.
  	 */
  	public Short getShort(String key, Short defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a string associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated string.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *            is not a String.
  	 */
  	public String getString(String key) {
  		return getValueFromJNDI(key);
  	}
  
  	/**
  	 * Get a string associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated string if key is found, default value otherwise.
  	 * @exception ClassCastException is thrown if the key maps to an object that
  	 *            is not a String.
  	 */
  	public String getString(String key, String defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get an array of strings associated with the given configuration
  	 * key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated string array if key is found.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a String/Vector of Strings.
  	 */
  	public String[] getStringArray(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a Vector of strings associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @return The associated Vector.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Vector.
  	 */
  	public Vector getVector(String key) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * Get a Vector of strings associated with the given configuration key.
  	 *
  	 * @param key The configuration key.
  	 * @param defaultValue The default value.
  	 * @return The associated Vector.
  	 * @exception ClassCastException is thrown if the key maps to an
  	 * object that is not a Vector.
  	 */
  	public Vector getVector(String key, Vector defaultValue) {
  		throw new Error("This operation is not supported");
  	}
  
  	/**
  	 * @return String
  	 */
  	public String getPrefix() {
  		return prefix;
  	}
  
  	/**
  	 * Sets the prefix.
  	 * @param prefix The prefix to set
  	 */
  	public void setPrefix(String prefix) {
  		this.prefix = prefix;
  	}
  
  	private String getValueFromJNDI(String key) {
  		try {
  			key = StringUtils.replace(key, ".", "/");
  			if (envCtx == null) {
  				Context initCtx = new InitialContext();
  				envCtx = (Context) initCtx.lookup(getPrefix());
  			}
  			String value = (String) envCtx.lookup(key);
  			return value;
  		} catch (Exception e) {
  
  			throw new NoSuchElementException(
  				'\'' + key + "' doesn't map to an existing object");
  
  		}
  	}
  }
  
  
  

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