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 84 85 public class MappedProperty extends Property { 86 Map values; 87 88 91 public MappedProperty() { 92 super(); 93 values = new TreeMap (); 94 } 95 96 102 public void setMappedValue(String key, String value) { 103 if (key == null || key.length() == 0) { 104 throw new IllegalArgumentException ("key must be non-zero length, non null string"); 105 } 106 107 values.put(key, value); 108 } 109 110 116 public String getMappedValue(String key) { 117 if (key == null || key.length() == 0) { 118 throw new IllegalArgumentException ("key must be non-zero length, non null string"); 119 } 120 121 return (String ) values.get(key); 122 } 123 124 129 public Map getValues() { 130 return values; 131 } 132 133 143 public Configuration createConfigBean(DefaultConfigBean targetBean, 144 ComponentConfig config, 145 ComponentMetadata metadata) { 146 Map parsedProperties = config.getMappedProperties(this.getName()); 147 if (parsedProperties == null) { 148 for (Iterator i = values.keySet().iterator(); i.hasNext();) { 153 String key = (String ) i.next(); 154 String value = (String ) values.get(key); 155 String type = this.getType(); 156 try { 157 targetBean.set(this.getName(), ConvertUtils.convert(value, ClassLocator.loadClass(type))); 158 } catch (ClassNotFoundException ex) { 159 Logger log = Logger.getLogger(this.getClass()); 160 log.error("Error converting value " + value 161 + " to class : " + type + " class not found.", ex); 162 throw new IllegalArgumentException ("Class not found: " + type); 163 } 164 } 165 166 return targetBean; 167 } 168 169 for (Iterator i = parsedProperties.keySet().iterator(); i.hasNext();) { 170 String key = (String ) i.next(); 171 String value = config.getMappedProperty(this.getName(), key); 172 173 if (value == null || value.length() == 0) { 174 value = this.getMappedValue(key); 175 } 176 String type = this.getType(); 177 try { 178 targetBean.set(this.getName(), key, ConvertUtils.convert(value, ClassLocator.loadClass(type))); 179 } catch (ClassNotFoundException ex) { 180 Logger log = Logger.getLogger(this.getClass()); 181 log.error("Error converting value " + value 182 + " to class : " + type + " class not found.", ex); 183 throw new IllegalArgumentException ("Class not found: " + type); 184 } 185 } 186 187 for (Iterator i = values.keySet().iterator(); i.hasNext();) { 192 String key = (String ) i.next(); 193 194 if (!config.getMappedProperties(this.getName()).containsKey(key)) { 195 String value = (String ) values.get(key); 196 String type = this.getType(); 197 try { 198 targetBean.set(this.getName(), ConvertUtils.convert(value, ClassLocator.loadClass(type))); 199 } catch (ClassNotFoundException ex) { 200 Logger log = Logger.getLogger(this.getClass()); 201 log.error("Error converting value " + value 202 + " to class : " + type + " class not found.", ex); 203 throw new IllegalArgumentException ("Class not found: " + type); 204 } 205 } 206 } 207 return targetBean; 208 } 209 } | Popular Tags |