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 */
017package org.apache.activemq.jms.pool;
018
019import java.lang.reflect.Method;
020import java.util.Iterator;
021import java.util.Map;
022import java.util.Map.Entry;
023
024import javax.net.ssl.SSLServerSocket;
025
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029public final class IntrospectionSupport {
030
031    private static final Logger LOG = LoggerFactory.getLogger(IntrospectionSupport.class);
032
033    private IntrospectionSupport() {
034    }
035
036    public static boolean setProperties(Object target, Map props) {
037        boolean rc = false;
038
039        if (target == null) {
040            throw new IllegalArgumentException("target was null.");
041        }
042        if (props == null) {
043            throw new IllegalArgumentException("props was null.");
044        }
045
046        for (Iterator<?> iter = props.entrySet().iterator(); iter.hasNext();) {
047            Entry<?,?> entry = (Entry<?,?>)iter.next();
048            if (setProperty(target, (String)entry.getKey(), entry.getValue())) {
049                iter.remove();
050                rc = true;
051            }
052        }
053
054        return rc;
055    }
056
057    public static boolean setProperty(Object target, String name, Object value) {
058        try {
059            Class<?> clazz = target.getClass();
060            if (target instanceof SSLServerSocket) {
061                // overcome illegal access issues with internal implementation class
062                clazz = SSLServerSocket.class;
063            }
064            Method setter = findSetterMethod(clazz, name);
065            if (setter == null) {
066                return false;
067            }
068
069            // JDK 11: class or setter might not be publicly accessible
070            setter.setAccessible(true);
071
072            // If the type is null or it matches the needed type, just use the
073            // value directly
074            if (value == null || value.getClass() == setter.getParameterTypes()[0]) {
075                setter.invoke(target, value);
076            } else {
077                // We need to convert it
078                setter.invoke(target, convert(value, setter.getParameterTypes()[0]));
079            }
080            return true;
081        } catch (Exception e) {
082            LOG.error(String.format("Could not set property %s on %s", name, target), e);
083            return false;
084        }
085    }
086
087    private static Object convert(Object value, Class to) {
088        if (value == null) {
089            // lets avoid NullPointerException when converting to boolean for null values
090            if (boolean.class.isAssignableFrom(to)) {
091                return Boolean.FALSE;
092            }
093            return null;
094        }
095
096        // eager same instance type test to avoid the overhead of invoking the type converter
097        // if already same type
098        if (to.isAssignableFrom(value.getClass())) {
099            return to.cast(value);
100        }
101
102        if ((boolean.class.isAssignableFrom(to) || Boolean.class.isAssignableFrom(to)) && value instanceof String) {
103            return Boolean.valueOf((String)value);
104        }
105
106        throw new IllegalArgumentException("Cannot convert from " + value.getClass()
107                    + " to " + to + " with value " + value);
108    }
109
110    private static Method findSetterMethod(Class clazz, String name) {
111        // Build the method name.
112        name = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
113        Method[] methods = clazz.getMethods();
114        for (Method method : methods) {
115            Class<?> params[] = method.getParameterTypes();
116            if (method.getName().equals(name) && params.length == 1 ) {
117                return method;
118            }
119        }
120        return null;
121    }
122
123}