FileDocCategorySizeDatePackage
InvocationManagerImpl.javaAPI DocGlassfish v2 API10270Mon May 28 21:30:18 BST 2007com.sun.enterprise.util

InvocationManagerImpl

public class InvocationManagerImpl extends Object implements com.sun.enterprise.InvocationManager
Implementation of InvocationManager. Use ThreadLocal variable to keep track of per thread data
author
Tony Ng
author
Harpreet Singh

Fields Summary
static Logger
_logger
public static boolean
debug
private static LocalStringManagerImpl
localStrings
private InheritableThreadLocal
frames
private com.sun.enterprise.J2EETransactionManager
tm
Constructors Summary
public InvocationManagerImpl()


      
        frames = new InheritableThreadLocal() {
            protected Object initialValue() {
                return new InvocationArray();
            }

            // if this is a thread created by user in servlet's service method
            // create a new ComponentInvocation with transaction
            // set to null and instance set to null
            // so that the resource won't be enlisted or registered
            protected Object childValue(Object parentValue) {
                // always creates a new ArrayList
                InvocationArray result = new InvocationArray();
                InvocationArray v = (InvocationArray) parentValue;
                if (v.size() > 0 && v.outsideStartup()) {
                    // get current invocation
                    ComponentInvocation parentInv = 
                        (ComponentInvocation) v.get(v.size()-1);
                    if (parentInv.getInvocationType() == 
                        parentInv.SERVLET_INVOCATION) {

                        ComponentInvocation inv = 
                            new ComponentInvocation(null,
                                            parentInv.getContainerContext());
                        result.add(inv);
                    } else if (parentInv.getInvocationType() != parentInv.EJB_INVOCATION) {
			// Push a copy of invocation onto the new result ArrayList
			ComponentInvocation cpy = 
			    new ComponentInvocation 
			    ( parentInv.getInstance(), 
			      parentInv.getContainerContext());
			cpy.setTransaction (parentInv.getTransaction());
			result.add(cpy);
		    }
		    
                }
                return result;
            }
        };
    
Methods Summary
public java.util.ListgetAllInvocations()

        return (ArrayList) frames.get();
    
public com.sun.enterprise.ComponentInvocationgetCurrentInvocation()
return the Invocation object of the component being called

    // END IASRI# 4646060

        ArrayList v = (ArrayList) frames.get();
	int size = v.size();
        // BEGIN IASRI# 4646060
        if (size == 0) return null;
        // END IASRI# 4646060
        return (ComponentInvocation) v.get(size-1);
    
public com.sun.enterprise.ComponentInvocationgetPreviousInvocation()
return the Inovcation object of the caller return null if none exist (e.g. caller is from another VM)


        ArrayList v = (ArrayList) frames.get();
        int i = v.size();
        if (i < 2) return null;
        return (ComponentInvocation) v.get(i - 2);
    
public booleanisInvocationStackEmpty()
return true iff no invocations on the stack for this thread

        ArrayList v = (ArrayList) frames.get();
        return (v.size() == 0);
    
public booleanisStartupInvocation()

        if (frames != null) {
            InvocationArray v = (InvocationArray) frames.get();
            if (v != null) {
                  return v.outsideStartup() == false;
            }
        }

        return false;
    
public voidpostInvoke(com.sun.enterprise.ComponentInvocation inv)

        // START OF IASRI 4660742
        if (debug && _logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE,"IM: postInvoke" + inv.instance);
        }
        // END OF IASRI 4660742

        int invType = inv.getInvocationType();

	// Get this thread's ArrayList
	InvocationArray v = (InvocationArray) frames.get();
        if (invType == ComponentInvocation.SERVICE_STARTUP) {
            v.setInvocationAttribute(ComponentInvocation.UN_INITIALIZED);
            return;
        }

	int size = v.size();
	if (size == 0) 
	    throw new InvocationException();

        try {
	    // if ejb call EJBSecurityManager, for servlet call RealmAdapter
	    if (invType == inv.EJB_INVOCATION){
		SecurityManager sm = 
		    ((Container)inv.getContainerContext()).getSecurityManager();
		
		sm.postInvoke(inv);
	    } else if (invType == inv.SERVLET_INVOCATION){
		Realm rlm = ((Context)inv.getContainerContext()).getRealm();
		if (rlm instanceof RealmAdapter) {
		    RealmAdapter rad = (RealmAdapter) rlm;
		    rad.postSetRunAsIdentity (inv);
		}// else {
// 		    throw new InvocationException();
// 		}
	    }

	    // Get current and previous ComponentInvocation objects
	    ComponentInvocation prev, curr;
	    if (size < 2) 
		prev = null;
	    else 
		prev = (ComponentInvocation)v.get(size - 2);
	    curr = (ComponentInvocation)v.get(size - 1);

	    tm.postInvoke(curr, prev);

            Switch.getSwitch().getPoolManager().postInvoke();
	} 
	finally {
	    // pop the stack
	    v.remove(size - 1);
	}
    
public voidpreInvoke(com.sun.enterprise.ComponentInvocation inv)


        // START OF IASRI 4660742
        if (debug && _logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE,"IM: preInvoke" + inv.instance);
        }
        // END OF IASRI 4660742

	int invType = inv.getInvocationType();

	// Get this thread's ArrayList
        InvocationArray v = (InvocationArray) frames.get();
        if (invType == ComponentInvocation.SERVICE_STARTUP) {
            v.setInvocationAttribute(ComponentInvocation.SERVICE_STARTUP);
            return;
        }

	// if ejb call EJBSecurityManager, for servlet call RealmAdapter
	if (invType  == inv.EJB_INVOCATION) {
	    SecurityManager sm =
		((Container)inv.getContainerContext()).getSecurityManager();
	    sm.preInvoke(inv);
	} else if (invType == inv.SERVLET_INVOCATION){
	    Realm rlm = ((Context)inv.getContainerContext()).getRealm();
	    if (rlm instanceof RealmAdapter) {
		RealmAdapter rad = (RealmAdapter) rlm;
		rad.preSetRunAsIdentity(inv);
	    }
	}

	// push this invocation on the stack
        v.add(inv);

	// Get the previous invocation on the stack
        int size = v.size();
	ComponentInvocation prev;
        if (size < 2) 
	    prev = null;
	else 
	    prev = (ComponentInvocation) v.get(size - 2);

	// Call the TM
        if (tm == null)
            tm = Switch.getSwitch().getTransactionManager();
        tm.preInvoke(prev);