Search in sources :

Example 1 with MethodInvocationException

use of joynr.exceptions.MethodInvocationException in project joynr by bmwcarit.

the class AttributePollInterpreter method execute.

@Nonnull
public Promise<?> execute(ProviderContainer providerContainer, Method method) {
    String interfaceName = providerContainer.getInterfaceName();
    Object returnValueFromProvider = null;
    try {
        returnValueFromProvider = method.invoke(providerContainer.getProviderProxy());
    } catch (IllegalAccessException e) {
        String message = String.format("Method \"%s\" is not accessible on \"%s\" provider (exception: \"%s\").", method.getName(), interfaceName, e.toString());
        logger.error(message, e);
        JoynrVersion joynrVersion = AnnotationUtil.getAnnotation(providerContainer.getProviderProxy().getClass(), JoynrVersion.class);
        throw new MethodInvocationException(message, new Version(joynrVersion.major(), joynrVersion.minor()));
    } catch (IllegalArgumentException e) {
        String message = String.format("Provider of interface \"%s\" does not declare method \"%s\" (exception: \"%s\")", interfaceName, method.getName(), e.toString());
        logger.error(message, e);
        JoynrVersion joynrVersion = AnnotationUtil.getAnnotation(providerContainer.getProviderProxy().getClass(), JoynrVersion.class);
        throw new MethodInvocationException(message, new Version(joynrVersion.major(), joynrVersion.minor()));
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        String message = String.format("Calling method \"%s\" on \"%s\" provider threw an exception: \"%s\"", method.getName(), interfaceName, cause == null ? e.toString() : cause.toString());
        logger.error(message, e);
        throw new ProviderRuntimeException(cause == null ? e.toString() : cause.toString());
    } catch (Exception e) {
        String message = String.format("Calling method \"%s\" on \"%s\" provider threw an unexpected exception: \"%s\"", method.getName(), interfaceName, e.toString());
        logger.error(message, e);
        JoynrVersion joynrVersion = AnnotationUtil.getAnnotation(providerContainer.getProviderProxy().getClass(), JoynrVersion.class);
        throw new MethodInvocationException(message, new Version(joynrVersion.major(), joynrVersion.minor()));
    }
    if (returnValueFromProvider == null) {
        String message = String.format("Calling method \"%s\" on \"%s\" provider returned \"null\".", method.getName(), interfaceName);
        logger.error(message);
        throw new JoynrRuntimeException(message);
    }
    Promise<?> returnedPromiseFromProvider = null;
    try {
        returnedPromiseFromProvider = (Promise<?>) returnValueFromProvider;
    } catch (ClassCastException e) {
        String message = String.format("Calling method \"%s\" on \"%s\" provider did not return a promise.", method.getName(), interfaceName);
        logger.error(message, e);
        throw new JoynrRuntimeException(message, e);
    }
    return returnedPromiseFromProvider;
}
Also used : JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodInvocationException(joynr.exceptions.MethodInvocationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrVersion(io.joynr.JoynrVersion) JoynrVersion(io.joynr.JoynrVersion) Version(joynr.types.Version) MethodInvocationException(joynr.exceptions.MethodInvocationException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) Nonnull(javax.annotation.Nonnull)

Example 2 with MethodInvocationException

use of joynr.exceptions.MethodInvocationException in project joynr by bmwcarit.

the class RequestInterpreter method invokeMethod.

public Object invokeMethod(RequestCaller requestCaller, OneWayRequest request) {
    // A method is identified by its defining request caller, its name and the types of its arguments
    MethodSignature methodSignature = new MethodSignature(requestCaller, request.getMethodName(), request.getParamDatatypes());
    ensureMethodMetaInformationPresent(requestCaller, request, methodSignature);
    Method method = methodSignatureToMethodMap.get(methodSignature);
    Object[] params = null;
    try {
        if (method.getParameterTypes().length > 0) {
            // method with parameters
            params = request.getParams();
        }
        joynrMessageScope.activate();
        setContext(requestCaller, request);
        logger.trace("invoke provider method {}({})", method.getName(), params == null ? "" : params);
        return requestCaller.invoke(method, params);
    } catch (IllegalAccessException e) {
        logger.error("RequestInterpreter: Received an RPC invocation for a non public method {}", request);
        JoynrVersion joynrVersion = AnnotationUtil.getAnnotation(requestCaller.getProxy().getClass(), JoynrVersion.class);
        throw new MethodInvocationException(e, new Version(joynrVersion.major(), joynrVersion.minor()));
    } catch (InvocationTargetException e) {
        logger.debug("invokeMethod error", e);
        Throwable cause = e.getCause();
        logger.error("RequestInterpreter: Could not perform an RPC invocation: {}", cause == null ? e.toString() : cause.getMessage());
        throw new ProviderRuntimeException(cause == null ? e.toString() : cause.toString());
    } finally {
        requestCaller.removeContext();
        joynrMessageScope.deactivate();
    }
}
Also used : MethodSignature(io.joynr.proxy.MethodSignature) JoynrVersion(io.joynr.JoynrVersion) JoynrVersion(io.joynr.JoynrVersion) Version(joynr.types.Version) Method(java.lang.reflect.Method) MethodInvocationException(joynr.exceptions.MethodInvocationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Example 3 with MethodInvocationException

use of joynr.exceptions.MethodInvocationException in project joynr by bmwcarit.

the class RequestInterpreter method execute.

public void execute(final ProviderCallback<Reply> callback, RequestCaller requestCaller, final Request request) {
    Promise<? extends AbstractDeferred> promise;
    logger.debug("execute request on provider: {}", request);
    try {
        promise = (Promise<?>) invokeMethod(requestCaller, request);
    } catch (MethodInvocationException | ProviderRuntimeException e) {
        logger.warn("execute request on provider failed with exception: {}, request: {}", e, request);
        callback.onFailure(e);
        return;
    } catch (Exception e) {
        JoynrVersion joynrVersion = AnnotationUtil.getAnnotation(requestCaller.getProxy().getClass(), JoynrVersion.class);
        MethodInvocationException methodInvocationException = new MethodInvocationException(e, new Version(joynrVersion.major(), joynrVersion.minor()));
        logger.warn("execute request on provider failed with exception: {}, request: {}", methodInvocationException, request);
        callback.onFailure(methodInvocationException);
        return;
    }
    promise.then(new PromiseListener() {

        @Override
        public void onRejection(JoynrException error) {
            logger.debug("execute request on provider onRejection: {}, request: {}", error, request);
            callback.onFailure(error);
        }

        @Override
        public void onFulfillment(Object... values) {
            logger.debug("execute request on provider onFulfillment: {}, request: {}", values, request);
            callback.onSuccess(createReply(request, values));
        }
    });
}
Also used : JoynrVersion(io.joynr.JoynrVersion) JoynrVersion(io.joynr.JoynrVersion) Version(joynr.types.Version) PromiseListener(io.joynr.provider.PromiseListener) JoynrException(io.joynr.exceptions.JoynrException) MethodInvocationException(joynr.exceptions.MethodInvocationException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) MethodInvocationException(joynr.exceptions.MethodInvocationException) JoynrException(io.joynr.exceptions.JoynrException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Example 4 with MethodInvocationException

use of joynr.exceptions.MethodInvocationException in project joynr by bmwcarit.

the class RequestInterpreter method ensureMethodMetaInformationPresent.

private void ensureMethodMetaInformationPresent(RequestCaller requestCaller, OneWayRequest request, MethodSignature methodSignature) {
    try {
        if (!methodSignatureToMethodMap.containsKey(methodSignature)) {
            Method method;
            method = ReflectionUtils.findMethodByParamTypeNames(methodSignature.getRequestCaller().getProxy().getClass(), methodSignature.getMethodName(), methodSignature.getParameterTypeNames());
            methodSignatureToMethodMap.putIfAbsent(methodSignature, method);
        }
    } catch (NoSuchMethodException e) {
        logger.error("RequestInterpreter: Received an RPC invocation for a non existing method" + request, e);
        JoynrVersion joynrVersion = AnnotationUtil.getAnnotation(requestCaller.getProxy().getClass(), JoynrVersion.class);
        throw new MethodInvocationException(e.toString(), new Version(joynrVersion.major(), joynrVersion.minor()));
    }
}
Also used : JoynrVersion(io.joynr.JoynrVersion) JoynrVersion(io.joynr.JoynrVersion) Version(joynr.types.Version) Method(java.lang.reflect.Method) MethodInvocationException(joynr.exceptions.MethodInvocationException)

Example 5 with MethodInvocationException

use of joynr.exceptions.MethodInvocationException in project joynr by bmwcarit.

the class SerializationTest method serializeReplyWithMethodInvocationException.

@Test
public void serializeReplyWithMethodInvocationException() throws IOException {
    MethodInvocationException error = new MethodInvocationException("detail message: MessageInvocationException");
    Reply reply = new Reply(UUID.randomUUID().toString(), error);
    String writeValueAsString = objectMapper.writeValueAsString(reply);
    System.out.println(writeValueAsString);
    Reply receivedReply = objectMapper.readValue(writeValueAsString, Reply.class);
    Assert.assertEquals(reply, receivedReply);
}
Also used : Reply(joynr.Reply) MethodInvocationException(joynr.exceptions.MethodInvocationException) Test(org.junit.Test)

Aggregations

MethodInvocationException (joynr.exceptions.MethodInvocationException)5 JoynrVersion (io.joynr.JoynrVersion)4 Version (joynr.types.Version)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ProviderRuntimeException (joynr.exceptions.ProviderRuntimeException)3 Method (java.lang.reflect.Method)2 JoynrException (io.joynr.exceptions.JoynrException)1 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)1 PromiseListener (io.joynr.provider.PromiseListener)1 MethodSignature (io.joynr.proxy.MethodSignature)1 Nonnull (javax.annotation.Nonnull)1 Reply (joynr.Reply)1 Test (org.junit.Test)1