1 16 17 package org.apache.commons.configuration.beanutils; 18 19 import java.util.Iterator ; 20 import java.util.ArrayList ; 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.beanutils.DynaProperty; 26 import org.apache.commons.configuration.Configuration; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 39 public class ConfigurationDynaClass implements DynaClass 40 { 41 private final static Log log = LogFactory.getLog(ConfigurationDynaClass.class); 42 43 Configuration configuration; 44 45 50 public ConfigurationDynaClass(Configuration configuration) 51 { 52 super(); 53 if (log.isTraceEnabled()) log.trace("ConfigurationDynaClass(" + configuration + ")"); 54 this.configuration = configuration; 55 } 56 57 60 public DynaProperty getDynaProperty(String name) 61 { 62 if (log.isTraceEnabled()) 63 { 64 log.trace("getDynaProperty(" + name + ")"); 65 } 66 67 if (name == null) 68 { 69 throw new IllegalArgumentException ("No such property name=[" + name + "]"); 70 } 71 72 Object value = configuration.getProperty(name); 73 if (value == null) 74 { 75 return null; 76 } 77 else 78 { 79 Class type = value.getClass(); 80 81 if (type == Byte .class) 82 { 83 type = Byte.TYPE; 84 } 85 if (type == Character .class) 86 { 87 type = Character.TYPE; 88 } 89 else if (type == Boolean .class) 90 { 91 type = Boolean.TYPE; 92 } 93 else if (type == Double .class) 94 { 95 type = Double.TYPE; 96 } 97 else if (type == Float .class) 98 { 99 type = Float.TYPE; 100 } 101 else if (type == Integer .class) 102 { 103 type = Integer.TYPE; 104 } 105 else if (type == Long .class) 106 { 107 type = Long.TYPE; 108 } 109 else if (type == Short .class) 110 { 111 type = Short.TYPE; 112 } 113 114 return new DynaProperty(name, type); 115 } 116 } 117 118 121 public DynaProperty[] getDynaProperties() 122 { 123 if (log.isTraceEnabled()) 124 { 125 log.trace("getDynaProperties()"); 126 } 127 128 Iterator keys = configuration.getKeys(); 129 List properties = new ArrayList (); 130 while (keys.hasNext()) 131 { 132 String key = (String ) keys.next(); 133 DynaProperty property = getDynaProperty(key); 134 properties.add(property); 135 } 136 137 DynaProperty[] propertyArray = new DynaProperty[properties.size()]; 138 properties.toArray(propertyArray); 139 if (log.isDebugEnabled()) 140 { 141 log.debug("Found " + properties.size() + " properties."); 142 } 143 144 return propertyArray; 145 } 146 147 150 public String getName() 151 { 152 return ConfigurationDynaBean.class.getName(); 153 } 154 155 158 public DynaBean newInstance() throws IllegalAccessException , InstantiationException 159 { 160 return new ConfigurationDynaBean(configuration); 161 } 162 163 } 164 | Popular Tags |