Advertisement
tpeierls

com.example.server.Main

Jan 23rd, 2012
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.11 KB | None | 0 0
  1. /*
  2.  * This code is placed in the public domain by its author, Tim Peierls.
  3.  */
  4. package com.example.server;
  5.  
  6. import com.google.inject.Guice;
  7. import com.google.inject.Injector;
  8. import com.google.inject.Stage;
  9. import com.google.inject.servlet.GuiceServletContextListener;
  10.  
  11. import javax.inject.Inject;
  12.  
  13. import javax.servlet.ServletContextEvent;
  14.  
  15. import org.nnsoft.guice.rocoto.configuration.ConfigurationModule;
  16. import static org.nnsoft.guice.rocoto.Rocoto.expandVariables;
  17.  
  18. //import org.restlet.ext.guice.RestletGuice;
  19.  
  20.  
  21. /**
  22.  * The main entry point for the server, which can operate
  23.  * either as a servlet context listener or as a standalone
  24.  * program. This is where the Guice injector is created.
  25.  */
  26. public class Main extends GuiceServletContextListener {
  27.  
  28.     /**
  29.      * Driver for standalone mode. Ignores the fact that Main is a servlet
  30.      * context listener and just creates the injector with a direct call,
  31.      * gets the main service, and starts it.
  32.      */
  33.     public static void main(String... args) {
  34.         new Main(DeploymentMode.STANDALONE)
  35.             .getInjector()
  36.             .getInstance(MainService.class)
  37.             .start();
  38.     }
  39.  
  40.     /**
  41.      * Constructor for servlet mode. This class is the servlet context listener
  42.      * named in the web.xml file. {@link GuiceServletContextListener} arranges
  43.      * to call {@link #getInjector getInjector} during context initialization.
  44.      */
  45.     public Main() {
  46.         this(DeploymentMode.SERVLET);
  47.     }
  48.  
  49.  
  50.     //
  51.     // GuiceServletContextListener methods
  52.     //
  53.  
  54.     /**
  55.      * Provides the injector to the {@link GuiceServletContextListener} in
  56.      * servlet mode and is called directly in standalone mode (in {@link #main main}).
  57.      * Uncomment the "Restlet" in front of "Guice.createInjector" to use the Restlet
  58.      * Guice extension, which makes a FinderFactory available that can produce
  59.      * injecting Finders for your resources.
  60.      */
  61.     @Override protected Injector getInjector() {
  62.         return /*Restlet*/Guice.createInjector(getStage(), expandVariables(
  63.  
  64.             // Other modules go here: ...
  65.  
  66.             new AwsCredentialsModule(), // Shows how to bind AWS credentials for injection with @Named
  67.  
  68.             new MainServletModule(),    // Servlet configuration (ignored in standalone mode)
  69.  
  70.             new ConfigurationModule() {
  71.                 @Override protected void bindConfigurations() {
  72.                     // MainComponent uses this to decide whether to add a server
  73.                     // connector for HTTP, which is needed only in standalone mode.
  74.                     // (It's added automatically in servlet mode.)
  75.                     bind(DeploymentMode.class).toInstance(deploymentMode);
  76.  
  77.                     // Need to bind HTTP_PORT in MainComponent regardless of run mode.
  78.                     // Variables are expanded by Rocoto.
  79.                     bindProperty(MainComponent.HTTP_PORT)
  80.                         .toValue("${com.example.server.httpPort|8182}");
  81.  
  82.                     // In development, we set all Ant properties that begin with
  83.                     // "com.example.server." as system properties, but we don't
  84.                     // define any Elastic Beanstalk system properties. In Elastic
  85.                     // Beanstalk, we _only_ get the E.B. system properties.
  86.                     bindSystemProperties();
  87.  
  88.                     // Ensures injection of Main.service. In servlet mode, service
  89.                     // needs to be available to contextInitialized so it can start
  90.                     // the service. (In standalone mode, we start the service directly.)
  91.                     requestInjection(Main.this);
  92.                 }
  93.             }
  94.         ));
  95.     }
  96.  
  97.  
  98.     //
  99.     // ServletContextListener methods
  100.     //
  101.  
  102.     /**
  103.      * Used to start {@link MainService} in servlet mode.
  104.      */
  105.     @Override public void contextInitialized(ServletContextEvent event) {
  106.         // Injector is created here.
  107.         super.contextInitialized(event);
  108.  
  109.         // Starts a new thread and returns immediately.
  110.         service.start();
  111.     }
  112.  
  113.     /**
  114.      * Used to stop {@link MainService} in servlet mode.
  115.      */
  116.     @Override public void contextDestroyed(ServletContextEvent event) {
  117.         try {
  118.             service.stopAndWait();
  119.         } finally {
  120.             // Let the servlet machinery do its final cleanup, like detecting
  121.             // stray thread locals and other annoying things.
  122.             super.contextDestroyed(event);
  123.         }
  124.     }
  125.  
  126.  
  127.     /**
  128.      * Define this differently to obtain a Guice stage from,
  129.      * for example, system or file properties.
  130.      */
  131.     protected Stage getStage() {
  132.         return Stage.DEVELOPMENT;
  133.     }
  134.  
  135.  
  136.     //
  137.     // Implementation
  138.     //
  139.  
  140.     /**
  141.      * The main service, injected so servlet context listener can start it.
  142.      */
  143.     @Inject volatile MainService service;
  144.  
  145.     /**
  146.      * Common constructor saves the deployment mode.
  147.      */
  148.     private Main(final DeploymentMode deploymentMode) {
  149.         this.deploymentMode = deploymentMode;
  150.     }
  151.  
  152.     private final DeploymentMode deploymentMode;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement