Coverage Report - org.apache.commons.configuration.beanutils.ConfigurationDynaBean
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationDynaBean
85%
85/100
94%
33/35
5,7
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.commons.configuration.beanutils;
 19  
 
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.commons.beanutils.DynaBean;
 24  
 import org.apache.commons.beanutils.DynaClass;
 25  
 import org.apache.commons.configuration.Configuration;
 26  
 import org.apache.commons.configuration.ConfigurationMap;
 27  
 import org.apache.commons.configuration.ConversionException;
 28  
 import org.apache.commons.configuration.SubsetConfiguration;
 29  
 import org.apache.commons.lang.BooleanUtils;
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 
 33  
 /**
 34  
  * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
 35  
  * configurations properties from a wrapped configuration-collection
 36  
  * {@link org.apache.commons.configuration.Configuration} instance. It also
 37  
  * implements a {@link java.util.Map} interface so that it can be used in
 38  
  * JSP 2.0 Expression Language expressions.
 39  
  *
 40  
  * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
 41  
  * to the appropriate <code>Configuration</code> subset using the
 42  
  * {@link org.apache.commons.configuration.Configuration#subset}
 43  
  * method. Similarly, indexed properties reference lists of configuration
 44  
  * properties using the
 45  
  * {@link org.apache.commons.configuration.Configuration#getList(String)}
 46  
  * method. Setting an indexed property always throws an exception.</p>
 47  
  *
 48  
  * <p>Note: Some of the methods expect that a dot (&quot;.&quot;) is used as
 49  
  * property delimitor for the wrapped configuration. This is true for most of
 50  
  * the default configurations. Hierarchical configurations, for which a specific
 51  
  * expression engine is set, may cause problems.</p>
 52  
  *
 53  
  * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
 54  
  * @version $Revision: 492216 $, $Date: 2007-01-03 17:51:24 +0100 (Mi, 03 Jan 2007) $
 55  
  * @since 1.0-rc1
 56  
  */
 57  2
 public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
 58  
 {
 59  
     /** Constant for the property delimiter.*/
 60  
     private static final String PROPERTY_DELIMITER = ".";
 61  
 
 62  
     /** The logger.*/
 63  2
     private static Log log = LogFactory.getLog(ConfigurationDynaBean.class);
 64  
 
 65  
     /**
 66  
      * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
 67  
      * the configuration this bean is associated with.
 68  
      * @param configuration the configuration
 69  
      */
 70  
     public ConfigurationDynaBean(Configuration configuration)
 71  
     {
 72  78
         super(configuration);
 73  78
         if (log.isTraceEnabled())
 74  
         {
 75  0
             log.trace("ConfigurationDynaBean(" + configuration + ")");
 76  
         }
 77  78
     }
 78  
 
 79  
     public void set(String name, Object value)
 80  
     {
 81  778
         if (log.isTraceEnabled())
 82  
         {
 83  0
             log.trace("set(" + name + "," + value + ")");
 84  
         }
 85  
 
 86  778
         if (value == null)
 87  
         {
 88  2
             throw new NullPointerException("Error trying to set property to null.");
 89  
         }
 90  
 
 91  776
         if (value instanceof List)
 92  
         {
 93  76
             List list = (List) value;
 94  76
             Iterator iterator = list.iterator();
 95  532
             while (iterator.hasNext())
 96  
             {
 97  380
                 getConfiguration().addProperty(name, iterator.next());
 98  
             }
 99  
         }
 100  700
         else if (value instanceof int[])
 101  
         {
 102  76
             int[] array = (int[]) value;
 103  456
             for (int i = 0; i < array.length; i++)
 104  
             {
 105  380
                 getConfiguration().addProperty(name, new Integer(array[i]));
 106  
             }
 107  
         }
 108  624
         else if (value instanceof boolean[])
 109  
         {
 110  76
             boolean[] array = (boolean[]) value;
 111  456
             for (int i = 0; i < array.length; i++)
 112  
             {
 113  380
                 getConfiguration().addProperty(name, BooleanUtils.toBooleanObject(array[i]));
 114  
             }
 115  
         }
 116  548
         else if (value instanceof char[])
 117  
         {
 118  76
             char[] array = (char[]) value;
 119  456
             for (int i = 0; i < array.length; i++)
 120  
             {
 121  380
                 getConfiguration().addProperty(name, new Character(array[i]));
 122  
             }
 123  
         }
 124  472
         else if (value instanceof byte[])
 125  
         {
 126  76
             byte[] array = (byte[]) value;
 127  456
             for (int i = 0; i < array.length; i++)
 128  
             {
 129  380
                 getConfiguration().addProperty(name, new Byte(array[i]));
 130  
             }
 131  
         }
 132  396
         else if (value instanceof short[])
 133  
         {
 134  76
             short[] array = (short[]) value;
 135  456
             for (int i = 0; i < array.length; i++)
 136  
             {
 137  380
                 getConfiguration().addProperty(name, new Short(array[i]));
 138  
             }
 139  
         }
 140  320
         else if (value instanceof long[])
 141  
         {
 142  76
             long[] array = (long[]) value;
 143  456
             for (int i = 0; i < array.length; i++)
 144  
             {
 145  380
                 getConfiguration().addProperty(name, new Long(array[i]));
 146  
             }
 147  
         }
 148  244
         else if (value instanceof float[])
 149  
         {
 150  76
             float[] array = (float[]) value;
 151  456
             for (int i = 0; i < array.length; i++)
 152  
             {
 153  380
                 getConfiguration().addProperty(name, new Float(array[i]));
 154  
             }
 155  
         }
 156  168
         else if (value instanceof double[])
 157  
         {
 158  76
             double[] array = (double[]) value;
 159  456
             for (int i = 0; i < array.length; i++)
 160  
             {
 161  380
                 getConfiguration().addProperty(name, new Double(array[i]));
 162  
             }
 163  
         }
 164  92
         else if (value instanceof Object[])
 165  
         {
 166  76
             Object[] array = (Object[]) value;
 167  456
             for (int i = 0; i < array.length; i++)
 168  
             {
 169  380
                 getConfiguration().addProperty(name, array[i]);
 170  
             }
 171  
         }
 172  
         else
 173  
         {
 174  16
             getConfiguration().setProperty(name, value);
 175  
         }
 176  776
     }
 177  
 
 178  
     public Object get(String name)
 179  
     {
 180  52
         if (log.isTraceEnabled())
 181  
         {
 182  0
             log.trace("get(" + name + ")");
 183  
         }
 184  
 
 185  
         // get configuration property
 186  52
         Object result = getConfiguration().getProperty(name);
 187  52
         if (result == null)
 188  
         {
 189  
             // otherwise attempt to create bean from configuration subset
 190  6
             Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
 191  6
             if (!subset.isEmpty())
 192  
             {
 193  2
                 result = new ConfigurationDynaBean(subset);
 194  
             }
 195  
         }
 196  
 
 197  52
         if (log.isDebugEnabled())
 198  
         {
 199  0
             log.debug(name + "=[" + result + "]");
 200  
         }
 201  
 
 202  52
         if (result == null)
 203  
         {
 204  4
             throw new IllegalArgumentException("Property '" + name + "' does not exist.");
 205  
         }
 206  48
         return result;
 207  
     }
 208  
 
 209  
     public boolean contains(String name, String key)
 210  
     {
 211  12
         Configuration subset = getConfiguration().subset(name);
 212  12
         if (subset == null)
 213  
         {
 214  0
             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
 215  
         }
 216  
 
 217  12
         return subset.containsKey(key);
 218  
     }
 219  
 
 220  
     public Object get(String name, int index)
 221  
     {
 222  
         try
 223  
         {
 224  64
             List list = getConfiguration().getList(name);
 225  62
             if (list.isEmpty())
 226  
             {
 227  0
                 throw new IllegalArgumentException("Indexed property '" + name + "' does not exist.");
 228  
             }
 229  
 
 230  62
             return list.get(index);
 231  
         }
 232  
         catch (ConversionException e)
 233  
         {
 234  2
             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
 235  
         }
 236  
     }
 237  
 
 238  
     public Object get(String name, String key)
 239  
     {
 240  12
         Configuration subset = getConfiguration().subset(name);
 241  12
         if (subset == null)
 242  
         {
 243  0
             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
 244  
         }
 245  
 
 246  12
         return subset.getProperty(key);
 247  
     }
 248  
 
 249  
     public DynaClass getDynaClass()
 250  
     {
 251  22
         return new ConfigurationDynaClass(getConfiguration());
 252  
     }
 253  
 
 254  
     public void remove(String name, String key)
 255  
     {
 256  4
         Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
 257  4
         subset.setProperty(key, null);
 258  4
     }
 259  
 
 260  
     public void set(String name, int index, Object value)
 261  
     {
 262  
         try
 263  
         {
 264  12
             Object property = getConfiguration().getProperty(name);
 265  
 
 266  12
             if (property == null)
 267  
             {
 268  0
                 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
 269  
             }
 270  12
             else if (property instanceof List)
 271  
             {
 272  12
                 List list = (List) property;
 273  12
                 list.set(index, value);
 274  10
                 getConfiguration().setProperty(name, list);
 275  
             }
 276  0
             else if (property.getClass().isArray())
 277  
             {
 278  0
                 Object[] array = (Object[]) property;
 279  0
                 array[index] = value;
 280  
             }
 281  0
             else if (index == 0)
 282  
             {
 283  0
                 getConfiguration().setProperty(name, value);
 284  
             }
 285  
             else
 286  
             {
 287  0
                 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
 288  
             }
 289  10
         }
 290  
         catch (ConversionException e)
 291  
         {
 292  0
             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
 293  
         }
 294  10
     }
 295  
 
 296  
     public void set(String name, String key, Object value)
 297  
     {
 298  4
         getConfiguration().setProperty(name + "." + key, value);
 299  4
     }
 300  
 }