1 16 package org.apache.commons.betwixt.expression; 17 18 import java.lang.reflect.Array ; 19 import java.lang.reflect.Method ; 20 import java.util.Collection ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 41 public class MapEntryAdder { 42 43 44 47 48 private static Log log = LogFactory.getLog( MapEntryAdder.class ); 49 50 51 54 59 public static void setLog(Log newLog) { 60 log = newLog; 61 } 62 63 66 67 private Method adderMethod; 68 69 70 private boolean keyUpdated = false; 71 72 private Object key; 73 74 75 private boolean valueUpdated = false; 76 77 private Object value; 78 79 80 83 89 public MapEntryAdder(Method method) { 90 91 Class [] types = method.getParameterTypes(); 92 if ( types == null || types.length != 2) { 93 throw new IllegalArgumentException ( 94 "Method used to add entries to maps must have two parameter."); 95 } 96 this.adderMethod = method; 97 } 98 99 102 110 public Updater getKeyUpdater() { 111 112 return new Updater() { 113 public void update( Context context, Object keyValue ) { 114 if ( !keyUpdated ) { 116 keyUpdated = true; 117 key = keyValue; 118 if ( log.isTraceEnabled() ) { 119 log.trace( "Setting entry key to " + key ); 120 log.trace( "Current entry value is " + value ); 121 } 122 if ( valueUpdated ) { 123 callAdderMethod( context ); 124 } 125 } 126 } 127 }; 128 } 129 130 138 public Updater getValueUpdater() { 139 140 return new Updater() { 141 public void update( Context context, Object valueValue ) { 142 if ( !valueUpdated ) { 144 valueUpdated = true; 145 value = valueValue; 146 if ( log.isTraceEnabled() ) { 147 log.trace( "Setting entry value to " + value); 148 log.trace( "Current entry key is " + key ); 149 } 150 if ( keyUpdated ) { 151 callAdderMethod( context ); 152 } 153 } 154 } 155 }; 156 } 157 158 159 160 163 169 private void callAdderMethod(Context context) { 170 log.trace("Calling adder method"); 171 172 keyUpdated = false; 174 valueUpdated = false; 175 176 184 Class [] types = adderMethod.getParameterTypes(); 185 Class keyType = types[0]; 187 Class valueType = types[1]; 189 190 Object bean = context.getBean(); 191 if ( bean != null ) { 192 if ( key instanceof String ) { 193 key = context.getObjectStringConverter() 195 .stringToObject( (String ) key, keyType, null, context ); 196 } 197 198 if ( value instanceof String ) { 199 value = context.getObjectStringConverter() 201 .stringToObject( (String ) value, valueType, null, context ); 202 } 203 204 if (value instanceof Collection && valueType.isArray()) { 206 Collection valuesAsCollection = (Collection ) value; 207 Class componentType = valueType.getComponentType(); 208 if (componentType != null) { 209 Object [] valuesAsArray = 210 (Object []) Array.newInstance(componentType, valuesAsCollection.size()); 211 value = valuesAsCollection.toArray(valuesAsArray); 212 } 213 } 214 215 216 Object [] arguments = { key, value }; 217 try { 218 if ( log.isTraceEnabled() ) { 219 log.trace( 220 "Calling adder method: " + adderMethod.getName() + " on bean: " + bean 221 + " with key: " + key + " and value: " + value 222 ); 223 } 224 adderMethod.invoke( bean, arguments ); 225 226 } catch (Exception e) { 227 log.warn( 228 "Cannot evaluate adder method: " + adderMethod.getName() + " on bean: " + bean 229 + " of type: " + bean.getClass().getName() + " with value: " + value 230 + " of type: " + valueType + " and key: " + key 231 + " of type: " + keyType 232 ); 233 log.debug(e); 234 } 235 } 236 } 237 } 238 | Popular Tags |