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