Search in sources :

Example 1 with AbstractDeferred

use of io.joynr.provider.AbstractDeferred in project joynr by bmwcarit.

the class ProviderWrapper method invoke.

/**
 * When a method is invoked via a joynr call, then it is delegated to an instance of the bean with which this
 * instance was initialised, if the method is part of the business interface and to this instance if it was part of
 * the {@link JoynrProvider} interface or the <code>Object</code> class.
 *
 * @param proxy
 *            the proxy object on which the method was called.
 * @param method
 *            the specific method which was called.
 * @param args
 *            the arguments with which the method was called.
 *
 * @return the result of the delegate method call on the EJB, but wrapped in a promise, as all the provider methods
 *         in joynr are declared that way.
 */
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    boolean isProviderMethod = matchesJoynrProviderMethod(method);
    Method delegateToMethod = getMethodFromInterfaces(bean.getBeanClass(), method, isProviderMethod);
    Object delegate = createDelegateForMethod(method, isProviderMethod);
    Object result = null;
    try {
        if (isProviderMethod(method, delegateToMethod)) {
            JoynrJeeMessageContext.getInstance().activate();
            copyMessageCreatorInfo();
            copyMessageContext();
        }
        JoynrException joynrException = null;
        try {
            result = delegateToMethod.invoke(delegate, args);
        } catch (InvocationTargetException e) {
            joynrException = getJoynrExceptionFromInvocationException(e);
        }
        if (delegate != this) {
            AbstractDeferred deferred = createAndResolveOrRejectDeferred(method, result, joynrException);
            Promise<AbstractDeferred> promiseResult = new Promise<>(deferred);
            return promiseResult;
        }
    } finally {
        if (isProviderMethod(method, delegateToMethod)) {
            JoynrJeeMessageContext.getInstance().deactivate();
        }
    }
    return result;
}
Also used : Promise(io.joynr.provider.Promise) JoynrException(io.joynr.exceptions.JoynrException) Method(java.lang.reflect.Method) AbstractDeferred(io.joynr.provider.AbstractDeferred) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with AbstractDeferred

use of io.joynr.provider.AbstractDeferred in project joynr by bmwcarit.

the class ProviderWrapper method createAndResolveOrRejectDeferred.

@SuppressWarnings("unchecked")
private AbstractDeferred createAndResolveOrRejectDeferred(Method method, Object result, JoynrException joynrException) {
    AbstractDeferred deferred;
    if (result == null && method.getReturnType().equals(Void.class)) {
        deferred = new DeferredVoid();
        if (joynrException == null) {
            ((DeferredVoid) deferred).resolve();
        }
    } else {
        if (result instanceof MultiReturnValuesContainer) {
            deferred = new MultiValueDeferred();
            if (joynrException == null) {
                ((MultiValueDeferred) deferred).resolve(((MultiReturnValuesContainer) result).getValues());
            }
        } else {
            deferred = new Deferred<Object>();
            if (joynrException == null) {
                ((Deferred<Object>) deferred).resolve(result);
            }
        }
    }
    if (joynrException != null) {
        LOG.debug("Provider method invocation resulted in provider runtime exception - rejecting the deferred {} with {}", deferred, joynrException);
        if (joynrException instanceof ApplicationException) {
            try {
                Method rejectMethod = AbstractDeferred.class.getDeclaredMethod("reject", new Class[] { JoynrException.class });
                rejectMethod.setAccessible(true);
                rejectMethod.invoke(deferred, new Object[] { joynrException });
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                LOG.warn("Unable to set {} as rejection reason on {}. Wrapping in ProviderRuntimeException instead.", joynrException, deferred);
                deferred.reject(new ProviderRuntimeException(((ApplicationException) joynrException).getMessage()));
            }
        } else if (joynrException instanceof ProviderRuntimeException) {
            deferred.reject((ProviderRuntimeException) joynrException);
        }
    }
    return deferred;
}
Also used : AbstractDeferred(io.joynr.provider.AbstractDeferred) MultiValueDeferred(io.joynr.provider.MultiValueDeferred) Deferred(io.joynr.provider.Deferred) Method(java.lang.reflect.Method) AbstractDeferred(io.joynr.provider.AbstractDeferred) DeferredVoid(io.joynr.provider.DeferredVoid) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultiReturnValuesContainer(io.joynr.dispatcher.rpc.MultiReturnValuesContainer) ApplicationException(joynr.exceptions.ApplicationException) DeferredVoid(io.joynr.provider.DeferredVoid) MultiValueDeferred(io.joynr.provider.MultiValueDeferred) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Aggregations

AbstractDeferred (io.joynr.provider.AbstractDeferred)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 MultiReturnValuesContainer (io.joynr.dispatcher.rpc.MultiReturnValuesContainer)1 JoynrException (io.joynr.exceptions.JoynrException)1 Deferred (io.joynr.provider.Deferred)1 DeferredVoid (io.joynr.provider.DeferredVoid)1 MultiValueDeferred (io.joynr.provider.MultiValueDeferred)1 Promise (io.joynr.provider.Promise)1 ApplicationException (joynr.exceptions.ApplicationException)1 ProviderRuntimeException (joynr.exceptions.ProviderRuntimeException)1