/* * Copyright (C) 2004-2008 Jive Software, 2017-2025 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jivesoftware.util; import java.awt.Color; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * A utility class that provides methods that are useful for dealing with * Java Beans. */ public class BeanUtils { private static final Logger Log = LoggerFactory.getLogger(BeanUtils.class); /** * The date format recognized for parsing/formatting dates. */ public static final String DATE_FORMAT = "MM/dd/yyyy"; private static DateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT); /** * Sets the properties of a Java Bean based on the String name/value pairs in * the specified Map. Because this method has to know how to convert a * String value into the correct type for the bean, only a few bean property * types are supported. They are: String, boolean, int, long, float, double, * Color, and Class.

* * If key/value pairs exist in the Map that don't correspond to properties * of the bean, they will be ignored. * * @param bean the JavaBean to set properties on. * @param properties String name/value pairs of the properties to set. */ public static void setProperties(Object bean, Map properties) { try { // Loop through all the property names in the Map for (String propName : properties.keySet()) { try { // Create a property descriptor for the named property. If // the bean doesn't have the named property, an // Introspection will be thrown. PropertyDescriptor descriptor = new PropertyDescriptor( propName, bean.getClass()); // Load the class type of the property. Class propertyType = descriptor.getPropertyType(); // Get the value of the property by converting it from a // String to the correct object type. Object value = decode(propertyType, properties.get(propName)); // Set the value of the bean. descriptor.getWriteMethod().invoke(bean, value); } catch (IntrospectionException ie) { // Ignore. This exception means that the key in the map // does not correspond to a property of the bean. } catch (InvocationTargetException ite) { // Ignore. This exception most often occurs when a // value in the map is null and the target method doesn't // support null properties. } } } catch (Exception e) { Log.error(e.getMessage(), e); } } /** * Sets the properties of a Java Bean based on the request's properties. Because * this method has to know how to convert a String value into the correct type * for the bean, only a few bean property types are supported. They are: String, * boolean, int, long, float, double, Color, and Class.

* * If key/value pairs exist in the Map that don't correspond to properties * of the bean, they will be ignored. * * @param bean the JavaBean to set properties on. * @param request the HTTP request. */ public static void setProperties(Object bean, HttpServletRequest request) { for (Enumeration propNames = request.getParameterNames(); propNames.hasMoreElements();) { String propName = propNames.nextElement(); try { // Create a property descriptor for the named property. If // the bean doesn't have the named property, an // Introspection will be thrown. PropertyDescriptor descriptor = new PropertyDescriptor( propName, bean.getClass()); // Load the class type of the property. Class propertyType = descriptor.getPropertyType(); // Get the value of the property by converting it from a // String to the correct object type. Object value = decode(propertyType, request.getParameter(propName)); // Set the value of the bean. descriptor.getWriteMethod().invoke(bean, value); } catch (IntrospectionException ie) { // Ignore. This exception means that the key in the map // does not correspond to a property of the bean. } catch (InvocationTargetException ite) { // Ignore. This exception most often occurs when a // value in the map is null and the target method doesn't // support null properties. } catch (IllegalAccessException e) { Log.error(e.getMessage(), e); } catch (Exception e) { Log.error(e.getMessage(), e); } } } /** * Gets the properties from a Java Bean and returns them in a Map of String * name/value pairs. Because this method has to know how to convert a * bean property into a String value, only a few bean property * types are supported. They are: String, boolean, int, long, float, double, * Color, and Class. * * @param bean a Java Bean to get properties from. * @return a Map of all properties as String name/value pairs. */ public static Map getProperties(Object bean) { Map properties = new HashMap<>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); // Loop through all properties of the bean. PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); String [] names = new String[descriptors.length]; for (int i=0; i