// Path: src/main/java/com/baidu/unbiz/multitask/exception/TaskBizException.java
// public class TaskBizException extends RuntimeException {
// 
//     private static final long serialVersionUID = -7375423850222016116L;
// 
//     public TaskBizException(String msg) {
//         super(msg);
//     }
// 
//     public TaskBizException(Throwable cause) {
//         super(cause);
//     }
// 
//     public TaskBizException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.baidu.unbiz.multitask.exception.TaskBizException;
package com.baidu.unbiz.multitask.task.thread;




/**
 * 并行抓取上线文环境
 *
 * @author wangchongjie
 * @since 2015-11-21 下午7:58:49
 */
public class TaskContext implements MultiResult {

    private static final Log LOG = LogFactory.getLog(TaskContext.class);

    /**
     * 启用的ThreadLocal
     * 将在task执行时生效
     */
    private static Set<ThreadLocal> threadLocalSet = new HashSet<ThreadLocal>();
    /**
     * 一组结果数据仓库
     */
    private Map<String, Object> result = new ConcurrentHashMap<String, Object>();
    /**
     * 一组结果Exception仓库
     */
    private Map<String, Exception> exception = new ConcurrentHashMap<String, Exception>();
    /**
     * 启用ThreadLocal后，一组任务Task执行前的镜像值
     */
    private Map<ThreadLocal, Object> threadLocalValues = new HashMap<ThreadLocal, Object>();

    /**
     * 执行环境上下文属性
     */
    private Map<String, Object> attribute = new ConcurrentHashMap<String, Object>();

    /**
     * 结果数据存储
     *
     * @param key
     * @param value
     */
    public <E> void putResult(String key, E value) {
        if (value != null) {
            result.put(key, value);
        }
    }

    /**
     * 并行执行时上抛Exception
     *
     * @param key
     * @param value
     */
    public void throwException(String key, Exception value) {
        exception.put(key, value);
    }

    /**
     * 获取并行执行的数据结果
     *
     * @param taskName
     *
     * @return fetcher对应的数据结果
     */
    @SuppressWarnings("unchecked")
    public <E> E getResult(String taskName) {
        Exception ex = exception.get(taskName);
        if (ex != null) {
            LOG.error("Execute fail:", ex);
            if (ex instanceof RuntimeException) {
                throw (RuntimeException) ex;
            } else {
// Path: src/main/java/com/baidu/unbiz/multitask/exception/TaskBizException.java
// public class TaskBizException extends RuntimeException {
// 
//     private static final long serialVersionUID = -7375423850222016116L;
// 
//     public TaskBizException(String msg) {
//         super(msg);
//     }
// 
//     public TaskBizException(Throwable cause) {
//         super(cause);
//     }
// 
//     public TaskBizException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


// Path: src/main/java/com/baidu/unbiz/multitask/task/thread/TaskContext.java
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.baidu.unbiz.multitask.exception.TaskBizException;

package com.baidu.unbiz.multitask.task.thread;




/**
 * 并行抓取上线文环境
 *
 * @author wangchongjie
 * @since 2015-11-21 下午7:58:49
 */
public class TaskContext implements MultiResult {

    private static final Log LOG = LogFactory.getLog(TaskContext.class);

    /**
     * 启用的ThreadLocal
     * 将在task执行时生效
     */
    private static Set<ThreadLocal> threadLocalSet = new HashSet<ThreadLocal>();
    /**
     * 一组结果数据仓库
     */
    private Map<String, Object> result = new ConcurrentHashMap<String, Object>();
    /**
     * 一组结果Exception仓库
     */
    private Map<String, Exception> exception = new ConcurrentHashMap<String, Exception>();
    /**
     * 启用ThreadLocal后，一组任务Task执行前的镜像值
     */
    private Map<ThreadLocal, Object> threadLocalValues = new HashMap<ThreadLocal, Object>();

    /**
     * 执行环境上下文属性
     */
    private Map<String, Object> attribute = new ConcurrentHashMap<String, Object>();

    /**
     * 结果数据存储
     *
     * @param key
     * @param value
     */
    public <E> void putResult(String key, E value) {
        if (value != null) {
            result.put(key, value);
        }
    }

    /**
     * 并行执行时上抛Exception
     *
     * @param key
     * @param value
     */
    public void throwException(String key, Exception value) {
        exception.put(key, value);
    }

    /**
     * 获取并行执行的数据结果
     *
     * @param taskName
     *
     * @return fetcher对应的数据结果
     */
    @SuppressWarnings("unchecked")
    public <E> E getResult(String taskName) {
        Exception ex = exception.get(taskName);
        if (ex != null) {
            LOG.error("Execute fail:", ex);
            if (ex instanceof RuntimeException) {
                throw (RuntimeException) ex;
            } else {
                throw new TaskBizException(ex);