@BeforeClass
  public static void startServer() throws Exception {
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.addConnector(connector);

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);

    ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
    container.addEndpoint(CaptureHeadersSocket.class);
    container.addEndpoint(EmptySocket.class);
    container.addEndpoint(NoExtensionsSocket.class);

    server.start();
    String host = connector.getHost();
    if (host == null) {
      host = "localhost";
    }
    int port = connector.getLocalPort();
    baseServerUri = new URI(String.format("ws://%s:%d/", host, port));
    LOG.debug("Server started on {}", baseServerUri);
  }
  @Before
  public void prepare() throws Exception {
    server = new Server();
    connector = new ServerConnector(server);
    server.addConnector(connector);

    ServletContextHandler context = new ServletContextHandler(server, "/", true, false);
    ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
    ServerEndpointConfig config =
        ServerEndpointConfig.Builder.create(ServerTextStreamer.class, PATH).build();
    container.addEndpoint(config);

    server.start();

    wsClient = ContainerProvider.getWebSocketContainer();
    server.addBean(wsClient, true);
  }
Beispiel #3
0
  @Test
  public void testPortableJMX() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);

    ServletContextHandler context = new ServletContextHandler(server, "/");

    WebSocketServerContainerInitializer.configureContext(context);

    String value =
        BayeuxServerImpl.ATTRIBUTE + "," + Oort.OORT_ATTRIBUTE + "," + Seti.SETI_ATTRIBUTE;
    context.setInitParameter(ServletContextHandler.MANAGED_ATTRIBUTES, value);

    // CometD servlet
    String cometdServletPath = "/cometd";
    String cometdURLMapping = cometdServletPath + "/*";
    ServletHolder cometdServletHolder = new ServletHolder(CometDServlet.class);
    cometdServletHolder.setInitParameter("timeout", "10000");
    cometdServletHolder.setInitParameter("ws.cometdURLMapping", cometdURLMapping);
    cometdServletHolder.setInitOrder(1);
    context.addServlet(cometdServletHolder, cometdURLMapping);

    ServletHolder jmxServletHolder = new ServletHolder(CometDJMXExporter.class);
    jmxServletHolder.setInitOrder(2);
    context.addServlet(jmxServletHolder, "/jmx");

    server.start();

    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    String domain = BayeuxServerImpl.class.getPackage().getName();
    Set<ObjectName> mbeanNames =
        mbeanServer.queryNames(ObjectName.getInstance(domain + ":*"), null);
    Assert.assertEquals(1, mbeanNames.size());
    ObjectName objectName = mbeanNames.iterator().next();
    @SuppressWarnings("unchecked")
    Set<String> channels = (Set<String>) mbeanServer.getAttribute(objectName, "channels");
    Assert.assertTrue(channels.size() > 0);

    server.stop();
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public Task<Void> start() {

    // Ensuring that jersey will use singletons from the orbit container.
    ServiceLocator locator = Injections.createLocator();
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration dc = dcs.createDynamicConfiguration();

    final List<Class<?>> classes = new ArrayList<>(providers);
    if (container != null) {
      classes.addAll(container.getClasses());
      for (final Class<?> c : container.getClasses()) {
        if (c.isAnnotationPresent(Singleton.class)) {
          Injections.addBinding(
              Injections.newFactoryBinder(
                      new Factory() {
                        @Override
                        public Object provide() {
                          return container.get(c);
                        }

                        @Override
                        public void dispose(final Object instance) {}
                      })
                  .to(c),
              dc);
        }
      }
    }
    dc.commit();

    final ResourceConfig resourceConfig = new ResourceConfig();

    // installing jax-rs classes known by the orbit container.
    for (final Class c : classes) {
      if (c.isAnnotationPresent(javax.ws.rs.Path.class)
          || c.isAnnotationPresent(javax.ws.rs.ext.Provider.class)) {
        resourceConfig.register(c);
      }
    }

    final WebAppContext webAppContext = new WebAppContext();
    final ProtectionDomain protectionDomain = EmbeddedHttpServer.class.getProtectionDomain();
    final URL location = protectionDomain.getCodeSource().getLocation();
    logger.info(location.toExternalForm());
    webAppContext.setInitParameter("useFileMappedBuffer", "false");
    webAppContext.setWar(location.toExternalForm());
    // this sets the default service locator to one that bridges to the orbit container.
    webAppContext.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator);
    webAppContext.setContextPath("/*");
    webAppContext.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");

    final ContextHandler resourceContext = new ContextHandler();
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[] {"index.html"});
    resourceHandler.setBaseResource(Resource.newClassPathResource("/web"));

    resourceContext.setHandler(resourceHandler);
    resourceContext.setInitParameter("useFileMappedBuffer", "false");
    final ContextHandlerCollection contexts = new ContextHandlerCollection();

    contexts.setHandlers(
        new Handler[] {
          wrapHandlerWithMetrics(resourceContext, "resourceContext"),
          wrapHandlerWithMetrics(webAppContext, "webAppContext")
        });

    server = new Server(port);
    server.setHandler(contexts);
    try {
      /// Initialize javax.websocket layer
      final ServerContainer serverContainer =
          WebSocketServerContainerInitializer.configureContext(webAppContext);

      for (Class c : classes) {
        if (c.isAnnotationPresent(ServerEndpoint.class)) {
          final ServerEndpoint annotation = (ServerEndpoint) c.getAnnotation(ServerEndpoint.class);

          final ServerEndpointConfig serverEndpointConfig =
              ServerEndpointConfig.Builder.create(c, annotation.value())
                  .configurator(
                      new ServerEndpointConfig.Configurator() {
                        @Override
                        public <T> T getEndpointInstance(final Class<T> endpointClass)
                            throws InstantiationException {
                          return container.get(endpointClass);
                        }
                      })
                  .build();

          serverContainer.addEndpoint(serverEndpointConfig);
        }
      }
    } catch (Exception e) {
      logger.error("Error starting jetty", e);
      throw new UncheckedException(e);
    }

    try {
      server.start();
    } catch (Exception e) {
      logger.error("Error starting jetty", e);
      throw new UncheckedException(e);
    }
    return Task.done();
  }
Beispiel #5
0
  @Test
  public void testJMX() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);

    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
    server.addBean(mbeanContainer);

    ServletContextHandler context = new ServletContextHandler(server, "/");

    WebSocketServerContainerInitializer.configureContext(context);

    String value =
        BayeuxServerImpl.ATTRIBUTE + "," + Oort.OORT_ATTRIBUTE + "," + Seti.SETI_ATTRIBUTE;
    context.setInitParameter(ServletContextHandler.MANAGED_ATTRIBUTES, value);

    // CometD servlet
    String cometdServletPath = "/cometd";
    String cometdURLMapping = cometdServletPath + "/*";
    ServletHolder cometdServletHolder = new ServletHolder(CometDServlet.class);
    cometdServletHolder.setInitParameter("timeout", "10000");
    cometdServletHolder.setInitParameter("ws.cometdURLMapping", cometdURLMapping);
    cometdServletHolder.setInitOrder(1);
    context.addServlet(cometdServletHolder, cometdURLMapping);

    ServletHolder oortServletHolder = new ServletHolder(OortStaticConfigServlet.class);
    oortServletHolder.setInitParameter(
        OortConfigServlet.OORT_URL_PARAM, "http://localhost" + cometdServletPath);
    oortServletHolder.setInitOrder(2);
    context.addServlet(oortServletHolder, "/oort");

    ServletHolder setiServletHolder = new ServletHolder(SetiServlet.class);
    setiServletHolder.setInitOrder(3);
    context.addServlet(setiServletHolder, "/seti");

    server.start();

    String domain = BayeuxServerImpl.class.getPackage().getName();
    Set<ObjectName> mbeanNames =
        mbeanServer.queryNames(ObjectName.getInstance(domain + ":*"), null);
    Assert.assertEquals(1, mbeanNames.size());
    ObjectName objectName = mbeanNames.iterator().next();
    @SuppressWarnings("unchecked")
    Set<String> channels = (Set<String>) mbeanServer.getAttribute(objectName, "channels");
    Assert.assertTrue(channels.size() > 0);

    domain = Oort.class.getPackage().getName();
    mbeanNames = mbeanServer.queryNames(ObjectName.getInstance(domain + ":*,type=oort"), null);
    Assert.assertEquals(1, mbeanNames.size());
    objectName = mbeanNames.iterator().next();

    String channel = "/foo";
    mbeanServer.invoke(
        objectName,
        "observeChannel",
        new Object[] {channel},
        new String[] {String.class.getName()});
    @SuppressWarnings("unchecked")
    Set<String> observedChannels =
        (Set<String>) mbeanServer.getAttribute(objectName, "observedChannels");
    Assert.assertTrue(observedChannels.contains(channel));

    domain = Seti.class.getPackage().getName();
    mbeanNames = mbeanServer.queryNames(ObjectName.getInstance(domain + ":*,type=seti"), null);
    Assert.assertEquals(1, mbeanNames.size());
    objectName = mbeanNames.iterator().next();
    ObjectName oortObjectName = (ObjectName) mbeanServer.getAttribute(objectName, "oort");
    Assert.assertEquals("oort", oortObjectName.getKeyProperty("type"));

    server.stop();
  }