/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.ejb;

import javax.ejb.EJBException;
import javax.ejb.SessionSynchronization;
import javax.transaction.Status;
import javax.transaction.Synchronization;
import javax.transaction.Transaction;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EJBSynchronization implements Synchronization {
    private Transaction tx = null;
    private SessionSynchronization sessionSync = null;

    public EJBSynchronization(SessionSynchronization sessionSync, Transaction tx) {
        this.sessionSync = sessionSync;
        this.tx = tx;
    }

    public void afterBegin() {
        try {
            sessionSync.afterBegin();
        }
        catch(Exception e) {
            try {
                tx.setRollbackOnly();
            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
            throw new EJBException("Unexpected error in afterBegin", e);
        }
    }

    public void afterCompletion(int status) {
        try {
            if(status == Status.STATUS_COMMITTED) {
                sessionSync.afterCompletion(true);
            }
            else {
                sessionSync.afterCompletion(false);
            }
        }
        catch(Exception e) {
            throw new EJBException("Unexpected error in afterCompletion", e);
        }
    }

    public void beforeCompletion() {
        try {
            sessionSync.beforeCompletion();
        }
        catch(Exception e) {
            try {
                tx.setRollbackOnly();
            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
            throw new EJBException("Unexpected error in beforeCompletion", e);
        }
    }
}
