Package com.softwaremill.common.task

Source Code of com.softwaremill.common.task.ExecuteWithRequestContext

package com.softwaremill.common.task;

import org.jboss.weld.context.bound.BoundRequestContext;
import com.softwaremill.common.util.dependency.D;

import java.util.HashMap;
import java.util.Map;

/**
* @author Adam Warski (adam at warski dot org)
*/
public class ExecuteWithRequestContext {
    private final Task task;

    public ExecuteWithRequestContext(Task task) {
        this.task = task;
    }

    public void execute() {
        doExecute(task);
    }

    private <T extends Task<T>> void doExecute(T task) {
        BoundRequestContext requestContext = D.inject(BoundRequestContext.class);

        Map<String, Object> context = new HashMap<String, Object>();
        try {
            requestContext.associate(context);
            requestContext.activate();

            D.inject(task.getExecutorBeanClass()).execute(task);
        } finally {
            requestContext.invalidate();
            requestContext.deactivate();
            requestContext.dissociate(context);
        }
    }
}
TOP

Related Classes of com.softwaremill.common.task.ExecuteWithRequestContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.