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