Search in sources :

Example 1 with IExecutionCallback

use of org.jowidgets.cap.common.api.execution.IExecutionCallback in project jo-client-platform by jo-source.

the class ProgressResponseCallback method setSubProgress.

private void setSubProgress(final Progress progress, final IExecutionCallback executionCallback) {
    for (final Progress subProgress : progress.getSubProgressList()) {
        // set the subProgress recursively
        setSubProgress(subProgress, executionCallback);
        // check if the task id is already registered
        Tuple<ValueHolder<Progress>, IExecutionCallback> subCallback = subCallbacks.get(subProgress.getTaskId());
        // if not, create a sub execution
        if (subCallback == null) {
            final ValueHolder<Progress> newProgress = new ValueHolder<Progress>(new Progress());
            subCallback = new Tuple<ValueHolder<Progress>, IExecutionCallback>(newProgress, executionCallback.createSubExecution(subProgress.getStepProportion()));
            subCallbacks.put(subProgress.getTaskId(), subCallback);
        }
        // set the progress on the execution callback and set the last progress to the current progress
        setProgress(subProgress, subCallback.getFirst().get(), subCallback.getSecond());
        subCallback.getFirst().set(subProgress);
    }
}
Also used : Progress(org.jowidgets.cap.remoting.common.Progress) IExecutionCallback(org.jowidgets.cap.common.api.execution.IExecutionCallback) ValueHolder(org.jowidgets.util.ValueHolder)

Example 2 with IExecutionCallback

use of org.jowidgets.cap.common.api.execution.IExecutionCallback in project jo-client-platform by jo-source.

the class ExecutorAnnotationPostProcessor method createExecutorProxy.

private Object createExecutorProxy(final BeanFactory beanFactory, final String beanName, final Method method) {
    final boolean voidMethod = method.getReturnType() == void.class;
    Boolean singleExecutor = null;
    Integer dataArgPosition = null;
    Integer callbackArgPosition = null;
    final List<Integer> paramArgPositions = new LinkedList<Integer>();
    for (int i = 0; i < method.getParameterTypes().length; i++) {
        final Class<?> parameterType = method.getParameterTypes()[i];
        if (IBean.class.isAssignableFrom(parameterType) && singleExecutor == null) {
            singleExecutor = true;
            dataArgPosition = i;
            continue;
        }
        if (List.class.isAssignableFrom(parameterType) && singleExecutor == null) {
            final Type genericParameterType = method.getGenericParameterTypes()[i];
            if (genericParameterType instanceof ParameterizedType) {
                final ParameterizedType parameterizedType = (ParameterizedType) genericParameterType;
                if (IBean.class.isAssignableFrom((Class<?>) parameterizedType.getActualTypeArguments()[0])) {
                    singleExecutor = false;
                    dataArgPosition = i;
                    continue;
                }
            }
        }
        if (parameterType == IExecutionCallback.class) {
            callbackArgPosition = i;
            continue;
        }
        paramArgPositions.add(i);
    }
    final Object[] args = new Object[method.getParameterTypes().length];
    final Integer finalDataArgPosition = dataArgPosition;
    final Integer finalCallbackArgPosition = callbackArgPosition;
    if (singleExecutor != null && singleExecutor) {
        return new IBeanExecutor<IBean, Object>() {

            @Override
            public IBean execute(final IBean data, final Object parameter, final IExecutionCallback executionCallback) {
                if (finalDataArgPosition != null) {
                    args[finalDataArgPosition] = data;
                }
                if (finalCallbackArgPosition != null) {
                    args[finalCallbackArgPosition] = executionCallback;
                }
                if (paramArgPositions.size() == 1) {
                    args[paramArgPositions.get(0)] = getSingleParameterValue(parameter, method, paramArgPositions.get(0));
                } else if (!paramArgPositions.isEmpty()) {
                    for (int i = 0; i < paramArgPositions.size(); i++) {
                        args[paramArgPositions.get(i)] = getMultiParameterValue(parameter, method, paramArgPositions.get(i), i);
                    }
                }
                final Object result = ReflectionUtils.invokeMethod(method, beanFactory.getBean(beanName), args);
                if (voidMethod) {
                    return data;
                }
                return (IBean) result;
            }
        };
    } else {
        return new IBeanListExecutor<IBean, Object>() {

            @SuppressWarnings("unchecked")
            @Override
            public List<IBean> execute(List<IBean> data, final Object parameter, final IExecutionCallback executionCallback) {
                if (voidMethod) {
                    data = new ArrayList<IBean>(data);
                }
                if (finalDataArgPosition != null) {
                    args[finalDataArgPosition] = data;
                }
                if (finalCallbackArgPosition != null) {
                    args[finalCallbackArgPosition] = executionCallback;
                }
                if (paramArgPositions.size() == 1) {
                    args[paramArgPositions.get(0)] = getSingleParameterValue(parameter, method, paramArgPositions.get(0));
                } else if (!paramArgPositions.isEmpty()) {
                    for (int i = 0; i < paramArgPositions.size(); i++) {
                        args[paramArgPositions.get(i)] = getMultiParameterValue(parameter, method, paramArgPositions.get(i), i);
                    }
                }
                final Object result = ReflectionUtils.invokeMethod(method, beanFactory.getBean(beanName), args);
                if (voidMethod) {
                    return data;
                }
                if (result instanceof IBean) {
                    return Collections.singletonList((IBean) result);
                }
                return (List<IBean>) result;
            }
        };
    }
}
Also used : IExecutionCallback(org.jowidgets.cap.common.api.execution.IExecutionCallback) LinkedList(java.util.LinkedList) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) IBeanExecutor(org.jowidgets.cap.service.api.executor.IBeanExecutor) IBeanListExecutor(org.jowidgets.cap.service.api.executor.IBeanListExecutor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) IBean(org.jowidgets.cap.common.api.bean.IBean)

Example 3 with IExecutionCallback

use of org.jowidgets.cap.common.api.execution.IExecutionCallback in project jo-client-platform by jo-source.

the class AbstractCapServiceInvocationHandler method invoke.

@Override
public final Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    final Class<?>[] parameterTypes = method.getParameterTypes();
    final IExecutionCallback executionCallback = getExecutionCallback(parameterTypes, args);
    final int resultCallbackIndex = getFirstMatchingIndex(IResultCallback.class, parameterTypes);
    if (resultCallbackIndex == -1) {
        if (executionCallback != null && executionCallback.isCanceled()) {
            throw new ServiceCanceledException();
        }
        return invokeSyncSignature(method, args, executionCallback);
    } else {
        @SuppressWarnings("unchecked") final IResultCallback<Object> resultCallback = (IResultCallback<Object>) args[resultCallbackIndex];
        if (executionCallback != null && executionCallback.isCanceled()) {
            resultCallback.exception(new ServiceCanceledException());
            return null;
        }
        return invokeAsyncSignature(method, args, resultCallbackIndex, resultCallback, executionCallback);
    }
}
Also used : IExecutionCallback(org.jowidgets.cap.common.api.execution.IExecutionCallback) ServiceCanceledException(org.jowidgets.cap.common.api.exception.ServiceCanceledException) IResultCallback(org.jowidgets.cap.common.api.execution.IResultCallback)

Example 4 with IExecutionCallback

use of org.jowidgets.cap.common.api.execution.IExecutionCallback in project jo-client-platform by jo-source.

the class RemoteMethodInvocationHandler method getFilteredArgs.

/**
 * Filter the callback arguments
 *
 * @param args The args to filter
 * @param parameterTypes
 * @return the filtered args
 */
@SuppressWarnings("unchecked")
private Object[] getFilteredArgs(final Object[] args, final Class<?>[] parameterTypes) {
    if (args != null) {
        final Object[] result = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
            final Object object = args[i];
            if (object instanceof IResultCallback<?>) {
                result[i] = null;
            } else if (object instanceof IExecutionCallback) {
                result[i] = null;
            } else if (object instanceof InputStream) {
                result[i] = new InputStreamDummy();
            } else if (InputStream[].class.isAssignableFrom(parameterTypes[i])) {
                final InputStream[] inputStreams = (InputStream[]) object;
                if (inputStreams != null) {
                    final InputStreamDummy[] inputStreamDummies = new InputStreamDummy[inputStreams.length];
                    for (int j = 0; j < inputStreams.length; j++) {
                        if (inputStreams[j] != null) {
                            inputStreamDummies[j] = new InputStreamDummy();
                        } else {
                            inputStreamDummies[j] = null;
                        }
                    }
                    result[i] = inputStreamDummies;
                } else {
                    result[i] = null;
                }
            } else if (Iterable.class.isAssignableFrom(parameterTypes[i])) {
                final Iterable<?> iterable = (Iterable<?>) object;
                if (iterable != null && isIterableOfType(iterable, InputStream.class)) {
                    if (!Collection.class.isAssignableFrom(parameterTypes[i])) {
                        throw new IllegalArgumentException("Iterables that hold InputStreams must be Collections");
                    }
                    final Collection<InputStreamDummy> inputStreamDummies;
                    try {
                        inputStreamDummies = (Collection<InputStreamDummy>) iterable.getClass().newInstance();
                    } catch (final Exception e) {
                        throw new IllegalArgumentException("Collections that holds input streams must have a public default constructor", e);
                    }
                    for (final Object element : iterable) {
                        if (element instanceof InputStream) {
                            inputStreamDummies.add(new InputStreamDummy());
                        } else if (element != null) {
                            throw new IllegalArgumentException("Collections with mixed types must not contain InputStreams");
                        } else {
                            inputStreamDummies.add(null);
                        }
                    }
                    result[i] = inputStreamDummies;
                } else {
                    result[i] = object;
                }
            } else {
                result[i] = object;
            }
        }
        return result;
    } else {
        return new Object[0];
    }
}
Also used : InputStream(java.io.InputStream) InputStreamDummy(org.jowidgets.cap.remoting.common.InputStreamDummy) IExecutionCallback(org.jowidgets.cap.common.api.execution.IExecutionCallback) Collection(java.util.Collection) IResultCallback(org.jowidgets.cap.common.api.execution.IResultCallback)

Example 5 with IExecutionCallback

use of org.jowidgets.cap.common.api.execution.IExecutionCallback in project jo-client-platform by jo-source.

the class RemoteMethodInvocationHandler method invokeRemoteMethod.

private Object invokeRemoteMethod(final Object proxy, final Method method, final Object[] args) {
    final Class<?>[] parameterTypes = method.getParameterTypes();
    final IResultCallback<Object> resultCallback = getResultCallback(parameterTypes, args);
    final IExecutionCallback executionCallback = getExecutionCallback(parameterTypes, args);
    final ArrayList<InputStream> inputStreams = getInputStreams(parameterTypes, args);
    final IInterimResponseCallback<Progress> interimResponseCallback;
    final IInterimRequestCallback<UserQuestionRequest, UserQuestionResult> userQuestionRequestCallback;
    if (executionCallback != null) {
        interimResponseCallback = new ProgressResponseCallback(executionCallback);
        userQuestionRequestCallback = new UserQuestionRequestCallback(executionCallback);
    } else {
        interimResponseCallback = new DummyProgressResponseCallback();
        userQuestionRequestCallback = new DummyUserQuestionRequestCallback(resultCallback);
    }
    final InputStreamRequestCallback inputStreamRequestCallback;
    if (!EmptyCheck.isEmpty(inputStreams)) {
        inputStreamRequestCallback = new InputStreamRequestCallback(inputStreams);
    } else {
        inputStreamRequestCallback = null;
    }
    final GenericInterimRequestCallback interimRequestCallback = new GenericInterimRequestCallback(userQuestionRequestCallback, inputStreamRequestCallback);
    final RemoteInvocationParameter parameter = new RemoteInvocationParameter(serviceId, method.getName(), parameterTypes, getFilteredArgs(args, parameterTypes));
    if (resultCallback != null) {
        invokeAsync(resultCallback, interimResponseCallback, interimRequestCallback, parameter, executionCallback, inputStreams);
        return null;
    } else {
        return invokeSync(interimResponseCallback, interimRequestCallback, parameter, executionCallback, inputStreams);
    }
}
Also used : Progress(org.jowidgets.cap.remoting.common.Progress) RemoteInvocationParameter(org.jowidgets.cap.remoting.common.RemoteInvocationParameter) InputStream(java.io.InputStream) IExecutionCallback(org.jowidgets.cap.common.api.execution.IExecutionCallback) UserQuestionRequest(org.jowidgets.cap.remoting.common.UserQuestionRequest) UserQuestionResult(org.jowidgets.cap.common.api.execution.UserQuestionResult)

Aggregations

IExecutionCallback (org.jowidgets.cap.common.api.execution.IExecutionCallback)5 InputStream (java.io.InputStream)2 IResultCallback (org.jowidgets.cap.common.api.execution.IResultCallback)2 Progress (org.jowidgets.cap.remoting.common.Progress)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 IBean (org.jowidgets.cap.common.api.bean.IBean)1 ServiceCanceledException (org.jowidgets.cap.common.api.exception.ServiceCanceledException)1 UserQuestionResult (org.jowidgets.cap.common.api.execution.UserQuestionResult)1 InputStreamDummy (org.jowidgets.cap.remoting.common.InputStreamDummy)1 RemoteInvocationParameter (org.jowidgets.cap.remoting.common.RemoteInvocationParameter)1 UserQuestionRequest (org.jowidgets.cap.remoting.common.UserQuestionRequest)1 IBeanExecutor (org.jowidgets.cap.service.api.executor.IBeanExecutor)1 IBeanListExecutor (org.jowidgets.cap.service.api.executor.IBeanListExecutor)1 ValueHolder (org.jowidgets.util.ValueHolder)1