|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * The Universal Permissive License (UPL), Version 1.0 |
| 4 | + */ |
| 5 | +package oracle.weblogic.deploy.util; |
| 6 | + |
| 7 | +import oracle.weblogic.deploy.aliases.TypeUtils; |
| 8 | +import oracle.weblogic.deploy.exception.ExceptionHelper; |
| 9 | + |
| 10 | +import java.lang.reflect.Array; |
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * Utility methods for configuring custom MBeans. |
| 17 | + */ |
| 18 | +public final class CustomBeanUtils { |
| 19 | + |
| 20 | + private CustomBeanUtils() { |
| 21 | + // hide the constructor for this utility class |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Invoke the specified set method on the MBean with the provided value, converted to the desired type. |
| 26 | + * The conversion and invocation are done together to avoid automatic Jython data conversion. |
| 27 | + * |
| 28 | + * @param mbean the value to be converted |
| 29 | + * @param propertyType the class representing the target type |
| 30 | + * @param propertyValue the class representing the target type |
| 31 | + * @throws IllegalAccessException if method invocation fails |
| 32 | + * @throws IllegalArgumentException if the data conversion or method invocation fails |
| 33 | + * @throws InvocationTargetException if method invocation fails |
| 34 | + */ |
| 35 | + public static void callMethod(Object mbean, Method method, Class<?> propertyType, Object propertyValue) |
| 36 | + throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { |
| 37 | + |
| 38 | + // convert the specified property value to the desired type |
| 39 | + Object setValue = CustomBeanUtils.convertValue(propertyValue, propertyType); |
| 40 | + |
| 41 | + // call the setter with the target value |
| 42 | + method.invoke(mbean, setValue); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Convert the specified value to the specified type. |
| 47 | + * The conversions and available types are specific to user-defined custom mbeans. |
| 48 | + * |
| 49 | + * @param value the value to be converted |
| 50 | + * @param dataType the class representing the target type |
| 51 | + * @throws IllegalArgumentException if the data conversion fails |
| 52 | + * @return the value converted to the new type, or null if conversion failed |
| 53 | + */ |
| 54 | + static Object convertValue(Object value, Class<?> dataType) { |
| 55 | + // package private to allow unit test access |
| 56 | + |
| 57 | + Object result; |
| 58 | + |
| 59 | + if (Object[].class.isAssignableFrom(dataType)) { |
| 60 | + result = convertArrayValue(value, dataType); |
| 61 | + } else { |
| 62 | + result = convertSingleValue(value, dataType); |
| 63 | + } |
| 64 | + |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Convert the specified array value to the specified type. |
| 70 | + * This may require rebuilding the array with the desired type. |
| 71 | + * |
| 72 | + * @param value the value to be converted |
| 73 | + * @param dataType the class representing the target type |
| 74 | + * @throws IllegalArgumentException if the data conversion fails |
| 75 | + * @return the value converted to the new type, or null if conversion failed |
| 76 | + */ |
| 77 | + private static Object convertArrayValue(Object value, Class<?> dataType) { |
| 78 | + Class<?> componentType = dataType.getComponentType(); |
| 79 | + Object[] result; |
| 80 | + |
| 81 | + if (dataType.isAssignableFrom(value.getClass())) { |
| 82 | + // the value may already match the target type |
| 83 | + result = (Object[]) value; |
| 84 | + |
| 85 | + } else { |
| 86 | + // rebuild the array with the target component type and converted elements |
| 87 | + Object[] source; |
| 88 | + |
| 89 | + if(Object[].class.isAssignableFrom(value.getClass())) { |
| 90 | + source = (Object[]) value; |
| 91 | + |
| 92 | + } else if(value instanceof List) { |
| 93 | + // probably a python PyList from model |
| 94 | + source = ((List) value).toArray(); |
| 95 | + |
| 96 | + } else { |
| 97 | + String text = value.toString(); |
| 98 | + source = text.split(TypeUtils.DEFAULT_STRING_LIST_DELIMITER); |
| 99 | + } |
| 100 | + |
| 101 | + Object arrayObject = Array.newInstance(componentType, source.length); |
| 102 | + |
| 103 | + for (int i = 0; i < source.length; i++) { |
| 104 | + Object elementValue = convertSingleValue(source[i], componentType); |
| 105 | + Array.set(arrayObject, i, elementValue); |
| 106 | + } |
| 107 | + |
| 108 | + result = (Object[]) arrayObject; |
| 109 | + } |
| 110 | + |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Convert the specified single value to the specified type. |
| 116 | + * This invokes the low-level alias conversion methods, with a few adjustments. |
| 117 | + * |
| 118 | + * @param value the value to be converted |
| 119 | + * @param dataType the class representing the target type |
| 120 | + * @throws IllegalArgumentException if the data type is not recognized |
| 121 | + * @return the value converted to the new type, or null if conversion failed |
| 122 | + */ |
| 123 | + private static Object convertSingleValue(Object value, Class<?> dataType) { |
| 124 | + if (value == null) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + String textValue; |
| 129 | + if (value instanceof char[]) { |
| 130 | + textValue = String.valueOf((char[]) value); |
| 131 | + } else { |
| 132 | + textValue = value.toString().trim(); |
| 133 | + if (textValue.length() == 0) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // This block of code is similar to the conversion in TypeUtils.convertToType(), but with some differences. |
| 139 | + // Custom MBeans only allow a subset of the data types of full alias conversion, and the Java types have to |
| 140 | + // be strictly maintained to be passed the the reflected set methods. In addition, convertToType() does not |
| 141 | + // convert arrays to the proper data type, so that is handled at a more granular level in this class. |
| 142 | + |
| 143 | + Object result; |
| 144 | + |
| 145 | + try { |
| 146 | + if (dataType == String.class) { |
| 147 | + result = textValue; |
| 148 | + |
| 149 | + } else if((dataType == Boolean.class) || (dataType == Boolean.TYPE)) { |
| 150 | + String booleanText = TypeUtils.convertToBoolean(textValue); |
| 151 | + result = Boolean.parseBoolean(booleanText); |
| 152 | + |
| 153 | + } else if(dataType == Integer.class) { |
| 154 | + result = Integer.valueOf(textValue); |
| 155 | + |
| 156 | + } else if(dataType == Short.class) { |
| 157 | + result = Short.valueOf(textValue); |
| 158 | + |
| 159 | + } else if(dataType == Long.class) { |
| 160 | + result = Long.valueOf(textValue); |
| 161 | + |
| 162 | + } else if(dataType == Float.class) { |
| 163 | + result = Float.valueOf(textValue); |
| 164 | + |
| 165 | + } else if(dataType == Double.class) { |
| 166 | + result = Double.valueOf(textValue); |
| 167 | + |
| 168 | + } else if(dataType == Character.class) { |
| 169 | + result = TypeUtils.convertToCharacter(textValue); |
| 170 | + |
| 171 | + } else { |
| 172 | + String message = ExceptionHelper.getMessage("WLSDPLY-12132", dataType); |
| 173 | + throw new IllegalArgumentException(message); |
| 174 | + } |
| 175 | + |
| 176 | + } catch(NumberFormatException nfe) { |
| 177 | + String message = ExceptionHelper.getMessage("WLSDPLY-12133", textValue, dataType); |
| 178 | + throw new IllegalArgumentException(message); |
| 179 | + } |
| 180 | + |
| 181 | + return result; |
| 182 | + } |
| 183 | +} |
0 commit comments