Ejemplo n.º 1
0
/**
 * @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();
  }
}
Ejemplo n.º 2
0
/**
 * @author <a href="mailto:[email protected]" >Morten Sabroe Mortensen</a>
 * @version $Id: ApplicationContextListener.java,v 1.2 2009/10/07 10:12:09 momor Exp $
 */
public class ApplicationContextListener implements ServletContextListener {
  /** Constructor. */
  public ApplicationContextListener() {
    super();
  }

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

  /** */
  protected void initializeJSF() {
    // Make sure the faces application is set:
    {
      ApplicationFactory f =
          (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
      Application a = f.getApplication();
      if (!(a instanceof SystemApplication)) {
        Application b = new SystemApplication(a);
        f.setApplication(b);
      }
    }
  }

  /** */
  public void contextInitialized(ServletContextEvent ev) {
    initializeJSF();
  }

  /** */
  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) {
    statFlush();

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