1 18 19 package org.apache.beehive.controls.system.jdbc.parser; 20 21 import org.apache.beehive.controls.api.ControlException; 22 import org.apache.beehive.controls.api.context.ControlBeanContext; 23 import org.apache.beehive.controls.system.jdbc.TypeMappingsFactory; 24 25 import java.lang.reflect.Method ; 26 import java.util.Map ; 27 import java.util.regex.Pattern ; 28 29 38 public final class ReflectionFragment extends SqlFragment { 39 40 private static final String PREPARED_STATEMENT_SUB_MARK = "?"; 41 private static final Pattern s_parameterNamePattern = Pattern.compile("\\."); 42 43 private final String _parameterName; 44 private final String [] _nameQualifiers; 45 private int _sqlDataType; 46 47 52 ReflectionFragment(String parameterName) { 53 _parameterName = parameterName; 54 _sqlDataType = TypeMappingsFactory.TYPE_UNKNOWN; 55 _nameQualifiers = s_parameterNamePattern.split(_parameterName); 56 } 57 58 64 ReflectionFragment(String parameterName, String sqlDataType) { 65 this(parameterName); 66 if (sqlDataType != null) { 67 _sqlDataType = TypeMappingsFactory.getInstance().convertStringToSQLType(sqlDataType); 68 } 69 } 70 71 79 String getPreparedStatementText(ControlBeanContext context, Method m, Object [] args) { 80 return PREPARED_STATEMENT_SUB_MARK; 81 } 82 83 88 boolean hasParamValue() { return true; } 89 90 95 String getParameterName() { return _parameterName; } 96 97 102 String [] getParameterNameQualifiers() { 103 String [] nameQualifiersCopy = new String [_nameQualifiers.length]; 104 System.arraycopy(_nameQualifiers, 0, nameQualifiersCopy, 0, _nameQualifiers.length); 105 return nameQualifiersCopy; 106 } 107 108 113 int getParamSqlDataType() { return _sqlDataType; } 114 115 120 public String toString() { return PREPARED_STATEMENT_SUB_MARK; } 121 122 130 Object [] getParameterValues(ControlBeanContext context, Method method, Object [] args) { 131 132 Object value = null; 133 try { 134 value = context.getParameterValue(method, _nameQualifiers[0], args); 135 } catch (IllegalArgumentException iae) { 136 throw new ControlException("Invalid argument name in SQL statement: " + _nameQualifiers[0], iae); 137 } 138 139 for (int i = 1; i < _nameQualifiers.length; i++) { 140 value = extractValue(value, _nameQualifiers[i - 1], _nameQualifiers[i]); 142 } 143 return new Object []{value}; 144 } 145 146 150 158 private Object extractValue(Object aValue, String aName, String bName) { 159 160 Class aClass = aValue.getClass(); 161 Object value = null; 162 163 String bNameCapped = Character.toUpperCase(bName.charAt(0)) + bName.substring(1); 167 Method getMethod = null; 168 Class retType = null; 169 170 try { 175 176 getMethod = aClass.getMethod("is" + bNameCapped, (Class []) null); 177 retType = getMethod.getReturnType(); 178 if (!(retType.equals(Boolean .class) || 179 retType.equals(Boolean.TYPE))) { 180 getMethod = null; 182 } else { 183 187 boolean getMethodFound = true; 188 try { 189 aClass.getMethod("get" + bNameCapped, (Class [])null); 190 } catch (NoSuchMethodException e) { 191 getMethodFound = false; 192 } 193 194 if (getMethodFound) { 195 throw new ControlException("Colliding field accsessors in user defined class '" 196 + aClass.getName() + "' for field '" + bName 197 + "'. Please use is<FieldName> for boolean fields and get<FieldName> name for other datatypes."); 198 } 199 } 200 } catch (NoSuchMethodException e) { 201 } 202 203 if (getMethod == null) { 207 try { 208 getMethod = aClass.getMethod("get" + bNameCapped, (Class [])null); 209 retType = getMethod.getReturnType(); 210 } catch (NoSuchMethodException e) { 211 } 212 } 213 214 if (getMethod != null) { 215 try { 217 value = getMethod.invoke(aValue, (Object []) null); 218 } catch (IllegalAccessException e) { 219 throw new ControlException("Unable to access public method: " + e.toString()); 220 } catch (java.lang.reflect.InvocationTargetException e) { 221 throw new ControlException("Exception thrown when executing : " + getMethod.getName() + "() to use as parameter"); 222 } 223 return value; 224 } 225 226 230 try { 231 value = aClass.getField(bName).get(aValue); 232 return value; 233 } catch (NoSuchFieldException e) { 234 } catch (IllegalAccessException e) { 235 } 236 237 241 if (aValue instanceof Map ) { 242 try { 243 value = TypeMappingsFactory.getInstance().lookupType(aValue, new Object []{bName}); 244 return value; 245 } catch (Exception mapex) { 246 throw new ControlException("Exception thrown when executing Map.get() to resolve parameter" + mapex.toString()); 247 } 248 } 249 250 if (true) { 252 throw new ControlException("Illegal argument in SQL statement: " + _parameterName.toString() 253 + "; unable to find suitable method of retrieving property " + bName.toString() 254 + " out of object " + aName.toString() + "."); 255 } 256 return null; 257 } 258 } 259 | Popular Tags |