Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package com.nital;
  2.  
  3. import java.util.logging.Logger;
  4.  
  5. import com.nital.bundle.ReplayingActivator;
  6. import com.nital.net.Network;
  7.  
  8. /**
  9.  * This class represents the application that will form the base of
  10.  * other source files which will be able to run within this program.
  11.  * @author Thomas Nappo
  12.  */
  13. public final class Server extends ReplayingActivator<Server.State> {
  14.  
  15.     /**
  16.      * This singleton logger instance can be used for logging various
  17.      * component messages to the console.
  18.      */
  19.     private static final Logger logger = Logger.getLogger(Server.class.getName());
  20.  
  21.     /**
  22.      * Encapsulates the singleton instance of the server.
  23.      * @author Thomas Nappo
  24.      */
  25.     private static final class SingletonContainer {
  26.         private static final Server singleton = new Server();
  27.     }
  28.  
  29.     /**
  30.      * Gets the singleton instance of the server.
  31.      * @return The one and only instance of the server.
  32.      */
  33.     public static Server getSingleton() {
  34.         return SingletonContainer.singleton;
  35.     }
  36.  
  37.     /**
  38.      * Defines the component activation state.
  39.      * @author Thomas Nappo
  40.      */
  41.     protected static enum State {
  42.         NETWORK, FUTURE;
  43.     }
  44.    
  45.     /**
  46.      * Constructs a new server.
  47.      */
  48.     private Server() {
  49.         // changing changes the initial
  50.         // state of the parent activator.
  51.         super(State.NETWORK);
  52.     }
  53.  
  54.     /**
  55.      * This is the entry point of the application which implements
  56.      * the command line interface. From here we begin initialization
  57.      * of the application's separate components.
  58.      * @param args The command line parameters.
  59.      */
  60.     public static void main(String[] args) {
  61.         getSingleton(); // create the singleton
  62.        
  63.         logger.info("Initializing Nital...");
  64.        
  65.         // call for activation
  66.         getSingleton().start(getSingleton().getState());
  67.  
  68.         /*
  69.          * Append a shutdown hook which calls the stop method
  70.          * so we can be sure to run shutdown procedures before
  71.          * process termination.
  72.          */
  73.         Runtime.getRuntime().addShutdownHook(new Thread() {
  74.             @Override
  75.             public void run() {
  76.                 getSingleton().stop();
  77.             }
  78.         });
  79.     }
  80.  
  81.     @Override
  82.     public void start(State state) {
  83.         switch (state) {
  84.         case NETWORK:
  85.             Network.getSingleton().start();
  86.             checkpoint(State.FUTURE);
  87.             break;
  88.         }
  89.     }
  90.  
  91.     @Override
  92.     public void stop(State state) {
  93.         logger.info("Stopping Nital...");
  94.  
  95.         /*
  96.          * Here you can implement whatever shutdown procedures
  97.          * should take place.
  98.          */
  99.  
  100.         System.exit(0);
  101.     }
  102.  
  103.     @Override
  104.     protected void checkpoint(State state) {
  105.         logger.info("Reached deploy stage: " + state.name().toLowerCase());
  106.         super.checkpoint(state);
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement