1 64 package com.jcorporate.expresso.kernel.metadata; 65 66 import com.jcorporate.expresso.kernel.Configuration; 67 import com.jcorporate.expresso.kernel.digester.ComponentConfig; 68 import com.jcorporate.expresso.kernel.internal.DefaultConfigBean; 69 import com.jcorporate.expresso.kernel.util.ClassLocator; 70 import org.apache.commons.beanutils.ConvertUtils; 71 import org.apache.log4j.Logger; 72 73 import java.util.Iterator ; 74 import java.util.Map ; 75 import java.util.TreeMap ; 76 77 86 87 public class IndexedProperty extends Property { 88 Map values; 89 90 93 public IndexedProperty() { 94 super(); 95 values = new TreeMap (); 96 } 97 98 104 public void setIndexedValue(int index, String value) { 105 if (index < 0) { 106 throw new IllegalArgumentException ("index must be >= 0"); 107 } 108 109 values.put(new Integer (index), value); 110 } 111 112 118 public Map getValues() { 119 return values; 120 } 121 122 128 public String getIndexedValue(int index) { 129 if (index < 0) { 130 throw new IllegalArgumentException ("index must be >= 0"); 131 } 132 133 return (String ) values.get(new Integer (index)); 134 } 135 136 146 public Configuration createConfigBean(DefaultConfigBean targetBean, 147 ComponentConfig config, 148 ComponentMetadata metadata) { 149 Map parsedProperties = config.getIndexedProperties(this.getName()); 150 if (parsedProperties == null) { 151 for (Iterator i = values.keySet().iterator(); i.hasNext();) { 156 Integer index = (Integer ) i.next(); 157 String value = (String ) values.get(index); 158 String type = this.getType(); 159 try { 160 targetBean.set(this.getName(), ConvertUtils.convert(value, ClassLocator.loadClass(type))); 161 } catch (ClassNotFoundException ex) { 162 Logger log = Logger.getLogger(this.getClass()); 163 log.error("Error converting value " + value 164 + " to class : " + type + " class not found.", ex); 165 throw new IllegalArgumentException ("Class not found: " + type); 166 } 167 } 168 169 return targetBean; 170 } 171 172 for (Iterator i = parsedProperties.keySet().iterator(); i.hasNext();) { 173 Integer index = (Integer ) i.next(); 174 String value = config.getIndexedProperty(this.getName(), index.intValue()); 175 176 if (value == null || value.length() == 0) { 177 value = this.getIndexedValue(index.intValue()); 178 } 179 String type = this.getType(); 180 try { 181 targetBean.set(this.getName(), index.intValue(), 182 ConvertUtils.convert(value, ClassLocator.loadClass(type))); 183 } catch (ClassNotFoundException ex) { 184 Logger log = Logger.getLogger(this.getClass()); 185 log.error("Error converting value " + value 186 + " to class : " + type + " class not found.", ex); 187 throw new IllegalArgumentException ("Class not found: " + type); 188 } 189 } 190 191 for (Iterator i = values.keySet().iterator(); i.hasNext();) { 196 Integer index = (Integer ) i.next(); 197 198 if (!config.getIndexedProperties(this.getName()).containsKey(index)) { 199 String value = (String ) values.get(index); 200 String type = this.getType(); 201 try { 202 targetBean.set(this.getName(), ConvertUtils.convert(value, ClassLocator.loadClass(type))); 203 } catch (ClassNotFoundException ex) { 204 Logger log = Logger.getLogger(this.getClass()); 205 log.error("Error converting value " + value 206 + " to class : " + type + " class not found.", ex); 207 throw new IllegalArgumentException ("Class not found: " + type); 208 } 209 } 210 } 211 return targetBean; 212 } 213 } | Popular Tags |