package com.hammurapi.common.concurrent.transactions;

import java.util.concurrent.Future;

import javax.transaction.Transaction;
import javax.transaction.xa.XAResource;

import com.hammurapi.common.Context;
import com.hammurapi.common.MutableContext;
import com.hammurapi.common.concurrent.Logger;
import com.hammurapi.common.concurrent.PropertySet;

/**
 * Transactional execution context.
 * @author Pavel Vlasov
 *
 * @param <KP>
 */
public interface ExecutionContext<KP> extends Context, ProgressMonitorFactory {
	
	Transaction getTransaction();
	
//	ExecutorService getExecutorService(); Obtain through context lookup.
	
	/**
	 * Resources shall be registered before it can be used - Bitronix idiosyncrasy.
	 * Resources are automatically unregistered upon transaction completion.
	 * @param xaResource
	 */
	void registerXAResource(XAResource xaResource);
		
	/**
	 * Adds a command to be executed on transaction rollback.
	 * Use it to rollback modifications done on non-XA resources.
	 * @param undoCommand
	 */
	void addUndo(Runnable undoCommand);
	
	/**
	 * Executes command either in the same thread on in a background thread but in the context of the same transaction.
	 * @param command
	 * @param properties
	 * @param args
	 * @return
	 */
	<V> Future<V> execute(Command<KP, V> command, Context context, PropertySet<KP> properties, Object... args);
		
	PropertySet<KP> createPropertySet(PropertySet<KP>... chain);
	
	/**
	 * Creates a mutable context chained with this executor. This method is used for passing bindings and services to subtasks.
	 * @return
	 */
	MutableContext createContext();
	
	/**
	 * By invoking this method command indicates that it cannot complete because of pending dependencies and shall be
	 * executed later.
	 */
	void yield();
	
	Logger getLogger();

}
