| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.util; |
| 18 |
|
|
| 19 |
|
import java.beans.PropertyEditor; |
| 20 |
|
import java.beans.PropertyEditorManager; |
| 21 |
|
import java.lang.reflect.Field; |
| 22 |
|
import java.lang.reflect.Method; |
| 23 |
|
import java.lang.reflect.Modifier; |
| 24 |
|
import java.net.URI; |
| 25 |
|
import java.net.URISyntaxException; |
| 26 |
|
import java.util.Arrays; |
| 27 |
|
import java.util.HashMap; |
| 28 |
|
import java.util.Iterator; |
| 29 |
|
import java.util.LinkedHashMap; |
| 30 |
|
import java.util.Map; |
| 31 |
|
import java.util.Map.Entry; |
| 32 |
|
import java.util.Set; |
| 33 |
|
|
| 34 |
|
public class IntrospectionSupport { |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
0 |
private IntrospectionSupport() { |
| 40 |
0 |
} |
| 41 |
|
|
| 42 |
|
public static boolean getProperties(Object target, Map props, String optionPrefix) { |
| 43 |
|
|
| 44 |
0 |
boolean rc = false; |
| 45 |
0 |
if (target == null) { |
| 46 |
0 |
throw new IllegalArgumentException("target was null."); |
| 47 |
|
} |
| 48 |
0 |
if (props == null) { |
| 49 |
0 |
throw new IllegalArgumentException("props was null."); |
| 50 |
|
} |
| 51 |
0 |
if (optionPrefix == null) { |
| 52 |
0 |
optionPrefix = ""; |
| 53 |
|
} |
| 54 |
|
|
| 55 |
0 |
Class clazz = target.getClass(); |
| 56 |
0 |
Method[] methods = clazz.getMethods(); |
| 57 |
0 |
for (int i = 0; i < methods.length; i++) { |
| 58 |
0 |
Method method = methods[i]; |
| 59 |
0 |
String name = method.getName(); |
| 60 |
0 |
Class type = method.getReturnType(); |
| 61 |
0 |
Class params[] = method.getParameterTypes(); |
| 62 |
0 |
if (name.startsWith("get") && params.length == 0 && type != null && isSettableType(type)) { |
| 63 |
|
|
| 64 |
|
try { |
| 65 |
|
|
| 66 |
0 |
Object value = method.invoke(target, new Object[] {}); |
| 67 |
0 |
if (value == null) { |
| 68 |
0 |
continue; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
0 |
String strValue = convertToString(value, type); |
| 72 |
0 |
if (strValue == null) { |
| 73 |
0 |
continue; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
0 |
name = name.substring(3, 4).toLowerCase() + name.substring(4); |
| 77 |
0 |
props.put(optionPrefix + name, strValue); |
| 78 |
0 |
rc = true; |
| 79 |
|
|
| 80 |
0 |
} catch (Throwable ignore) { |
| 81 |
0 |
} |
| 82 |
|
|
| 83 |
|
} |
| 84 |
|
} |
| 85 |
|
|
| 86 |
0 |
return rc; |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
public static boolean setProperties(Object target, Map props, String optionPrefix) { |
| 90 |
222 |
boolean rc = false; |
| 91 |
222 |
if (target == null) { |
| 92 |
0 |
throw new IllegalArgumentException("target was null."); |
| 93 |
|
} |
| 94 |
222 |
if (props == null) { |
| 95 |
0 |
throw new IllegalArgumentException("props was null."); |
| 96 |
|
} |
| 97 |
|
|
| 98 |
222 |
for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { |
| 99 |
1110 |
String name = (String)iter.next(); |
| 100 |
1110 |
if (name.startsWith(optionPrefix)) { |
| 101 |
666 |
Object value = props.get(name); |
| 102 |
666 |
name = name.substring(optionPrefix.length()); |
| 103 |
666 |
if (setProperty(target, name, value)) { |
| 104 |
444 |
iter.remove(); |
| 105 |
444 |
rc = true; |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
1110 |
} |
| 109 |
222 |
return rc; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
public static Map extractProperties(Map props, String optionPrefix) { |
| 113 |
30 |
if (props == null) { |
| 114 |
0 |
throw new IllegalArgumentException("props was null."); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
30 |
HashMap rc = new HashMap(props.size()); |
| 118 |
|
|
| 119 |
30 |
for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { |
| 120 |
0 |
String name = (String)iter.next(); |
| 121 |
0 |
if (name.startsWith(optionPrefix)) { |
| 122 |
0 |
Object value = props.get(name); |
| 123 |
0 |
name = name.substring(optionPrefix.length()); |
| 124 |
0 |
rc.put(name, value); |
| 125 |
0 |
iter.remove(); |
| 126 |
|
} |
| 127 |
0 |
} |
| 128 |
|
|
| 129 |
30 |
return rc; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
public static boolean setProperties(Object target, Map props) { |
| 133 |
636 |
boolean rc = false; |
| 134 |
|
|
| 135 |
636 |
if (target == null) { |
| 136 |
0 |
throw new IllegalArgumentException("target was null."); |
| 137 |
|
} |
| 138 |
636 |
if (props == null) { |
| 139 |
0 |
throw new IllegalArgumentException("props was null."); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
636 |
for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) { |
| 143 |
33 |
Map.Entry entry = (Entry)iter.next(); |
| 144 |
33 |
if (setProperty(target, (String)entry.getKey(), entry.getValue())) { |
| 145 |
27 |
iter.remove(); |
| 146 |
27 |
rc = true; |
| 147 |
|
} |
| 148 |
33 |
} |
| 149 |
|
|
| 150 |
636 |
return rc; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
public static boolean setProperty(Object target, String name, Object value) { |
| 154 |
|
try { |
| 155 |
699 |
Class clazz = target.getClass(); |
| 156 |
699 |
Method setter = findSetterMethod(clazz, name); |
| 157 |
699 |
if (setter == null) { |
| 158 |
228 |
return false; |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
471 |
if (value == null || value.getClass() == setter.getParameterTypes()[0]) { |
| 164 |
225 |
setter.invoke(target, new Object[] {value}); |
| 165 |
225 |
} else { |
| 166 |
|
|
| 167 |
246 |
setter.invoke(target, new Object[] {convert(value, setter.getParameterTypes()[0])}); |
| 168 |
|
} |
| 169 |
471 |
return true; |
| 170 |
0 |
} catch (Throwable ignore) { |
| 171 |
0 |
return false; |
| 172 |
|
} |
| 173 |
|
} |
| 174 |
|
|
| 175 |
|
private static Object convert(Object value, Class type) throws URISyntaxException { |
| 176 |
246 |
PropertyEditor editor = PropertyEditorManager.findEditor(type); |
| 177 |
246 |
if (editor != null) { |
| 178 |
246 |
editor.setAsText(value.toString()); |
| 179 |
246 |
return editor.getValue(); |
| 180 |
|
} |
| 181 |
0 |
if (type == URI.class) { |
| 182 |
0 |
return new URI(value.toString()); |
| 183 |
|
} |
| 184 |
0 |
return null; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
private static String convertToString(Object value, Class type) throws URISyntaxException { |
| 188 |
0 |
PropertyEditor editor = PropertyEditorManager.findEditor(type); |
| 189 |
0 |
if (editor != null) { |
| 190 |
0 |
editor.setValue(value); |
| 191 |
0 |
return editor.getAsText(); |
| 192 |
|
} |
| 193 |
0 |
if (type == URI.class) { |
| 194 |
0 |
return ((URI)value).toString(); |
| 195 |
|
} |
| 196 |
0 |
return null; |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
private static Method findSetterMethod(Class clazz, String name) { |
| 200 |
|
|
| 201 |
699 |
name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); |
| 202 |
699 |
Method[] methods = clazz.getMethods(); |
| 203 |
5262 |
for (int i = 0; i < methods.length; i++) { |
| 204 |
5034 |
Method method = methods[i]; |
| 205 |
5034 |
Class params[] = method.getParameterTypes(); |
| 206 |
5034 |
if (method.getName().equals(name) && params.length == 1 && isSettableType(params[0])) { |
| 207 |
471 |
return method; |
| 208 |
|
} |
| 209 |
|
} |
| 210 |
228 |
return null; |
| 211 |
|
} |
| 212 |
|
|
| 213 |
|
private static boolean isSettableType(Class clazz) { |
| 214 |
471 |
if (PropertyEditorManager.findEditor(clazz) != null) { |
| 215 |
471 |
return true; |
| 216 |
|
} |
| 217 |
0 |
if (clazz == URI.class) { |
| 218 |
0 |
return true; |
| 219 |
|
} |
| 220 |
0 |
if (clazz == Boolean.class) { |
| 221 |
0 |
return true; |
| 222 |
|
} |
| 223 |
0 |
return false; |
| 224 |
|
} |
| 225 |
|
|
| 226 |
|
public static String toString(Object target) { |
| 227 |
0 |
return toString(target, Object.class); |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
public static String toString(Object target, Class stopClass) { |
| 231 |
0 |
LinkedHashMap map = new LinkedHashMap(); |
| 232 |
0 |
addFields(target, target.getClass(), stopClass, map); |
| 233 |
0 |
StringBuffer buffer = new StringBuffer(simpleName(target.getClass())); |
| 234 |
0 |
buffer.append(" {"); |
| 235 |
0 |
Set entrySet = map.entrySet(); |
| 236 |
0 |
boolean first = true; |
| 237 |
0 |
for (Iterator iter = entrySet.iterator(); iter.hasNext();) { |
| 238 |
0 |
Map.Entry entry = (Map.Entry)iter.next(); |
| 239 |
0 |
if (first) { |
| 240 |
0 |
first = false; |
| 241 |
0 |
} else { |
| 242 |
0 |
buffer.append(", "); |
| 243 |
|
} |
| 244 |
0 |
buffer.append(entry.getKey()); |
| 245 |
0 |
buffer.append(" = "); |
| 246 |
0 |
appendToString(buffer, entry.getValue()); |
| 247 |
0 |
} |
| 248 |
0 |
buffer.append("}"); |
| 249 |
0 |
return buffer.toString(); |
| 250 |
|
} |
| 251 |
|
|
| 252 |
|
protected static void appendToString(StringBuffer buffer, Object value) { |
| 253 |
|
|
| 254 |
|
|
| 255 |
|
|
| 256 |
|
|
| 257 |
|
|
| 258 |
0 |
buffer.append(value); |
| 259 |
|
|
| 260 |
0 |
} |
| 261 |
|
|
| 262 |
|
public static String simpleName(Class clazz) { |
| 263 |
0 |
String name = clazz.getName(); |
| 264 |
0 |
int p = name.lastIndexOf("."); |
| 265 |
0 |
if (p >= 0) { |
| 266 |
0 |
name = name.substring(p + 1); |
| 267 |
|
} |
| 268 |
0 |
return name; |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
private static void addFields(Object target, Class startClass, Class stopClass, LinkedHashMap map) { |
| 272 |
|
|
| 273 |
0 |
if (startClass != stopClass) { |
| 274 |
0 |
addFields(target, startClass.getSuperclass(), stopClass, map); |
| 275 |
|
} |
| 276 |
|
|
| 277 |
0 |
Field[] fields = startClass.getDeclaredFields(); |
| 278 |
0 |
for (int i = 0; i < fields.length; i++) { |
| 279 |
0 |
Field field = fields[i]; |
| 280 |
0 |
if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || Modifier.isPrivate(field.getModifiers())) { |
| 281 |
0 |
continue; |
| 282 |
|
} |
| 283 |
|
|
| 284 |
|
try { |
| 285 |
0 |
field.setAccessible(true); |
| 286 |
0 |
Object o = field.get(target); |
| 287 |
0 |
if (o != null && o.getClass().isArray()) { |
| 288 |
|
try { |
| 289 |
0 |
o = Arrays.asList((Object[])o); |
| 290 |
0 |
} catch (Throwable e) { |
| 291 |
0 |
} |
| 292 |
|
} |
| 293 |
0 |
map.put(field.getName(), o); |
| 294 |
0 |
} catch (Throwable e) { |
| 295 |
0 |
e.printStackTrace(); |
| 296 |
0 |
} |
| 297 |
|
} |
| 298 |
|
|
| 299 |
0 |
} |
| 300 |
|
|
| 301 |
|
} |