FileDocCategorySizeDatePackage
J2EEInstanceListener.javaAPI DocGlassfish v2 API14621Fri May 04 22:36:20 BST 2007com.sun.web.server

J2EEInstanceListener

public final class J2EEInstanceListener extends Object implements org.apache.catalina.InstanceListener
This class implements the Tomcat InstanceListener interface and handles the INIT,DESTROY and SERVICE, FILTER events.
author
Vivek Nagar
author
Tony Ng

Fields Summary
static Logger
_logger
private static final HashSet
beforeEvents
private static final HashSet
afterEvents
private InvocationManager
im
private J2EETransactionManager
tm
private InjectionManager
injectionMgr
private static AuthPermission
doAsPrivilegedPerm
Constructors Summary
public J2EEInstanceListener()

        im = Switch.getSwitch().getInvocationManager();
        tm = Switch.getSwitch().getTransactionManager();
        injectionMgr = Switch.getSwitch().getInjectionManager();
	//	lc = new LoginContext();
    
Methods Summary
private static voidcheckObjectForDoAsPermission(java.lang.Object o)


 
         
             

	if (System.getSecurityManager() != null) {
	    AccessController.doPrivileged(new PrivilegedAction() {
		public Object run() {
		    ProtectionDomain pD = o.getClass().getProtectionDomain();
		    Policy p = Policy.getPolicy();
		    if (!p.implies(pD,doAsPrivilegedPerm)) {
			throw new AccessControlException
			    ("permission required to override getUserPrincipal",
			     doAsPrivilegedPerm);
		    }
		    return null;
		}
	    });
	}
    
private static com.sun.enterprise.security.SecurityContextgetSecurityContextForPrincipal(java.security.Principal p)

	if (p == null) {
	    return null;
	} else if (p instanceof WebPrincipal) {
	    return ((WebPrincipal) p).getSecurityContext();
	} else {
	    return (SecurityContext) 
		AccessController.doPrivileged(new PrivilegedAction() {
		    public Object run() {
			Subject s = new Subject();
			s.getPrincipals().add(p);
			return new SecurityContext(p.getName(),s);
		    }
		});
	}
    
private voidhandleAfterEvent(org.apache.catalina.InstanceEvent event, java.lang.String eventType)


        if (AppVerification.doInstrument() 
                && (eventType.equals(InstanceEvent.AFTER_SERVICE_EVENT)
                        || eventType.equals(InstanceEvent.AFTER_INIT_EVENT)
                        || eventType.equals(InstanceEvent.AFTER_DISPATCH_EVENT))) {
            
            AppVerification.getInstrumentLogger().doInstrumentForWeb(event);
        }
        
        Object instance = null;
        if (eventType.equals(InstanceEvent.AFTER_FILTER_EVENT)) {
            instance = event.getFilter();
        } else {
            instance = event.getServlet();
        }
        Context context = (Context) event.getWrapper().getParent();
        ComponentInvocation inv = new ComponentInvocation(instance, context);
        try {
            im.postInvoke(inv);
        } catch (Exception ex) {
            throw new RuntimeException(
                _logger.getResourceBundle().getString(
                    "web_server.excep_handle_after_event"),
                ex);
        } finally {
            if (eventType.equals(InstanceEvent.AFTER_DESTROY_EVENT)) {
                tm.componentDestroyed(instance);                
                JndiNameEnvironment desc = (JndiNameEnvironment) 
                    Switch.getSwitch().getDescriptorFor(context);
                if (desc != null
                        && instance.getClass() != DefaultServlet.class
                        && instance.getClass() != JspServlet.class) {
                    try {
                        injectionMgr.invokeInstancePreDestroy(instance, desc);
                    } catch (InjectionException ie) {
                        _logger.log(Level.SEVERE,
                                    "web_server.excep_handle_after_event",
                                    ie);
                    }
                }
            }
            if (eventType.equals(InstanceEvent.AFTER_FILTER_EVENT) ||
                eventType.equals(InstanceEvent.AFTER_SERVICE_EVENT)) {
                // check it's top level invocation
                // BEGIN IASRI# 4646060
                if (im.getCurrentInvocation() == null) {
                // END IASRI# 4646060
                    try {
                        // clear security context
                        Realm ra = context.getRealm();
                        if (ra != null && (ra instanceof RealmAdapter)) {
                            ((RealmAdapter)ra).logout();
                        }
                    } catch (Exception ex) {
                        /** IASRI 4660742
                        ex.printStackTrace();
                        **/
                        // START OF IASRI 4660742
                            _logger.log(Level.SEVERE,
                                        "web_server.excep_handle_after_event",
                                        ex);
                        // END OF IASRI 4660742
                    }
                    try {
                        if (tm.getTransaction() != null) {
                            tm.rollback();
                        }
                        tm.cleanTxnTimeout();
                    } catch (Exception ex) {}
                }
                tm.componentDestroyed(instance);
            }
        }
    
private voidhandleBeforeEvent(org.apache.catalina.InstanceEvent event, java.lang.String eventType)


        Object instance = null;
        if (eventType.equals(InstanceEvent.BEFORE_FILTER_EVENT)) {
            instance = event.getFilter();
        } else {
            instance = event.getServlet();
        }            
        Context context = (Context) event.getWrapper().getParent();

        // set security context
        // BEGIN IASRI 4688449
        //try {
        Realm ra = context.getRealm();
        /** IASRI 4713234
        if (ra != null) {
            HttpServletRequest request = 
                (HttpServletRequest) event.getRequest();
            if (request != null && request.getUserPrincipal() != null) {
                WebPrincipal prin = 
                    (WebPrincipal) request.getUserPrincipal();
                // ra.authenticate(prin);
                
                // It is inefficient to call authenticate just to set
                // sec.ctx.  Instead, WebPrincipal modified to keep the
                // previously created secctx, and set it here directly.

                SecurityContext.setCurrent(prin.getSecurityContext());
            }
        }
        **/
        // START OF IASRI 4713234
        if (ra != null) {

            ServletRequest request = 
		(ServletRequest) event.getRequest();
            if (request != null && request instanceof HttpServletRequest) {

                HttpServletRequest hreq = (HttpServletRequest)request;
		HttpServletRequest base = hreq;
		
		Principal prin = hreq.getUserPrincipal();
		Principal basePrincipal = prin; 
		
		boolean wrapped = false;

		while (prin != null && base != null) {
		    
		    if (base instanceof ServletRequestWrapper) {
			// unwarp any wrappers to find the base object
			ServletRequest sr = 
			    ((ServletRequestWrapper) base).getRequest();

			if (sr instanceof HttpServletRequest) {

			    base = (HttpServletRequest) sr;
			    wrapped = true;
			    continue;
			} 
		    }

		    if (wrapped) {
			basePrincipal = base.getUserPrincipal();
		    } 

		    else if (base instanceof CoyoteRequestFacade) {
			// try to avoid the getUnWrappedCoyoteRequest call
			// when we can identify see we have the texact class.
			if (base.getClass() != CoyoteRequestFacade.class) {
			    basePrincipal = ((CoyoteRequestFacade)base).
				getUnwrappedCoyoteRequest().getUserPrincipal();
			}
		    } else {
			basePrincipal = base.getUserPrincipal();
		    }

		    break;
		}

		if (prin != null && prin == basePrincipal && 
		    prin instanceof WebPrincipal) {

		    SecurityContext.setCurrent
			(getSecurityContextForPrincipal(prin));
		    
		} else if (prin != basePrincipal) {
		    
		    // the wrapper has overridden getUserPrincipal
		    // reject the request if the wrapper does not have
		    // the necessary permission.

		    checkObjectForDoAsPermission(hreq);

		    SecurityContext.setCurrent
			(getSecurityContextForPrincipal(prin));
		}
	    }
        }
        // END OF IASRI 4713234
        //} catch (Exception ex) {
            /** IASRI 4660742
            ex.printStackTrace();
	          **/
	          // START OF IASRI 4660742
                  //_logger.log(Level.SEVERE,"web_server.excep_handle_before_event",ex);
	          // END OF IASRI 4660742
        //}
        // END IASRI 4688449

        ComponentInvocation inv = new ComponentInvocation(instance, context);
        try {
            im.preInvoke(inv);
            if (eventType.equals(InstanceEvent.BEFORE_SERVICE_EVENT)) {
                // enlist resources with TM for service method
                Transaction tran = null;
                if ((tran = tm.getTransaction()) != null) {
                    inv.setTransaction(tran);
                }
                tm.enlistComponentResources();
            } else if (eventType.equals(InstanceEvent.BEFORE_INIT_EVENT)) {
                
                // Perform any required resource injection on the servlet 
                // instance.  This needs to be done after the invocation of
                // preInvoke, but before any of the servlet's application
                // code is executed.
                JndiNameEnvironment desc = (JndiNameEnvironment) 
                    Switch.getSwitch().getDescriptorFor(context);

                // There won't be a corresponding J2EE naming environment
                // descriptor for certain internal servlets so just skip
                // injection for those ones.
                if( desc != null
                        && instance.getClass() != DefaultServlet.class
                        && instance.getClass() != JspServlet.class) {
                    injectionMgr.injectInstance(instance, desc);
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(
                _logger.getResourceBundle().getString(
                    "web_server.excep_handle_before_event"),
                ex);
        }
    
public voidinstanceEvent(org.apache.catalina.InstanceEvent event)

        String eventType = event.getType();
	if(_logger.isLoggable(Level.FINEST)) {
            _logger.log(Level.FINEST,"*** InstanceEvent: " + eventType);
        }
        if (beforeEvents.contains(eventType)) {
            handleBeforeEvent(event, eventType);
        } else if (afterEvents.contains(eventType)) {
            handleAfterEvent(event, eventType);
        }