/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.web.tomcat.security;

import org.apache.catalina.InstanceEvent;
import org.apache.catalina.InstanceListener;
import org.apache.catalina.Wrapper;
import org.jboss.logging.Logger;
import org.jboss.metadata.WebMetaData;
import org.jboss.security.RunAsIdentity;

/**
 * An InstanceListener used to push/pop the servlet run-as identity for the
 * init/destroy lifecycle events.
 * 
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.10.3 $
 */
public class RunAsListener implements InstanceListener
{
   /** There is no api to install an initialized listener so the
    * WebMetaData had to be passed via a thread local.
    */
   public static ThreadLocal metaDataLocal = new ThreadLocal();

   private static Logger log = Logger.getLogger(RunAsListener.class);
   private WebMetaData metaData;

   public RunAsListener()
   {
      this.metaData = (WebMetaData) metaDataLocal.get();
   }

   /**
    * Push the run-as identity on the before init/destroy, pop it on the
    * after init/destroy events.
    * 
    * @param event - the type of instance event
    */ 
   public void instanceEvent(InstanceEvent event)
   {
      Wrapper servlet = event.getWrapper();
      String type = event.getType();
      if (servlet != null && metaData != null)
      {
         boolean trace = log.isTraceEnabled();
         String name = servlet.getName();
         RunAsIdentity identity = metaData.getRunAsIdentity(name);
         if (trace)
            log.trace(name + ", runAs: " + identity);
         // Push the identity on the before init/destroy
         if( type.equals(InstanceEvent.BEFORE_INIT_EVENT)
            || type.equals(InstanceEvent.BEFORE_DESTROY_EVENT) )
         {
            SecurityAssociationActions.pushRunAsIdentity(identity);
         }
         // Pop the identity on the after init/destroy
         else if( type.equals(InstanceEvent.AFTER_INIT_EVENT)
            || type.equals(InstanceEvent.AFTER_DESTROY_EVENT) )
         {
            SecurityAssociationActions.popRunAsIdentity();
         }
      }
   }
}
