Search in sources :

Example 1 with ObjectHelper

use of org.apache.camel.util.ObjectHelper in project camel by apache.

the class BeanInfo method createInvocation.

private MethodInvocation createInvocation(Object pojo, Exchange exchange, Method explicitMethod) throws AmbiguousMethodCallException, MethodNotFoundException {
    MethodInfo methodInfo = null;
    // find the explicit method to invoke
    if (explicitMethod != null) {
        for (List<MethodInfo> infos : operations.values()) {
            for (MethodInfo info : infos) {
                if (explicitMethod.equals(info.getMethod())) {
                    return info.createMethodInvocation(pojo, exchange);
                }
            }
        }
        throw new MethodNotFoundException(exchange, pojo, explicitMethod.getName());
    }
    String methodName = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class);
    if (methodName != null) {
        // do not use qualifier for name
        String name = methodName;
        if (methodName.contains("(")) {
            name = ObjectHelper.before(methodName, "(");
            // the must be a ending parenthesis
            if (!methodName.endsWith(")")) {
                throw new IllegalArgumentException("Method should end with parenthesis, was " + methodName);
            }
            // (we can use betweenOuterPair as it return null if the syntax is invalid)
            if (ObjectHelper.betweenOuterPair(methodName, '(', ')') == null) {
                throw new IllegalArgumentException("Method should have even pair of parenthesis, was " + methodName);
            }
        }
        boolean emptyParameters = methodName.endsWith("()");
        // for example to log the class type or the likes
        if ("class".equals(name) || "getClass".equals(name)) {
            try {
                Method method = pojo.getClass().getMethod("getClass");
                methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method, Collections.<ParameterInfo>emptyList(), Collections.<ParameterInfo>emptyList(), false, false);
            } catch (NoSuchMethodException e) {
                throw new MethodNotFoundException(exchange, pojo, "getClass");
            }
        // special for length on an array type
        } else if ("length".equals(name) && pojo.getClass().isArray()) {
            try {
                // need to use arrayLength method from ObjectHelper as Camel's bean OGNL support is method invocation based
                // and not for accessing fields. And hence we need to create a MethodInfo instance with a method to call
                // and therefore use arrayLength from ObjectHelper to return the array length field.
                Method method = ObjectHelper.class.getMethod("arrayLength", Object[].class);
                ParameterInfo pi = new ParameterInfo(0, Object[].class, null, ExpressionBuilder.mandatoryBodyExpression(Object[].class, true));
                List<ParameterInfo> lpi = new ArrayList<ParameterInfo>(1);
                lpi.add(pi);
                methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method, lpi, lpi, false, false);
                // Need to update the message body to be pojo for the invocation
                exchange.getIn().setBody(pojo);
            } catch (NoSuchMethodException e) {
                throw new MethodNotFoundException(exchange, pojo, "getClass");
            }
        } else {
            List<MethodInfo> methods = getOperations(name);
            if (methods != null && methods.size() == 1) {
                // only one method then choose it
                methodInfo = methods.get(0);
                // validate that if we want an explicit no-arg method, then that's what we get
                if (emptyParameters && methodInfo.hasParameters()) {
                    throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)");
                }
            } else if (methods != null) {
                // there are more methods with that name so we cannot decide which to use
                // but first let's try to choose a method and see if that complies with the name
                // must use the method name which may have qualifiers
                methodInfo = chooseMethod(pojo, exchange, methodName);
                // validate that if we want an explicit no-arg method, then that's what we get
                if (emptyParameters) {
                    if (methodInfo == null || methodInfo.hasParameters()) {
                        // we could not find a no-arg method with that name
                        throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)");
                    }
                }
                if (methodInfo == null || (name != null && !name.equals(methodInfo.getMethod().getName()))) {
                    throw new AmbiguousMethodCallException(exchange, methods);
                }
            } else {
                // a specific method was given to invoke but not found
                throw new MethodNotFoundException(exchange, pojo, methodName);
            }
        }
    }
    if (methodInfo == null) {
        // no name or type
        methodInfo = chooseMethod(pojo, exchange, null);
    }
    if (methodInfo == null) {
        methodInfo = defaultMethod;
    }
    if (methodInfo != null) {
        LOG.trace("Chosen method to invoke: {} on bean: {}", methodInfo, pojo);
        return methodInfo.createMethodInvocation(pojo, exchange);
    }
    LOG.debug("Cannot find suitable method to invoke on bean: {}", pojo);
    return null;
}
Also used : ObjectHelper(org.apache.camel.util.ObjectHelper) Method(java.lang.reflect.Method) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ObjectHelper

use of org.apache.camel.util.ObjectHelper in project camel by apache.

the class EtcdServiceDiscovery method getServices.

protected List<ServiceDefinition> getServices(Predicate<EtcdServiceDefinition> filter) {
    List<ServiceDefinition> servers = Collections.emptyList();
    if (isRunAllowed()) {
        try {
            final EtcdConfiguration conf = getConfiguration();
            final EtcdKeyGetRequest request = getClient().get(conf.getServicePath()).recursive();
            if (conf.hasTimeout()) {
                request.timeout(conf.getTimeout(), TimeUnit.SECONDS);
            }
            final EtcdKeysResponse response = request.send().get();
            if (Objects.nonNull(response.node) && !response.node.nodes.isEmpty()) {
                servers = response.node.nodes.stream().map(node -> node.value).filter(ObjectHelper::isNotEmpty).map(this::nodeFromString).filter(Objects::nonNull).filter(filter).sorted(EtcdServiceDefinition.COMPARATOR).collect(Collectors.toList());
            }
        } catch (Exception e) {
            throw new RuntimeCamelException(e);
        }
    }
    return servers;
}
Also used : EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Logger(org.slf4j.Logger) EtcdClient(mousio.etcd4j.EtcdClient) Predicate(java.util.function.Predicate) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) EtcdConfiguration(org.apache.camel.component.etcd.EtcdConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EtcdKeyGetRequest(mousio.etcd4j.requests.EtcdKeyGetRequest) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) DefaultServiceDiscovery(org.apache.camel.impl.cloud.DefaultServiceDiscovery) ObjectHelper(org.apache.camel.util.ObjectHelper) Collections(java.util.Collections) EtcdHelper(org.apache.camel.component.etcd.EtcdHelper) ObjectHelper(org.apache.camel.util.ObjectHelper) EtcdKeyGetRequest(mousio.etcd4j.requests.EtcdKeyGetRequest) EtcdConfiguration(org.apache.camel.component.etcd.EtcdConfiguration) Objects(java.util.Objects) EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Aggregations

List (java.util.List)2 ObjectHelper (org.apache.camel.util.ObjectHelper)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Objects (java.util.Objects)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 EtcdClient (mousio.etcd4j.EtcdClient)1 EtcdKeyGetRequest (mousio.etcd4j.requests.EtcdKeyGetRequest)1 EtcdKeysResponse (mousio.etcd4j.responses.EtcdKeysResponse)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 ServiceDefinition (org.apache.camel.cloud.ServiceDefinition)1 EtcdConfiguration (org.apache.camel.component.etcd.EtcdConfiguration)1 EtcdHelper (org.apache.camel.component.etcd.EtcdHelper)1 DefaultServiceDiscovery (org.apache.camel.impl.cloud.DefaultServiceDiscovery)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1