Java 类com.google.inject.servlet.GuiceServletContextListener 实例源码

项目:nexus-public    文件:SiestaTestSupport.java   
@Before
public void startJetty() throws Exception {
  servletTester = new ServletTester();
  servletTester.getContext().addEventListener(new GuiceServletContextListener()
  {
    final Injector injector = Guice.createInjector(new TestModule());

    @Override
    protected Injector getInjector() {
      return injector;
    }
  });

  url = servletTester.createConnector(true) + TestModule.MOUNT_POINT;
  servletTester.addFilter(GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
  servletTester.addServlet(DummyServlet.class, "/*");
  servletTester.start();

  client = ClientBuilder.newClient();
}
项目:Mastering-Mesos    文件:JettyServerModule.java   
@VisibleForTesting
static ServletContextListener makeServletContextListener(
    final Injector parentInjector,
    final Module childModule) {

  return new GuiceServletContextListener() {
    @Override
    protected Injector getInjector() {
      return parentInjector.createChildInjector(
          childModule,
          new JerseyServletModule() {
            @Override
            protected void configureServlets() {
              bind(HttpStatsFilter.class).in(Singleton.class);
              filter("*").through(HttpStatsFilter.class);

              bind(LeaderRedirectFilter.class).in(Singleton.class);
              filterRegex(allOf(LEADER_ENDPOINTS))
                  .through(LeaderRedirectFilter.class);

              bind(GuiceContainer.class).in(Singleton.class);
              filterRegex(allOf(ImmutableSet.copyOf(JAX_RS_ENDPOINTS.values())))
                  .through(GuiceContainer.class, GUICE_CONTAINER_PARAMS);

              filterRegex("/assets/scheduler(?:/.*)?").through(LeaderRedirectFilter.class);

              serve("/assets", "/assets/*")
                  .with(new DefaultServlet(), ImmutableMap.of(
                      "resourceBase", STATIC_ASSETS_ROOT,
                      "dirAllowed", "false"));

              for (Class<?> jaxRsHandler : JAX_RS_ENDPOINTS.keySet()) {
                bind(jaxRsHandler);
              }
            }
          });
    }
  };
}
项目:robe    文件:ServletScanner.java   
private void createServletEventListener(Environment environment, final Injector injector) {
    environment.getApplicationContext().addEventListener(new GuiceServletContextListener() {
        @Override
        protected Injector getInjector() {
            return injector;
        }
    });
}
项目:exposr    文件:Main.java   
private static HttpServer setupHttpServer(URI baseUri, final Injector injector) {
    final HttpServer serverLocal = GrizzlyHttpServerFactory
            .createHttpServer(baseUri, false);

    final WebappContext context = new WebappContext("Guice Webapp sample",
            "");

    context.addListener(new GuiceServletContextListener(){
        @Override
        protected Injector getInjector() {
            return injector;
        }
    });

    // Initialize and register Jersey ServletContainer
    final ServletRegistration servletRegistration = context.addServlet(
            "ServletContainer", ServletContainer.class);
    servletRegistration.addMapping("/*");
    servletRegistration.setInitParameter("javax.ws.rs.Application",
            WebApp.class.getName());

    // Initialize and register GuiceFilter
    final FilterRegistration registration = context.addFilter(
            "GuiceFilter", GuiceFilter.class);
    registration.addMappingForUrlPatterns(
            EnumSet.allOf(DispatcherType.class), "/*");

    context.deploy(serverLocal);

    return serverLocal;
}
项目:lilrest    文件:JaxRsServerModule.java   
@Provides
public ServletContextHandler servletContext(GuiceResteasyBootstrapServletContextListener resteasyListener,
                                            GuiceFilter guiceFilter,
                                            GuiceServletContextListener guiceServletContextListener) {
    final FilterHolder guiceFilterHolder = new FilterHolder(guiceFilter);
    final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addFilter(guiceFilterHolder, "/*", EnumSet.allOf(DispatcherType.class));
    context.addEventListener(resteasyListener);
    context.addEventListener(guiceServletContextListener);
    return context;
}
项目:lilrest    文件:JaxRsServerModule.java   
@Provides
private GuiceServletContextListener getGuiceServletContextListener(final Injector injector) {
    return new GuiceServletContextListener() {
        @Override
        protected Injector getInjector() {
            return injector;
        }
    };
}
项目:joynr    文件:AbstractServiceInterfaceTest.java   
@Before
public void setUp() throws Exception {

    // starts the server with a random port
    jettyServer = new Server(0);

    WebAppContext bpCtrlWebapp = new WebAppContext();
    bpCtrlWebapp.setResourceBase("./src/main/java");
    bpCtrlWebapp.setParentLoaderPriority(true);

    bpCtrlWebapp.addFilter(GuiceFilter.class, "/*", null);
    bpCtrlWebapp.addEventListener(new GuiceServletContextListener() {

        private Injector injector;

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            injector = Guice.createInjector(getServletTestModule());
            super.contextInitialized(servletContextEvent);
        }

        @Override
        protected Injector getInjector() {
            return injector;
        }
    });

    jettyServer.setHandler(bpCtrlWebapp);

    jettyServer.start();

    int port = ((ServerConnector) jettyServer.getConnectors()[0]).getLocalPort();
    serverUrl = String.format("http://localhost:%d", port);
}
项目:alexa-routing    文件:Main.java   
public static void main(String[] args) throws Exception {
    System.setProperty(Sdk.DISABLE_REQUEST_SIGNATURE_CHECK_SYSTEM_PROPERTY, "true");

    String envPort = System.getenv("PORT");
    int port = envPort == null || envPort.isEmpty() ? 8000 : Integer.valueOf(envPort);


    // Configure server and its associated servlets
    Server server = new Server(port);

    ServletContextHandler context = new ServletContextHandler();
    context.addEventListener(new GuiceServletContextListener() {
        @Override
        protected Injector getInjector() {
            Module services = new AbstractModule() {
                @Override
                protected void configure() {
                    bind(SpeechRouter.class).toProvider(new Provider<SpeechRouter>() {
                        @Inject Injector injector;

                        @Override
                        public SpeechRouter get() {
                            try {
                                return SpeechRouter.create(injector, "rottentomatoes");
                            } catch (Exception e) {
                                e.printStackTrace();
                                return null;
                            }
                        }
                    }).in(Singleton.class);
                    bind(AlexaSessionProvider.class).in(RequestScoped.class);
                    bind(RequestContextProvider.class).in(RequestScoped.class);
                    bind(SpeechletServlet.class).toProvider(new Provider<SpeechletServlet>() {
                        @Inject SpeechRouter router;
                        @Inject Injector injector;

                        @Override
                        public SpeechletServlet get() {
                            SpeechletServlet servlet = new SpeechletServlet();
                            servlet.setSpeechlet(new RoutingSpeechlet(router, injector));
                            return servlet;
                        }
                    }).in(Singleton.class);
                }
            };

            return Guice.createInjector(services, new ServletModule() {
                @Override
                protected void configureServlets() {
                    serve("/sample-utterances").with(SampleUtterancesServlet.class);
                    serve("/intent-schema").with(IntentSchemaServlet.class);
                    serve("/rotten-tomatoes").with(SpeechletServlet.class);
                }
            });
        }
    });
    context.addFilter(GuiceFilter.class, "/*", null);
    context.addServlet(DefaultServlet.class, "/");
    server.setHandler(context);
    server.start();
    server.join();
}
项目:Wiab.pro    文件:ServerRpcProvider.java   
public void startWebSocketServer(final Injector injector) {
  httpServer = new Server();

  List<Connector> connectors = getSelectChannelConnectors(httpAddresses);
  if (connectors.isEmpty()) {
    LOG.severe("No valid http end point address provided!");
  }
  for (Connector connector : connectors) {
    httpServer.addConnector(connector);
  }
  final WebAppContext context = new WebAppContext();

  context.setParentLoaderPriority(true);

  if (jettySessionManager != null) {
    // This disables JSessionIDs in URLs redirects
    // see: http://stackoverflow.com/questions/7727534/how-do-you-disable-jsessionid-for-jetty-running-with-the-eclipse-jetty-maven-plu
    // and: http://jira.codehaus.org/browse/JETTY-467?focusedCommentId=114884&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-114884
    jettySessionManager.setSessionIdPathParameterName(null);

    context.getSessionHandler().setSessionManager(jettySessionManager);
  }
  final ResourceCollection resources = new ResourceCollection(resourceBases);
  context.setBaseResource(resources);

  addWebSocketServlets();

  try {
    final Injector parentInjector = injector;

    final ServletModule servletModule = getServletModule(parentInjector);

    ServletContextListener contextListener = new GuiceServletContextListener() {

      private final Injector childInjector = parentInjector.createChildInjector(servletModule);

      @Override
      protected Injector getInjector() {
        return childInjector;
      }
    };

    context.addEventListener(contextListener);
    context.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
    context.addFilter(GzipFilter.class, "/webclient/*", EnumSet.allOf(DispatcherType.class));
    httpServer.setHandler(context);

    httpServer.start();
    restoreSessions();

  } catch (Exception e) { // yes, .start() throws "Exception"
    LOG.severe("Fatal error starting http server.", e);
    return;
  }
  LOG.fine("WebSocket server running.");
}
项目:system-a-test    文件:SystemATestAbstract.java   
@Override
protected GuiceServletContextListener getServletConfig() {
    return new SystemAServletConfig();
}
项目:incubator-wave    文件:ServerRpcProvider.java   
public void startWebSocketServer(final Injector injector) {
  httpServer = new Server();

  List<Connector> connectors = getSelectChannelConnectors(httpAddresses);
  if (connectors.isEmpty()) {
    LOG.severe("No valid http end point address provided!");
  }
  for (Connector connector : connectors) {
    httpServer.addConnector(connector);
  }
  final WebAppContext context = new WebAppContext();

  context.setParentLoaderPriority(true);

  if (jettySessionManager != null) {
    // This disables JSessionIDs in URLs redirects
    // see: http://stackoverflow.com/questions/7727534/how-do-you-disable-jsessionid-for-jetty-running-with-the-eclipse-jetty-maven-plu
    // and: http://jira.codehaus.org/browse/JETTY-467?focusedCommentId=114884&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-114884
    jettySessionManager.setSessionIdPathParameterName(null);

    context.getSessionHandler().setSessionManager(jettySessionManager);
  }
  final ResourceCollection resources = new ResourceCollection(resourceBases);
  context.setBaseResource(resources);

  addWebSocketServlets();

  try {

    final ServletModule servletModule = getServletModule();

    ServletContextListener contextListener = new GuiceServletContextListener() {

      private final Injector childInjector = injector.createChildInjector(servletModule);

      @Override
      protected Injector getInjector() {
        return childInjector;
      }
    };

    context.addEventListener(contextListener);
    context.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
    context.addFilter(GzipFilter.class, "/webclient/*", EnumSet.allOf(DispatcherType.class));
    httpServer.setHandler(context);

    httpServer.start();
    restoreSessions();

  } catch (Exception e) { // yes, .start() throws "Exception"
    LOG.severe("Fatal error starting http server.", e);
    return;
  }
  LOG.fine("WebSocket server running.");
}
项目:AisAbnormal    文件:WebServer.java   
public void start() throws Exception {
    ((ServerConnector) server.getConnectors()[0]).setReuseAddress(true);

    // Root context
    context.setContextPath("/abnormal");

    // Setup static content
    context.setResourceBase("src/main/webapp/");
    context.addServlet(DefaultServlet.class, "/");

    // Enable Jersey debug output
    context.setInitParameter("com.sun.jersey.config.statistic.Trace", "true");

    // Enable CORS - cross origin resource sharing
    FilterHolder cors = new FilterHolder();
    cors.setInitParameter("allowedOrigins", "https?://localhost:*, https?://*.e-navigation.net:*");
    cors.setInitParameter("allowedHeaders", "*");
    cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
    cors.setFilter(new CrossOriginFilter());
    context.addFilter(cors, "*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.INCLUDE));

    // Little hack to satisfy OpenLayers URLs in DMA context
    RewritePatternRule openlayersRewriteRule = new RewritePatternRule();
    openlayersRewriteRule.setPattern("/abnormal/theme/*");
    openlayersRewriteRule.setReplacement("/abnormal/js/theme/");

    RewriteHandler rewrite = new RewriteHandler();
    rewrite.setRewriteRequestURI(true);
    rewrite.setRewritePathInfo(false);
    rewrite.setOriginalPathAttribute("requestedPath");
    rewrite.addRule(openlayersRewriteRule);
    rewrite.setHandler(context);
    server.setHandler(rewrite);

    // Setup Guice-Jersey integration
    context.addEventListener(new GuiceServletContextListener() {
        @Override
        protected Injector getInjector() {
            return Guice.createInjector(new RestModule(
                    repositoryName,
                    pathToEventDatabase,
                    eventRepositoryType,
                    eventDataDbHost,
                    eventDataDbPort,
                    eventDataDbName,
                    eventDataDbUsername,
                    eventDataDbPassword
            ));
        }
    });
    context.addFilter(com.google.inject.servlet.GuiceFilter.class, "/rest/*", EnumSet.allOf(DispatcherType.class));

    // Start the server
    server.start();
}
项目:jetty-guice    文件:JettyServer.java   
public JettyServer(int port, GuiceServletContextListener guiceConfig) {
    this.port = port;
    this.guiceConfig = guiceConfig;
}
项目:http-test    文件:WebAppTest.java   
/**
 * Java version of the web.xml snippet:
 * <pre>
 * {@code
 * <listener>
 *   <listener-class>it.bioko.mysystem.injection.MySystemServletConfig</listener-class>
 * </listener>
 * }
 * <pre>
 * @return An instance of a subclass of {@link GuiceServletContextListener}
 */
protected abstract GuiceServletContextListener getServletConfig();