Skip to content

Commit f72a841

Browse files
Merge branch 'Issue#164-support-custom-security-providers' of github.com:oracle/weblogic-deploy-tooling into Issue#164-support-custom-security-providers
2 parents a86efe0 + 5fb9714 commit f72a841

File tree

6 files changed

+303
-56
lines changed

6 files changed

+303
-56
lines changed

core/src/main/java/oracle/weblogic/deploy/aliases/TypeUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* The Universal Permissive License (UPL), Version 1.0
44
*/
55
package oracle.weblogic.deploy.aliases;
@@ -32,7 +32,7 @@ public final class TypeUtils {
3232
private static final String CLASS = TypeUtils.class.getName();
3333
private static final PlatformLogger LOGGER = WLSDeployLogFactory.getLogger("wlsdeploy.util");
3434

35-
private static final String DEFAULT_STRING_LIST_DELIMITOR = ",";
35+
public static final String DEFAULT_STRING_LIST_DELIMITER = ",";
3636

3737
private TypeUtils() {
3838
// hide the constructor for this utility class
@@ -76,7 +76,7 @@ public static Object convertToType(String targetTypeName, Object value) throws A
7676
delimiter = File.pathSeparator;
7777
break;
7878
default:
79-
delimiter = DEFAULT_STRING_LIST_DELIMITOR;
79+
delimiter = DEFAULT_STRING_LIST_DELIMITER;
8080
}
8181
return convertToType(targetTypeName, value, delimiter);
8282
}
@@ -154,7 +154,7 @@ public static Object convertToType(String targetTypeName, Object value, String d
154154
* the conversion.
155155
*/
156156
public static Object convertToType(Class<?> targetType, Object value) throws AliasException {
157-
return convertToType(targetType, value, DEFAULT_STRING_LIST_DELIMITOR);
157+
return convertToType(targetType, value, DEFAULT_STRING_LIST_DELIMITER);
158158
}
159159

160160
/**
@@ -231,7 +231,7 @@ public static Object convertToType(Class<?> targetType, Object value, String del
231231
return result;
232232
}
233233

234-
private static String convertToBoolean(String strValue) {
234+
public static String convertToBoolean(String strValue) {
235235
String result;
236236
switch (strValue.toLowerCase(Locale.ENGLISH)) {
237237
case "1":
@@ -246,7 +246,7 @@ private static String convertToBoolean(String strValue) {
246246
return result;
247247
}
248248

249-
private static Character convertToCharacter(String strValue) {
249+
public static Character convertToCharacter(String strValue) {
250250
Character result = null;
251251
if (strValue.length() > 0) {
252252
result = strValue.charAt(0);

core/src/main/java/oracle/weblogic/deploy/util/ConvertUtils.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
Object result;
139+
140+
try {
141+
if (dataType == String.class) {
142+
result = textValue;
143+
144+
} else if((dataType == Boolean.class) || (dataType == Boolean.TYPE)) {
145+
String booleanText = TypeUtils.convertToBoolean(textValue);
146+
result = Boolean.parseBoolean(booleanText);
147+
148+
} else if(dataType == Integer.class) {
149+
result = Integer.valueOf(textValue);
150+
151+
} else if(dataType == Short.class) {
152+
result = Short.valueOf(textValue);
153+
154+
} else if(dataType == Long.class) {
155+
result = Long.valueOf(textValue);
156+
157+
} else if(dataType == Float.class) {
158+
result = Float.valueOf(textValue);
159+
160+
} else if(dataType == Double.class) {
161+
result = Double.valueOf(textValue);
162+
163+
} else if(dataType == Character.class) {
164+
result = TypeUtils.convertToCharacter(textValue);
165+
166+
} else {
167+
String message = ExceptionHelper.getMessage("WLSDPLY-12132", dataType);
168+
throw new IllegalArgumentException(message);
169+
}
170+
171+
} catch(NumberFormatException nfe) {
172+
String message = ExceptionHelper.getMessage("WLSDPLY-12133", textValue, dataType);
173+
throw new IllegalArgumentException(message);
174+
}
175+
176+
return result;
177+
}
178+
}

core/src/main/python/wlsdeploy/tool/util/custom_folder_helper.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
The Universal Permissive License (UPL), Version 1.0
44
"""
5-
from java.lang import IllegalArgumentException, IllegalAccessException
5+
from java.lang import IllegalArgumentException
6+
from java.lang import IllegalAccessException
67
from java.lang.reflect import InvocationTargetException
7-
from oracle.weblogic.deploy.util import ConvertUtils
8+
from oracle.weblogic.deploy.util import CustomBeanUtils
89

910
from wlsdeploy.exception import exception_helper
1011
from wlsdeploy.tool.util.alias_helper import AliasHelper
@@ -104,21 +105,15 @@ def update_security_folder(self, location, model_category, model_type, model_nam
104105

105106
property_type = parameter_types[0]
106107

107-
# convert the model value to the target type
108-
109-
set_value = ConvertUtils.convertValue(model_value, property_type)
110-
if set_value is None:
111-
ex = exception_helper.create_exception(self.exception_type, 'WLSDPLY-12131', str(model_value),
112-
str(property_type), model_key)
113-
self.logger.throwing(ex, class_name=self.__class_name, method_name=_method_name)
114-
raise ex
115-
116-
# call the setter with the target value
108+
# convert the model value to the target type and call the setter with the target value.
109+
# these are done together in Java to avoid automatic Jython type conversions.
117110

118111
try:
119-
method.invoke(provider_mbean, [set_value])
112+
CustomBeanUtils.callMethod(provider_mbean, method, property_type, model_value)
113+
114+
# failure converting value or calling method
120115
except (IllegalAccessException, IllegalArgumentException, InvocationTargetException), ex:
121-
ex = exception_helper.create_exception(self.exception_type, 'WLSDPLY-12132', str(method),
122-
str(set_value), ex.getLocalizedMessage(), error=ex)
116+
ex = exception_helper.create_exception(self.exception_type, 'WLSDPLY-12131', method,
117+
str(model_value), ex.getLocalizedMessage(), error=ex)
123118
self.logger.throwing(ex, class_name=self.__class_name, method_name=_method_name)
124119
raise ex

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,9 @@ WLSDPLY-12127=Updating attribute {0} to {1}
956956
WLSDPLY-12128=No property descriptor found for attribute {0}
957957
WLSDPLY-12129=Skipping read-only attribute {0}
958958
WLSDPLY-12130=The set method for attribute {0} has {1} arguments
959-
WLSDPLY-12131=Unable to convert value {0} to type {1} for attribute {2}
960-
WLSDPLY-12132=Unable to invoke method {0} with argument {1}: {2}
959+
WLSDPLY-12131=Unable to invoke method "{0}" with argument {1}: {2}
960+
WLSDPLY-12132=Unrecognized data type {0}
961+
WLSDPLY-12133=Unable to convert "{0}" to value of type {1}
961962

962963
# domain_creator.py
963964
WLSDPLY-12200={0} did not find the required {1} section in the model file {2}

0 commit comments

Comments
 (0)