public static void shutdownThreadPoolsNow() {
    try {
      ThreadPoolManager.shutdownAllNow();
    } catch (Throwable ex) {
      // Log info:
      {
        Level level = Level.SEVERE;
        Logger logger = log;
        if (logger.isLoggable(level)) {
          String message = "Failure to stop thread pools!";
          logger.log(level, message, ex);
        }
      }

      throw new RuntimeException(ex);
    }
  }
  protected void statFlush() {
    try {
      DSLDataAccessorFactory f = DefaultDSLDataAccessorFactory.getInstance();
      DSLDataAccessor a = f.getDSLDataAccessor();

      a.statFlush();
    } catch (Throwable ex) {
      // Log info:
      {
        Level level = Level.SEVERE;
        Logger logger = log;
        if (logger.isLoggable(level)) {
          String message = "Failure to flush statistical info!";
          logger.log(level, message, ex);
        }
      }
    }
  }
  protected void stopScheduler() {
    try {
      SchedulerFactory f = DefaultSchedulerFactory.getInstance();
      Scheduler s = f.getScheduler();
      s.stop(null, null);
    } catch (IOException ex) {
      // Log info:
      {
        String message = "Failure to stop scheduler!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    } catch (Throwable ex) {
      // Log info:
      {
        String message = "Failure to stop scheduler!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    }
  }
  protected void stopTerminal() {
    try {
      TerminalFactory f = DefaultTerminalFactory.getInstance();
      Terminal t = f.getTerminal();
      t.stop();
    } catch (IOException ex) {
      // Log info:
      {
        String message = "Failure to stop terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    } catch (Throwable ex) {
      // Log info:
      {
        String message = "Failure to stop terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    }
  }
/**
 * @author <a href="mailto:[email protected]" >Morten Sabroe Mortensen</a>
 * @version $Id: ApplicationContextListener.java,v 1.3 2009/10/07 09:38:45 momor Exp $
 */
public class ApplicationContextListener implements ServletContextListener {
  /** Constructor. */
  public ApplicationContextListener() {
    super();
  }

  /** */
  public static Logger log = Logger.getLogger(ApplicationContextListener.class.getName());

  /** */
  private void startScheduler() {
    try {
      SchedulerFactory f = DefaultSchedulerFactory.getInstance();
      Scheduler s = f.getScheduler();
      s.start(null, null);
    } catch (IOException ex) {
      // Log info:
      {
        String message = "Failure to start scheduler!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    } catch (Throwable ex) {
      // Log info:
      {
        String message = "Failure to start scheduler!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    }
  }

  /** */
  protected void stopScheduler() {
    try {
      SchedulerFactory f = DefaultSchedulerFactory.getInstance();
      Scheduler s = f.getScheduler();
      s.stop(null, null);
    } catch (IOException ex) {
      // Log info:
      {
        String message = "Failure to stop scheduler!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    } catch (Throwable ex) {
      // Log info:
      {
        String message = "Failure to stop scheduler!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    }
  }

  /** */
  protected void startTerminal() {
    try {
      TerminalFactory f = DefaultTerminalFactory.getInstance();
      Terminal t = f.getTerminal();
      t.start();
    } catch (IOException ex) {
      // Log info:
      {
        String message = "Failure to start terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    } catch (Throwable ex) {
      // Log info:
      {
        String message = "Failure to start terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    }
  }

  /** */
  protected void stopTerminal() {
    try {
      TerminalFactory f = DefaultTerminalFactory.getInstance();
      Terminal t = f.getTerminal();
      t.stop();
    } catch (IOException ex) {
      // Log info:
      {
        String message = "Failure to stop terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    } catch (Throwable ex) {
      // Log info:
      {
        String message = "Failure to stop terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    }
  }

  /** */
  public void contextInitialized(ServletContextEvent ev) {
    startTerminal();
    startScheduler();

    ensureInitOfDefaultDSLAMProviderManager();

    dk.tdc.example.RMI.server.DSLDataAccessorRMIServer.bindStubToRMIRegistry();
  }

  /** */
  protected void ensureInitOfDefaultDSLAMProviderManager() {
    /*
     * Note:
     *   This forces class load of the default DSLAM-provider manager and
     *   implies initialization.
     *   In particular, this initialization may imply initialization of
     *   contained DSLAM-provider allocators.
     */

    DSLAMProviderManagerFactory f = DefaultDSLAMProviderManagerFactory.getInstance();

    try {
      DSLAMProviderManager m =
          f.getDSLAMProviderManager(); // yes, force classload of the default instance!
    } catch (IOException ex) {
      // Ignore!
    }
  }

  /** */
  public static void shutdownThreadPools() {
    try {
      ThreadPoolManager.shutdownAll();
    } catch (Throwable ex) {
      // Log info:
      {
        Level level = Level.SEVERE;
        Logger logger = log;
        if (logger.isLoggable(level)) {
          String message = "Failure to stop thread pools!";
          logger.log(level, message, ex);
        }
      }

      throw new RuntimeException(ex);
    }
  }

  /** */
  public static void shutdownThreadPoolsNow() {
    try {
      ThreadPoolManager.shutdownAllNow();
    } catch (Throwable ex) {
      // Log info:
      {
        Level level = Level.SEVERE;
        Logger logger = log;
        if (logger.isLoggable(level)) {
          String message = "Failure to stop thread pools!";
          logger.log(level, message, ex);
        }
      }

      throw new RuntimeException(ex);
    }
  }

  /** */
  protected void statFlush() {
    try {
      DSLDataAccessorFactory f = DefaultDSLDataAccessorFactory.getInstance();
      DSLDataAccessor a = f.getDSLDataAccessor();

      a.statFlush();
    } catch (Throwable ex) {
      // Log info:
      {
        Level level = Level.SEVERE;
        Logger logger = log;
        if (logger.isLoggable(level)) {
          String message = "Failure to flush statistical info!";
          logger.log(level, message, ex);
        }
      }
    }
  }

  /** */
  public void contextDestroyed(ServletContextEvent ev) {
    // Shut down DSLAM-provider hieracy:
    {
      DSLAMProviderManagerFactory f = DefaultDSLAMProviderManagerFactory.getInstance();
      f.dispose(); // shut it all down!
    }

    stopScheduler();
    stopTerminal();

    statFlush();

    // shutdownThreadPools();
    shutdownThreadPoolsNow();
  }
}