/**
   * Listens for {@link LifecycleEvent} for the start of the {@link Server} to initialize itself and
   * then for after_stop events of each {@link Context}.
   */
  @Override
  public void lifecycleEvent(LifecycleEventRemoteInterface event)
      throws RemoteException, RemoteException {
    try {
      Lifecycle lifecycle = event.getLifecycle();
      if (Lifecycle.AFTER_START_EVENT.equals(event.getType()) && lifecycle instanceof Server) {
        // when the server starts, we register ourself as listener for
        // all context
        // as well as container event listener so that we know when new
        // Context are deployed
        Server server = (Server) lifecycle;
        registerListenersForServer(server);
      }

      if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType()) && lifecycle instanceof Server) {
        // Server is shutting down, so thread pools will be shut down so
        // there is no need to clean the threads
        serverStopping = true;
      }

      if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType()) && lifecycle instanceof Context) {
        stopIdleThreads((Context) lifecycle);
      }
    } catch (Exception e) {
      String msg = sm.getString("threadLocalLeakPreventionListener.lifecycleEvent.error", event);
      log.error(msg, e);
    }
  }
 @Override
 public void lifecycleEvent(LifecycleEvent event) {
   if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType())) {
     // The container is getting stopped, close all current connections
     Iterator<Request> iterator = cometRequests.iterator();
     while (iterator.hasNext()) {
       Request request = iterator.next();
       // Remove the session tracking attribute as it isn't
       // serializable or required.
       HttpSession session = request.getSession(false);
       if (session != null) {
         session.removeAttribute(cometRequestsAttribute);
       }
       // Close the comet connection
       try {
         CometEventImpl cometEvent = request.getEvent();
         cometEvent.setEventType(CometEvent.EventType.END);
         cometEvent.setEventSubType(CometEvent.EventSubType.WEBAPP_RELOAD);
         getNext().event(request, request.getResponse(), cometEvent);
         cometEvent.close();
       } catch (Exception e) {
         container.getLogger().warn(sm.getString("cometConnectionManagerValve.event"), e);
       }
     }
     cometRequests.clear();
   }
 }