package com.huatek.busi.service.impl;

import java.lang.reflect.Method;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.service.TestTaskService;
import com.huatek.busi.service.impl.job.SpringContextUtils;
import com.huatek.torch.frame.exception.OwlException;

@Component
@Service(interfaceClass = TestTaskService.class)
@Transactional(rollbackFor = Exception.class)
public class TestTaskServiceImpl implements TestTaskService {

    @Override
    public void doTest(String beanName, String params) {
        Object bean = SpringContextUtils.getBean(beanName);
        if (bean == null) {
            throw new OwlException(beanName + " not exist!");
        }
        Method method = null;
        if(StringUtils.isEmpty(params)) {
            method = ReflectionUtils.findMethod(bean.getClass(), "execute");
        }else {
            method = ReflectionUtils.findMethod(bean.getClass(), "execute", String.class);
        }
        if (method == null) {
            throw new OwlException(beanName + ".execute method not exist!");
        }
        if (StringUtils.isEmpty(params)) {
            ReflectionUtils.invokeMethod(method, bean);
        } else {
            ReflectionUtils.invokeMethod(method, bean, params);
        }
    }

}
