001    /** 
002     * <a href="http://activemq.org">ActiveMQ: The Open Source Message Fabric</a>
003     * 
004     * Copyright 2004 Protique Ltd
005     * 
006     * Licensed under the Apache License, Version 2.0 (the "License"); 
007     * you may not use this file except in compliance with the License. 
008     * You may obtain a copy of the License at 
009     * 
010     * http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS, 
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
015     * See the License for the specific language governing permissions and 
016     * limitations under the License. 
017     * 
018     **/
019    package org.activemq.util;
020    
021    import java.beans.PropertyEditor;
022    import java.beans.PropertyEditorManager;
023    import java.lang.reflect.Method;
024    import java.net.URI;
025    import java.net.URISyntaxException;
026    import java.util.Iterator;
027    import java.util.Map;
028    
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    
032    public class ReflectionSupport {
033        
034        private static final Log log = LogFactory.getLog(ReflectionSupport.class);
035        
036        public static void setProperties(Object target, Map props) {
037            if( target == null )
038                throw new IllegalArgumentException("target was null.");
039            if( props == null )
040                throw new IllegalArgumentException("props was null.");
041            
042            for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
043                String name = (String) iter.next();
044                Object value = props.get(name);
045                setProperty(target, name, value);
046            }
047        }
048    
049        private static void setProperty(Object target, String name, Object value) {
050            Class clazz = target.getClass();
051            Method setter = findSetterMethod(clazz, name);        
052            try {
053                // If the type is null or it matches the needed type, just use the value directly
054                if( value == null || value.getClass()==setter.getParameterTypes()[0] ) {
055                    setter.invoke(target, new Object[]{value});
056                } else {
057                    // We need to convert it
058                    setter.invoke(target, new Object[]{ convert(value, setter.getParameterTypes()[0]) });
059                }
060            } catch (Throwable e) {
061                log.debug("Could not set property: "+name+", value: "+value);
062            }
063        }
064    
065        private static Object convert(Object value, Class type) throws URISyntaxException {
066            PropertyEditor editor = PropertyEditorManager.findEditor(type);
067            if( editor != null ) { 
068                editor.setAsText(value.toString());
069                return editor.getValue();
070            }
071            if( type == URI.class ) {
072                return new URI(value.toString());
073            }
074            return null;
075        }
076    
077        private static Method findSetterMethod(Class clazz, String name) {
078            // Build the method name.
079            name = "set"+name.substring(0,1).toUpperCase()+name.substring(1);
080            Method[] methods = clazz.getMethods();
081            for (int i = 0; i < methods.length; i++) {
082                Method method = methods[i];
083                Class params[] = method.getParameterTypes();
084                if( method.getName().equals(name) 
085                        && params.length==1
086                        && isSettableType(params[0])) {
087                    return method;
088                }
089            }
090            return null;
091        }
092    
093        private static boolean isSettableType(Class clazz) {
094            if( PropertyEditorManager.findEditor(clazz)!=null )
095                return true;
096            if( clazz == URI.class )
097                return true;
098            return false;
099        }
100        
101    }