package org.jbpm.wire.operation;
import java.util.logging.Logger;
import org.jbpm.tx.Resource;
import org.jbpm.tx.StandardTransaction;
import org.jbpm.tx.Transaction;
import org.jbpm.wire.WireContext;
import org.jbpm.wire.WireException;
/**
* enlists this {@link Resource} with the current {@link Transaction}.
*
* <p>This {@link Operation} specifies that the object on which this operation is applied
* should be added as a {@link Resource} to the specified {@link Transaction}.
* </p>
*
* <p>property transactionName refers to the objectName of the {@link Transaction}
* and it may not be null.
* </p>
*
* @author Tom Baeyens
* @author Guillaume Porcher (documentation)
*/
public class EnlistOperation implements Operation {
private static final long serialVersionUID = 1L;
private static Logger log = Logger.getLogger(EnlistOperation.class.getName());
String transactionName = null;
/**
* @throws WireException if this operation is applied on an object which is not a resource
* or if the specified transaction cannot be found.
*/
public void apply(Object target, WireContext wireContext) {
if (! (target instanceof Resource)) {
throw new WireException("operation enlist can only be applied on objects that implement "+Resource.class.getName()+": "+target+(target!=null ? " ("+target.getClass().getName()+")" : ""));
}
Object object = null;
if (transactionName!=null) {
object = wireContext.get(transactionName);
} else {
object = wireContext.getEnvironment().getTransaction();
}
if ( (object==null)
|| (! (object instanceof StandardTransaction))
) {
throw new WireException("couldn't find "+StandardTransaction.class.getName()+" "+(transactionName!=null ? "'"+transactionName+"'" : "by type")+" to enlist resource "+target);
}
StandardTransaction standardTransaction = (StandardTransaction) object;
log.finest("enlisting resource "+target+" with transaction");
standardTransaction.enlistResource((Resource)target);
}
/**
* Gets the name of the transaction to which the object should be added.
*/
public String getTransactionName() {
return transactionName;
}
/**
* Sets the name of the transaction to which the object should be added.
* @param transactionName
*/
public void setTransactionName(String transactionName) {
this.transactionName = transactionName;
}
}