Search in sources :

Example 1 with IBeanListExecutor

use of org.jowidgets.cap.service.api.executor.IBeanListExecutor in project jo-client-platform by jo-source.

the class ExecutorAnnotationPostProcessor method postProcessAfterInitialization.

@SuppressWarnings("unchecked")
@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) {
    try {
        final ExecutorBean beanAnnotation = beanFactory.findAnnotationOnBean(beanName, ExecutorBean.class);
        if (beanAnnotation != null) {
            final IBeanAccess<? extends IBean> beanAccess = beanAccessProvider.getBeanAccess(beanAnnotation.value());
            final List<String> propertyNames = new BeanTypeUtil(beanAccess.getBeanType()).getPropertyNames();
            final Set<Method> methods = getExecutorMethods(bean);
            for (final Method method : methods) {
                final Object proxy = createExecutorProxy(beanFactory, beanName, method);
                final IExecutorServiceBuilder<IBean, Object> builder = CapServiceToolkit.executorServiceBuilder(beanAccess);
                if (proxy instanceof IBeanExecutor) {
                    builder.setExecutor((IBeanExecutor<IBean, Object>) proxy);
                } else {
                    builder.setExecutor((IBeanListExecutor<IBean, Object>) proxy);
                }
                builder.setBeanDtoFactory(propertyNames);
                final Executor executorAnnotation = method.getAnnotation(Executor.class);
                builder.setAllowDeletedBeans(executorAnnotation.allowDeletedBeans());
                builder.setAllowStaleBeans(executorAnnotation.allowStaleBeans());
                if (executorAnnotation.checker() != DefaultExecutableChecker.class) {
                    try {
                        builder.setExecutableChecker(executorAnnotation.checker().newInstance());
                    } catch (final InstantiationException e) {
                        throw new RuntimeException(e);
                    } catch (final IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
                IExecutorService<Object> executorService = builder.build();
                if (transactionManager != null) {
                    executorService = new TransactionProxyFactory(transactionManager).createProxy(executorService, "execute");
                }
                final IServiceId<IExecutorService<Object>> serviceId = new ServiceId<IExecutorService<Object>>(executorAnnotation.id(), IExecutorService.class);
                if (isLocal()) {
                    final DefaultCapServiceToolkit defaultCapServiceToolkit = new DefaultCapServiceToolkit();
                    final IServicesDecoratorProvider asyncDecoratorProvider = defaultCapServiceToolkit.serviceDecoratorProvider().asyncDecoratorProvider();
                    final IDecorator<IExecutorService<Object>> decorator = asyncDecoratorProvider.getDecorator(serviceId);
                    executorService = decorator.decorate(executorService);
                }
                SpringServiceProvider.getInstance().addService(serviceId, executorService);
            }
        }
    } catch (final NoSuchBeanDefinitionException e) {
    }
    return bean;
}
Also used : Method(java.lang.reflect.Method) IExecutorService(org.jowidgets.cap.common.api.service.IExecutorService) IServiceId(org.jowidgets.service.api.IServiceId) ServiceId(org.jowidgets.service.tools.ServiceId) DefaultCapServiceToolkit(org.jowidgets.cap.service.impl.DefaultCapServiceToolkit) ExecutorBean(org.jowidgets.cap.service.api.annotation.ExecutorBean) IBeanExecutor(org.jowidgets.cap.service.api.executor.IBeanExecutor) IBeanListExecutor(org.jowidgets.cap.service.api.executor.IBeanListExecutor) IBeanExecutor(org.jowidgets.cap.service.api.executor.IBeanExecutor) Executor(org.jowidgets.cap.service.api.annotation.Executor) IServicesDecoratorProvider(org.jowidgets.service.api.IServicesDecoratorProvider) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) IBean(org.jowidgets.cap.common.api.bean.IBean)

Example 2 with IBeanListExecutor

use of org.jowidgets.cap.service.api.executor.IBeanListExecutor 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)

Aggregations

IBean (org.jowidgets.cap.common.api.bean.IBean)2 IBeanExecutor (org.jowidgets.cap.service.api.executor.IBeanExecutor)2 IBeanListExecutor (org.jowidgets.cap.service.api.executor.IBeanListExecutor)2 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 IExecutionCallback (org.jowidgets.cap.common.api.execution.IExecutionCallback)1 IExecutorService (org.jowidgets.cap.common.api.service.IExecutorService)1 Executor (org.jowidgets.cap.service.api.annotation.Executor)1 ExecutorBean (org.jowidgets.cap.service.api.annotation.ExecutorBean)1 DefaultCapServiceToolkit (org.jowidgets.cap.service.impl.DefaultCapServiceToolkit)1 IServiceId (org.jowidgets.service.api.IServiceId)1 IServicesDecoratorProvider (org.jowidgets.service.api.IServicesDecoratorProvider)1 ServiceId (org.jowidgets.service.tools.ServiceId)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1