Skip to content

Commit

Permalink
Cleanup in SetMethodAction
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent e4752ad commit 71a23f7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,80 +60,77 @@ public SetMethodAction(Object bean, Set<P> props) {
*/
@Override
public Void run() throws Exception {
Iterator<P> it = props.iterator();
methods = bean.getClass().getMethods();
final Iterator<P> it = props.iterator();
while (it.hasNext()) {
EnvironmentProperty prop = it.next();
String propName = prop.getName();
Class<?> type = getTypeOf(prop);
//If there were no getter, use the EnvironmentProperty's
//property type
if (type == null) {
type = Class.forName(prop.getType());
final EnvironmentProperty prop = it.next();
final String resolvedValue = prop.getResolvedValue();
if (resolvedValue == null || resolvedValue.isBlank()) {
return null;
}

if (prop.getResolvedValue() != null && !prop.getResolvedValue().isBlank()) {
Method meth = getMutatorMethod(propName, type);

if (meth == null) {
// log WARNING, deployment can continue.
logger.log(Level.WARNING, "rardeployment.no_setter_method",
new Object[] {prop.getName(), bean.getClass().getName()});
} else {
try {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"Invoking" + meth + " on " + bean.getClass().getName() + "with " + "value ["
+ prop.getResolvedValueObject().getClass() + " , " + getFilteredPropValue(prop)
+ " ] ");
}
meth.invoke(bean, new Object[] {prop.getResolvedValueObject()});
} catch (IllegalArgumentException ia) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE,
"IllegalException while trying to set " + prop.getName() + " and value "
+ getFilteredPropValue(prop),
ia + " on an instance of " + bean.getClass()
+ " -- trying again with the type from bean");
}
boolean prevBoundsChecking = Descriptor.isBoundsChecking();
try {
Descriptor.setBoundsChecking(false);
prop.setType(type.getName());
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE,
"2nd try :: Invoking" + meth + " on " + bean.getClass().getName() + "with value ["
+ prop.getResolvedValueObject().getClass() + " , " + getFilteredPropValue(prop)
+ " ] ");
}
meth.invoke(bean, new Object[] {prop.getResolvedValueObject()});
} catch (Exception e) {
handleException(e, prop, bean);
} finally {
// restore boundsChecking
Descriptor.setBoundsChecking(prevBoundsChecking);
}
} catch (Exception ex) {
handleException(ex, prop, bean);
final String propName = prop.getName();
final Class<?> type = getTypeOf(prop);
final Method method = getSetter(propName, type);
if (method == null) {
// log WARNING, deployment can continue.
logger.log(Level.WARNING, "rardeployment.no_setter_method",
new Object[] {prop.getName(), bean.getClass().getName()});
return null;
}
try {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"Invoking" + method + " on " + bean.getClass().getName() + " with value ["
+ prop.getResolvedValueObject().getClass() + " , " + getFilteredPropValue(prop)
+ " ] ");
}
method.invoke(bean, prop.getResolvedValueObject());
} catch (final IllegalArgumentException ia) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE,
"IllegalArgumentException while trying to set " + prop.getName() + " and value "
+ getFilteredPropValue(prop),
ia + " on an instance of " + bean.getClass()
+ " -- trying again with the type from bean");
}
final boolean origBoundsChecking = Descriptor.isBoundsChecking();
try {
Descriptor.setBoundsChecking(false);
prop.setType(type.getName());
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE,
"2nd try :: Invoking" + method + " on " + bean.getClass().getName() + " with value ["
+ prop.getResolvedValueObject().getClass() + " , " + getFilteredPropValue(prop)
+ " ] ");
}
method.invoke(bean, prop.getResolvedValueObject());
} catch (final Exception e) {
throw createException(e, prop);
} finally {
// restore boundsChecking
Descriptor.setBoundsChecking(origBoundsChecking);
}
} catch (final Exception e) {
throw createException(e, prop);
}
}
return null;
}


private void handleException(Exception ex, EnvironmentProperty prop, Object bean) throws ConnectorRuntimeException {
private ConnectorRuntimeException createException(Exception ex, EnvironmentProperty prop) {
logger.log(Level.WARNING, "rardeployment.exception_on_invoke_setter",
new Object[] {prop.getName(), getFilteredPropValue(prop), ex.getMessage()});
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE,
"Exception while trying to set " + prop.getName() + " and value " + getFilteredPropValue(prop),
ex + " on an instance of " + bean.getClass());
}
throw (ConnectorRuntimeException) (new ConnectorRuntimeException(ex.getMessage()).initCause(ex));
return new ConnectorRuntimeException(ex.getMessage(), ex);
}


private static String getFilteredPropValue(EnvironmentProperty prop) {
if (prop == null) {
return "null";
Expand All @@ -144,48 +141,44 @@ private static String getFilteredPropValue(EnvironmentProperty prop) {
return "********";
}

return (prop.getResolvedValue());
return prop.getResolvedValue();
}


/**
* Retrieves the appropriate setter method in the resurce adapter java bean
* class
* Retrieves the appropriate setter method in the resurce adapter java bean class
*/
private Method getMutatorMethod(String propertyName, Class type) {
private Method getSetter(String propertyName, Class<?> parameterType) {
// Get all setter methods for property
String setterMethodName = "set" + getCamelCasedPropertyName(propertyName);
Method m = null;

//Get all setter methods for property
Method[] setterMethods = findMethod(setterMethodName);

Method method = null;
if (setterMethods.length == 1) {
//Only one setter method for this property
m = setterMethods[0];
// Only one setter method for this property
method = setterMethods[0];
} else {
//When more than one setter for the property, do type
//checking to determine property
//This check is very important, because the resource
//adapter java-bean's methods might be overridden and calling
//set over the wrong method will result in an exception
for (int i = 0; i < setterMethods.length; i++) {
Class<?>[] paramTypes = setterMethods[i].getParameterTypes();
// When more than one setter for the property, do type
// checking to determine property
// This check is very important, because the resource
// adapter java-bean's methods might be overridden and calling
// set over the wrong method will result in an exception
for (Method setterMethod : setterMethods) {
Class<?>[] paramTypes = setterMethod.getParameterTypes();
if (paramTypes.length > 0) {
if (paramTypes[0].equals(type) && paramTypes.length == 1) {
if (paramTypes[0].equals(parameterType) && paramTypes.length == 1) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "Method [ " + methods[i] + " ] matches with the right arg type");
logger.log(Level.FINER,
"Method [ " + setterMethod + " ] matches with the right arg type");
}
m = setterMethods[i];
method = setterMethod;
}
}
}
}

if (m != null) {
return m;
if (method == null) {
logger.log(Level.WARNING, "no.such.method", new Object[] {setterMethodName, bean.getClass().getName()});
}
logger.log(Level.WARNING, "no.such.method", new Object[] {setterMethodName, bean.getClass().getName()});
return null;
return method;
}


Expand All @@ -196,8 +189,10 @@ private Method getMutatorMethod(String propertyName, Class type) {
* This helps in ensuring that the type as coded in the java-bean
* is used while setting values on a java-bean instance,
* rather than on the values specified in ra.xml
*
* @throws ClassNotFoundException
*/
private Class<?> getTypeOf(EnvironmentProperty prop) {
private Class<?> getTypeOf(EnvironmentProperty prop) throws ClassNotFoundException {
String name = prop.getName();
Method accessorMeth = getAccessorMethod(name);
if (accessorMeth != null) {
Expand All @@ -207,7 +202,8 @@ private Class<?> getTypeOf(EnvironmentProperty prop) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "method.name.nogetterforproperty", new Object[] {prop.getName(), bean.getClass()});
}
return null;
// If there were no getter, use the EnvironmentProperty's property type
return Class.forName(prop.getType());
}


Expand All @@ -228,9 +224,8 @@ private Method getAccessorMethod(String propertyName) {
|| getterMethodsWithIsPrefix[0].getReturnType().equals(boolean.class))) {

return getterMethodsWithIsPrefix[0];
} else {
return null;
}
return null;
}


Expand Down Expand Up @@ -265,8 +260,6 @@ private Method[] findMethod(String methodName) {
* correct accessor and mutator method names for a give property.
*/
private String getCamelCasedPropertyName(String propertyName) {
return propertyName.substring(0, 1).toUpperCase(locale) +
propertyName.substring(1);
return propertyName.substring(0, 1).toUpperCase(locale) + propertyName.substring(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private Object getObjectFromString(String string, Class type) {
return obj;
}
}
if (string == null || ("".equals(string) && !type.equals(String.class))) {
if (string == null || (string.isEmpty() && !type.equals(String.class))) {
return null;
}
try {
Expand Down

0 comments on commit 71a23f7

Please sign in to comment.