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);
}
}
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;
}
};
}
}
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);
}
}
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];
}
}
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);
}
}
Aggregations