Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This code is placed in the public domain by its author, Tim Peierls.
- */
- package com.example.server;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.google.inject.Stage;
- import com.google.inject.servlet.GuiceServletContextListener;
- import javax.inject.Inject;
- import javax.servlet.ServletContextEvent;
- import org.nnsoft.guice.rocoto.configuration.ConfigurationModule;
- import static org.nnsoft.guice.rocoto.Rocoto.expandVariables;
- //import org.restlet.ext.guice.RestletGuice;
- /**
- * The main entry point for the server, which can operate
- * either as a servlet context listener or as a standalone
- * program. This is where the Guice injector is created.
- */
- public class Main extends GuiceServletContextListener {
- /**
- * Driver for standalone mode. Ignores the fact that Main is a servlet
- * context listener and just creates the injector with a direct call,
- * gets the main service, and starts it.
- */
- public static void main(String... args) {
- new Main(DeploymentMode.STANDALONE)
- .getInjector()
- .getInstance(MainService.class)
- .start();
- }
- /**
- * Constructor for servlet mode. This class is the servlet context listener
- * named in the web.xml file. {@link GuiceServletContextListener} arranges
- * to call {@link #getInjector getInjector} during context initialization.
- */
- public Main() {
- this(DeploymentMode.SERVLET);
- }
- //
- // GuiceServletContextListener methods
- //
- /**
- * Provides the injector to the {@link GuiceServletContextListener} in
- * servlet mode and is called directly in standalone mode (in {@link #main main}).
- * Uncomment the "Restlet" in front of "Guice.createInjector" to use the Restlet
- * Guice extension, which makes a FinderFactory available that can produce
- * injecting Finders for your resources.
- */
- @Override protected Injector getInjector() {
- return /*Restlet*/Guice.createInjector(getStage(), expandVariables(
- // Other modules go here: ...
- new AwsCredentialsModule(), // Shows how to bind AWS credentials for injection with @Named
- new MainServletModule(), // Servlet configuration (ignored in standalone mode)
- new ConfigurationModule() {
- @Override protected void bindConfigurations() {
- // MainComponent uses this to decide whether to add a server
- // connector for HTTP, which is needed only in standalone mode.
- // (It's added automatically in servlet mode.)
- bind(DeploymentMode.class).toInstance(deploymentMode);
- // Need to bind HTTP_PORT in MainComponent regardless of run mode.
- // Variables are expanded by Rocoto.
- bindProperty(MainComponent.HTTP_PORT)
- .toValue("${com.example.server.httpPort|8182}");
- // In development, we set all Ant properties that begin with
- // "com.example.server." as system properties, but we don't
- // define any Elastic Beanstalk system properties. In Elastic
- // Beanstalk, we _only_ get the E.B. system properties.
- bindSystemProperties();
- // Ensures injection of Main.service. In servlet mode, service
- // needs to be available to contextInitialized so it can start
- // the service. (In standalone mode, we start the service directly.)
- requestInjection(Main.this);
- }
- }
- ));
- }
- //
- // ServletContextListener methods
- //
- /**
- * Used to start {@link MainService} in servlet mode.
- */
- @Override public void contextInitialized(ServletContextEvent event) {
- // Injector is created here.
- super.contextInitialized(event);
- // Starts a new thread and returns immediately.
- service.start();
- }
- /**
- * Used to stop {@link MainService} in servlet mode.
- */
- @Override public void contextDestroyed(ServletContextEvent event) {
- try {
- service.stopAndWait();
- } finally {
- // Let the servlet machinery do its final cleanup, like detecting
- // stray thread locals and other annoying things.
- super.contextDestroyed(event);
- }
- }
- /**
- * Define this differently to obtain a Guice stage from,
- * for example, system or file properties.
- */
- protected Stage getStage() {
- return Stage.DEVELOPMENT;
- }
- //
- // Implementation
- //
- /**
- * The main service, injected so servlet context listener can start it.
- */
- @Inject volatile MainService service;
- /**
- * Common constructor saves the deployment mode.
- */
- private Main(final DeploymentMode deploymentMode) {
- this.deploymentMode = deploymentMode;
- }
- private final DeploymentMode deploymentMode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement