001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.util; 018 019 import java.beans.PropertyEditor; 020 import java.beans.PropertyEditorManager; 021 import java.lang.reflect.Field; 022 import java.lang.reflect.InvocationTargetException; 023 import java.lang.reflect.Method; 024 import java.lang.reflect.Modifier; 025 import java.net.URI; 026 import java.net.URISyntaxException; 027 import java.util.Arrays; 028 import java.util.HashMap; 029 import java.util.Iterator; 030 import java.util.LinkedHashMap; 031 import java.util.LinkedHashSet; 032 import java.util.Map; 033 import java.util.Set; 034 035 import org.apache.camel.TypeConverter; 036 import org.apache.commons.logging.Log; 037 import org.apache.commons.logging.LogFactory; 038 039 /** 040 * Helper for introspections of beans. 041 */ 042 public final class IntrospectionSupport { 043 044 private static final transient Log LOG = LogFactory.getLog(IntrospectionSupport.class); 045 046 /** 047 * Utility classes should not have a public constructor. 048 */ 049 private IntrospectionSupport() { 050 } 051 052 public static boolean getProperties(Object target, Map props, String optionPrefix) { 053 boolean rc = false; 054 if (target == null) { 055 throw new IllegalArgumentException("target was null."); 056 } 057 if (props == null) { 058 throw new IllegalArgumentException("props was null."); 059 } 060 if (optionPrefix == null) { 061 optionPrefix = ""; 062 } 063 064 Class clazz = target.getClass(); 065 Method[] methods = clazz.getMethods(); 066 for (Method method : methods) { 067 String name = method.getName(); 068 Class type = method.getReturnType(); 069 Class params[] = method.getParameterTypes(); 070 if (name.startsWith("get") && params.length == 0 && type != null && isSettableType(type)) { 071 try { 072 Object value = method.invoke(target); 073 if (value == null) { 074 continue; 075 } 076 077 String strValue = convertToString(value, type); 078 if (strValue == null) { 079 continue; 080 } 081 082 name = name.substring(3, 4).toLowerCase() + name.substring(4); 083 props.put(optionPrefix + name, strValue); 084 rc = true; 085 } catch (Throwable ignore) { 086 // ignore 087 } 088 } 089 } 090 091 return rc; 092 } 093 094 public static Object getProperty(Object target, String prop) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { 095 if (target == null) { 096 throw new IllegalArgumentException("target was null."); 097 } 098 if (prop == null) { 099 throw new IllegalArgumentException("prop was null."); 100 } 101 prop = prop.substring(0, 1).toUpperCase() + prop.substring(1); 102 103 Class clazz = target.getClass(); 104 Method method = getPropertyGetter(clazz, prop); 105 return method.invoke(target); 106 } 107 108 public static Method getPropertyGetter(Class type, String propertyName) throws NoSuchMethodException { 109 Method method = type.getMethod("get" + ObjectHelper.capitalize(propertyName)); 110 return method; 111 } 112 113 public static boolean setProperties(Object target, Map props, String optionPrefix) throws Exception { 114 boolean rc = false; 115 if (target == null) { 116 throw new IllegalArgumentException("target was null."); 117 } 118 if (props == null) { 119 throw new IllegalArgumentException("props was null."); 120 } 121 122 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { 123 String name = (String)iter.next(); 124 if (name.startsWith(optionPrefix)) { 125 Object value = props.get(name); 126 name = name.substring(optionPrefix.length()); 127 if (setProperty(target, name, value)) { 128 iter.remove(); 129 rc = true; 130 } 131 } 132 } 133 return rc; 134 } 135 136 public static Map extractProperties(Map props, String optionPrefix) { 137 if (props == null) { 138 throw new IllegalArgumentException("props was null."); 139 } 140 141 HashMap rc = new HashMap(props.size()); 142 143 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { 144 String name = (String)iter.next(); 145 if (name.startsWith(optionPrefix)) { 146 Object value = props.get(name); 147 name = name.substring(optionPrefix.length()); 148 rc.put(name, value); 149 iter.remove(); 150 } 151 } 152 153 return rc; 154 } 155 156 public static boolean setProperties(TypeConverter typeConverter, Object target, Map props) throws Exception { 157 boolean rc = false; 158 159 if (target == null) { 160 throw new IllegalArgumentException("target was null."); 161 } 162 if (props == null) { 163 throw new IllegalArgumentException("props was null."); 164 } 165 166 for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) { 167 Map.Entry entry = (Map.Entry)iter.next(); 168 if (setProperty(typeConverter, target, (String)entry.getKey(), entry.getValue())) { 169 iter.remove(); 170 rc = true; 171 } 172 } 173 174 return rc; 175 } 176 177 public static boolean setProperties(Object target, Map props) throws Exception { 178 return setProperties(null, target, props); 179 } 180 181 public static boolean setProperty(TypeConverter typeConverter, Object target, String name, Object value) throws Exception { 182 try { 183 Class clazz = target.getClass(); 184 // find candidates of setter methods as there can be overloaded setters 185 Set<Method> setters = findSetterMethods(typeConverter, clazz, name, value); 186 if (setters.isEmpty()) { 187 return false; 188 } 189 190 // loop and execute the best setter method 191 Exception typeConvertionFailed = null; 192 for (Method setter : setters) { 193 // If the type is null or it matches the needed type, just use the value directly 194 if (value == null || setter.getParameterTypes()[0].isAssignableFrom(value.getClass())) { 195 setter.invoke(target, value); 196 return true; 197 } else { 198 // We need to convert it 199 try { 200 Object convertedValue = convert(typeConverter, setter.getParameterTypes()[0], value); 201 setter.invoke(target, convertedValue); 202 return true; 203 } catch (IllegalArgumentException e) { 204 typeConvertionFailed = e; 205 // ignore as there could be another setter method where we could type convert with success 206 LOG.trace("Setter " + setter + " with parameter type " + setter.getParameterTypes()[0] 207 + " could not be used for type conertions of " + value); 208 } 209 } 210 } 211 // we did not find a setter method to use, and if we did try to use a type converter then throw 212 // this kind of exception as the caused by will hint this error 213 if (typeConvertionFailed != null) { 214 throw new IllegalArgumentException("Could not find a suitable setter for property: " + name 215 + " as there isn't a setter method with same type: " + value.getClass().getCanonicalName() 216 + " nor type convertion possbile: " + typeConvertionFailed.getMessage()); 217 } else { 218 return false; 219 } 220 } catch (InvocationTargetException e) { 221 Throwable throwable = e.getTargetException(); 222 if (throwable instanceof Exception) { 223 Exception exception = (Exception)throwable; 224 throw exception; 225 } else { 226 Error error = (Error)throwable; 227 throw error; 228 } 229 } 230 } 231 232 public static boolean setProperty(Object target, String name, Object value) throws Exception { 233 return setProperty(null, target, name, value); 234 } 235 236 private static Object convert(TypeConverter typeConverter, Class type, Object value) throws URISyntaxException { 237 if (typeConverter != null) { 238 Object answer = typeConverter.convertTo(type, value); 239 if (answer == null) { 240 throw new IllegalArgumentException("Could not convert \"" + value + "\" to " + type.getName()); 241 } 242 return answer; 243 } 244 PropertyEditor editor = PropertyEditorManager.findEditor(type); 245 if (editor != null) { 246 editor.setAsText(value.toString()); 247 return editor.getValue(); 248 } 249 if (type == URI.class) { 250 return new URI(value.toString()); 251 } 252 return null; 253 } 254 255 private static String convertToString(Object value, Class type) throws URISyntaxException { 256 PropertyEditor editor = PropertyEditorManager.findEditor(type); 257 if (editor != null) { 258 editor.setValue(value); 259 return editor.getAsText(); 260 } 261 if (type == URI.class) { 262 return value.toString(); 263 } 264 return null; 265 } 266 267 private static Set<Method> findSetterMethods(TypeConverter typeConverter, Class clazz, String name, Object value) { 268 Set<Method> candidates = new LinkedHashSet<Method>(); 269 270 // Build the method name. 271 name = "set" + ObjectHelper.capitalize(name); 272 while (clazz != Object.class) { 273 Method[] methods = clazz.getMethods(); 274 for (Method method : methods) { 275 Class params[] = method.getParameterTypes(); 276 if (method.getName().equals(name) && params.length == 1) { 277 Class paramType = params[0]; 278 if (typeConverter != null || isSettableType(paramType) || paramType.isInstance(value)) { 279 candidates.add(method); 280 } 281 } 282 } 283 clazz = clazz.getSuperclass(); 284 } 285 286 if (candidates.isEmpty()) { 287 return candidates; 288 } else if (candidates.size() == 1) { 289 // only one 290 return candidates; 291 } else { 292 // find the best match if possible 293 if (LOG.isTraceEnabled()) { 294 LOG.trace("Found " + candidates.size() + " suitable setter methods for setting " + name); 295 } 296 // perfer to use the one with the same instance if any exists 297 for (Method method : candidates) { 298 if (method.getParameterTypes()[0].isInstance(value)) { 299 if (LOG.isTraceEnabled()) { 300 LOG.trace("Method " + method + " is the best candidate as it has parameter with same instance type"); 301 } 302 // retain only this method in the answer 303 candidates.clear(); 304 candidates.add(method); 305 return candidates; 306 } 307 } 308 // fallback to return what we have found as candidates so far 309 return candidates; 310 } 311 } 312 313 private static boolean isSettableType(Class clazz) { 314 if (PropertyEditorManager.findEditor(clazz) != null) { 315 return true; 316 } 317 if (clazz == URI.class) { 318 return true; 319 } 320 if (clazz == Boolean.class) { 321 return true; 322 } 323 return false; 324 } 325 326 public static String toString(Object target) { 327 return toString(target, Object.class); 328 } 329 330 public static String toString(Object target, Class stopClass) { 331 LinkedHashMap map = new LinkedHashMap(); 332 addFields(target, target.getClass(), stopClass, map); 333 StringBuffer buffer = new StringBuffer(simpleName(target.getClass())); 334 buffer.append(" {"); 335 Set entrySet = map.entrySet(); 336 boolean first = true; 337 for (Iterator iter = entrySet.iterator(); iter.hasNext();) { 338 Map.Entry entry = (Map.Entry)iter.next(); 339 if (first) { 340 first = false; 341 } else { 342 buffer.append(", "); 343 } 344 buffer.append(entry.getKey()); 345 buffer.append(" = "); 346 appendToString(buffer, entry.getValue()); 347 } 348 buffer.append("}"); 349 return buffer.toString(); 350 } 351 352 protected static void appendToString(StringBuffer buffer, Object value) { 353 // if (value instanceof ActiveMQDestination) { 354 // ActiveMQDestination destination = (ActiveMQDestination) value; 355 // buffer.append(destination.getQualifiedName()); 356 // } 357 // else { 358 buffer.append(value); 359 // } 360 } 361 362 public static String simpleName(Class clazz) { 363 String name = clazz.getName(); 364 int p = name.lastIndexOf("."); 365 if (p >= 0) { 366 name = name.substring(p + 1); 367 } 368 return name; 369 } 370 371 private static void addFields(Object target, Class startClass, Class stopClass, LinkedHashMap map) { 372 if (startClass != stopClass) { 373 addFields(target, startClass.getSuperclass(), stopClass, map); 374 } 375 376 Field[] fields = startClass.getDeclaredFields(); 377 for (Field field : fields) { 378 if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) 379 || Modifier.isPrivate(field.getModifiers())) { 380 continue; 381 } 382 383 try { 384 field.setAccessible(true); 385 Object o = field.get(target); 386 if (o != null && o.getClass().isArray()) { 387 try { 388 o = Arrays.asList((Object[])o); 389 } catch (Throwable e) { 390 // ignore 391 } 392 } 393 map.put(field.getName(), o); 394 } catch (Throwable e) { 395 LOG.debug("Error adding fields", e); 396 } 397 } 398 } 399 400 }