@BeforeClass
  public static void setup() throws Exception {

    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder =
        new DeploymentInfo()
            .setClassLoader(ProgramaticLazyEndpointTest.class.getClassLoader())
            .setContextPath("/")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .addServlet(Servlets.servlet("add", AddEndpointServlet.class).setLoadOnStartup(100))
            .addServletContextAttribute(
                WebSocketDeploymentInfo.ATTRIBUTE_NAME,
                new WebSocketDeploymentInfo()
                    .setBuffers(new ByteBufferSlicePool(100, 1000))
                    .setWorker(DefaultServer.getWorker())
                    .addListener(
                        new WebSocketDeploymentInfo.ContainerReadyListener() {
                          @Override
                          public void ready(ServerWebSocketContainer container) {
                            deployment = container;
                          }
                        }))
            .setDeploymentName("servletContext.war");

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();

    DefaultServer.setRootHandler(manager.start());
  }
示例#2
0
  @Override
  public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
    WebSocketDeploymentInfo info =
        (WebSocketDeploymentInfo)
            deploymentInfo
                .getServletContextAttributes()
                .get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);

    if (info == null) {
      return;
    }
    if (info.getWorker() == null) {
      JsrWebSocketLogger.ROOT_LOGGER.xnioWorkerWasNull();
    }
    final List<ThreadSetupAction> setup = new ArrayList<ThreadSetupAction>();
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    final CompositeThreadSetupAction threadSetupAction = new CompositeThreadSetupAction(setup);
    ServerWebSocketContainer container =
        new ServerWebSocketContainer(
            deploymentInfo.getClassIntrospecter(),
            servletContext.getClassLoader(),
            info.getWorker(),
            info.getBuffers(),
            threadSetupAction,
            info.isDispatchToWorkerThread());
    try {
      for (Class<?> annotation : info.getAnnotatedEndpoints()) {
        container.addEndpoint(annotation);
      }
      for (ServerEndpointConfig programatic : info.getProgramaticEndpoints()) {
        container.addEndpoint(programatic);
      }
    } catch (DeploymentException e) {
      throw new RuntimeException(e);
    }
    servletContext.setAttribute(ServerContainer.class.getName(), container);
    info.containerReady(container);
    SecurityActions.addContainer(deploymentInfo.getClassLoader(), container);

    deploymentInfo.addListener(Servlets.listener(WebSocketListener.class));
  }