package org.gcube.common.scope.impl; import java.util.concurrent.Callable; import org.gcube.common.scope.api.ScopeProvider; /** * Utility to bind the execution of standard tasks to the current scope. * * @author Fabio Simeoni * */ public class ScopedTasks { /** * Binds a {@link Callable} task to the current scope. * @param task the task * @return an equivalent {@link Callable} task bound to the current scope */ static public Callable bind(final Callable task) { final String callScope = ScopeProvider.instance.get(); return new Callable() { @Override public V call() throws Exception { //bind underlying thread to callscope ScopeProvider.instance.set(callScope); try { return task.call(); } finally { ScopeProvider.instance.reset(); } } }; } /** * Binds a {@link Runnable} task to the current scope. * @param task the task * @return an equivalent {@link Runnable} task bound to the current scope */ static public Runnable bind(final Runnable task) { final String callScope = ScopeProvider.instance.get(); return new Runnable() { @Override public void run() { //bind underlying thread to callscope ScopeProvider.instance.set(callScope); try { task.run(); } finally { ScopeProvider.instance.reset(); } } }; } }