package org.jfox.ejb.handler;

import java.util.Iterator;

import org.jfox.ejb.Bucket;
import org.jfox.ejb.Invocation;

/**
 * set the ThreadContextClassLoader to Bean's load ClassLoader
 * when finally, restore the the previous ThreadContextClassLoader
 * * because Bean can refrence the class loaded only use it's J2EEClassLoader
 * <p/>
 * and set the Thread's current ejb name for Enterprise Context
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class A0_ThreadContextHandler extends AbstractHandler {

    public Object invokeHome(Bucket bucket, Invocation invocation, Iterator iter) throws Exception {
        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(bucket.getEJBClassLoader());

        try {
            return super.invokeHome(bucket, invocation, iter);
        }
        finally {
            Thread.currentThread().setContextClassLoader(oldLoader);
        }
    }

    public Object invokeBean(Bucket bucket, Invocation invocation, Iterator iter) throws Exception {
        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(bucket.getEJBClassLoader());
        try {
            return super.invokeBean(bucket, invocation, iter);
        }
        finally {
            Thread.currentThread().setContextClassLoader(oldLoader);
        }

    }

    public static void main(String[] args) {

    }
}

