mirror of https://github.com/apache/nifi.git
NIFI-4918: looping over several methods to try and fit the dynamic attribute.
If failed, use first method and throw error if not working. Array.length can never be less then 0 This closes #2499. Signed-off-by: Mark Payne <markap14@hotmail.com>
This commit is contained in:
parent
8b2c5b7246
commit
9cd0aab696
|
@ -138,7 +138,6 @@ public class JMSConnectionFactoryProvider extends AbstractControllerService impl
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return new instance of {@link ConnectionFactory}
|
* @return new instance of {@link ConnectionFactory}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -187,7 +186,7 @@ public class JMSConnectionFactoryProvider extends AbstractControllerService impl
|
||||||
* service configuration. For example, 'channel' property will correspond to
|
* service configuration. For example, 'channel' property will correspond to
|
||||||
* 'setChannel(..) method and 'queueManager' property will correspond to
|
* 'setChannel(..) method and 'queueManager' property will correspond to
|
||||||
* setQueueManager(..) method with a single argument.
|
* setQueueManager(..) method with a single argument.
|
||||||
*
|
* <p>
|
||||||
* There are also few adjustments to accommodate well known brokers. For
|
* There are also few adjustments to accommodate well known brokers. For
|
||||||
* example ActiveMQ ConnectionFactory accepts address of the Message Broker
|
* example ActiveMQ ConnectionFactory accepts address of the Message Broker
|
||||||
* in a form of URL while IBMs in the form of host/port pair (more common).
|
* in a form of URL while IBMs in the form of host/port pair (more common).
|
||||||
|
@ -243,7 +242,7 @@ public class JMSConnectionFactoryProvider extends AbstractControllerService impl
|
||||||
* 'propertyName'. For example, 'channel' property will correspond to
|
* 'propertyName'. For example, 'channel' property will correspond to
|
||||||
* 'setChannel(..) method and 'queueManager' property will correspond to
|
* 'setChannel(..) method and 'queueManager' property will correspond to
|
||||||
* setQueueManager(..) method with a single argument.
|
* setQueueManager(..) method with a single argument.
|
||||||
*
|
* <p>
|
||||||
* NOTE: There is a limited type conversion to accommodate property value
|
* NOTE: There is a limited type conversion to accommodate property value
|
||||||
* types since all NiFi configuration properties comes as String. It is
|
* types since all NiFi configuration properties comes as String. It is
|
||||||
* accomplished by checking the argument type of the method and executing
|
* accomplished by checking the argument type of the method and executing
|
||||||
|
@ -257,21 +256,26 @@ public class JMSConnectionFactoryProvider extends AbstractControllerService impl
|
||||||
*/
|
*/
|
||||||
private void setProperty(String propertyName, Object propertyValue) {
|
private void setProperty(String propertyName, Object propertyValue) {
|
||||||
String methodName = this.toMethodName(propertyName);
|
String methodName = this.toMethodName(propertyName);
|
||||||
Method method = Utils.findMethod(methodName, this.connectionFactory.getClass());
|
Method[] methods = Utils.findMethods(methodName, this.connectionFactory.getClass());
|
||||||
if (method != null) {
|
if (methods != null && methods.length > 0) {
|
||||||
try {
|
try {
|
||||||
|
for (Method method : methods) {
|
||||||
Class<?> returnType = method.getParameterTypes()[0];
|
Class<?> returnType = method.getParameterTypes()[0];
|
||||||
if (String.class.isAssignableFrom(returnType)) {
|
if (String.class.isAssignableFrom(returnType)) {
|
||||||
method.invoke(this.connectionFactory, propertyValue);
|
method.invoke(this.connectionFactory, propertyValue);
|
||||||
|
return;
|
||||||
} else if (int.class.isAssignableFrom(returnType)) {
|
} else if (int.class.isAssignableFrom(returnType)) {
|
||||||
method.invoke(this.connectionFactory, Integer.parseInt((String) propertyValue));
|
method.invoke(this.connectionFactory, Integer.parseInt((String) propertyValue));
|
||||||
|
return;
|
||||||
} else if (long.class.isAssignableFrom(returnType)) {
|
} else if (long.class.isAssignableFrom(returnType)) {
|
||||||
method.invoke(this.connectionFactory, Long.parseLong((String) propertyValue));
|
method.invoke(this.connectionFactory, Long.parseLong((String) propertyValue));
|
||||||
|
return;
|
||||||
} else if (boolean.class.isAssignableFrom(returnType)) {
|
} else if (boolean.class.isAssignableFrom(returnType)) {
|
||||||
method.invoke(this.connectionFactory, Boolean.parseBoolean((String) propertyValue));
|
method.invoke(this.connectionFactory, Boolean.parseBoolean((String) propertyValue));
|
||||||
} else {
|
return;
|
||||||
method.invoke(this.connectionFactory, propertyValue);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
methods[0].invoke(this.connectionFactory, propertyValue);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalStateException("Failed to set property " + propertyName, e);
|
throw new IllegalStateException("Failed to set property " + propertyName, e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@ import java.io.File;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -68,6 +71,36 @@ public final class Utils {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a method by name on the target class. If more then one method
|
||||||
|
* present it will return the first one encountered.
|
||||||
|
*
|
||||||
|
* @param name method name
|
||||||
|
* @param targetClass instance of target class
|
||||||
|
* @return Array of {@link Method}
|
||||||
|
*/
|
||||||
|
public static Method[] findMethods(String name, Class<?> targetClass) {
|
||||||
|
Class<?> searchType = targetClass;
|
||||||
|
ArrayList<Method> fittingMethods = new ArrayList<>();
|
||||||
|
while (searchType != null) {
|
||||||
|
Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods());
|
||||||
|
for (Method method : methods) {
|
||||||
|
if (name.equals(method.getName())) {
|
||||||
|
fittingMethods.add(method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
searchType = searchType.getSuperclass();
|
||||||
|
}
|
||||||
|
if (fittingMethods.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
//Sort so that in case there are two methods that accept the parameter type
|
||||||
|
//as first param use the one which accepts fewer parameters in total
|
||||||
|
Collections.sort(fittingMethods, Comparator.comparing(Method::getParameterCount));
|
||||||
|
return fittingMethods.toArray(new Method[fittingMethods.size()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds content of the directory specified with 'path' to the classpath. It
|
* Adds content of the directory specified with 'path' to the classpath. It
|
||||||
* does so by creating a new instance of the {@link URLClassLoader} using
|
* does so by creating a new instance of the {@link URLClassLoader} using
|
||||||
|
|
Loading…
Reference in New Issue