org.springframework.boot.context.embedded.EmbeddedServletContainer Java Examples

The following examples show how to use org.springframework.boot.context.embedded.EmbeddedServletContainer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DiscoveryClientRegistrationInvoker.java    From Moss with Apache License 2.0 6 votes vote down vote up
@Override
public void customize(ConfigurableApplicationContext context) {
    if (context instanceof EmbeddedWebApplicationContext
            && !AdminEndpointApplicationRunListener.isEmbeddedServletServer(context.getEnvironment())) {
        MetaDataProvider metaDataProvider = context.getBean(MetaDataProvider.class);
        EmbeddedServletContainer embeddedServletContainer = new EmbeddedServletContainer() {

            @Override
            public void start() throws EmbeddedServletContainerException {

            }

            @Override
            public void stop() throws EmbeddedServletContainerException {

            }

            @Override
            public int getPort() {
                return metaDataProvider.getServerPort();
            }
        };
        context.publishEvent(new EmbeddedServletContainerInitializedEvent((EmbeddedWebApplicationContext) context, embeddedServletContainer));
    }
}
 
Example #2
Source File: NettyEmbeddedServletContainerFactory.java    From Jinx with Apache License 2.0 6 votes vote down vote up
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
    ClassLoader parentClassLoader = resourceLoader != null ? resourceLoader.getClassLoader() : ClassUtils.getDefaultClassLoader();
    Package nettyPackage = Bootstrap.class.getPackage();
    String title = nettyPackage.getImplementationTitle();
    String version = nettyPackage.getImplementationVersion();
    logger.info("Running with " + title + " " + version);
    NettyEmbeddedContext context = new NettyEmbeddedContext(nettyContainerConfig,getContextPath(),
            new URLClassLoader(new URL[]{}, parentClassLoader),
            SERVER_INFO);
    if (isRegisterDefaultServlet()) {
        logger.warn("This container does not support a default servlet");
    }
    for (ServletContextInitializer initializer : initializers) {
        try {
            initializer.onStartup(context);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }
    logger.info("nettyContainerConfig :"+nettyContainerConfig.toString());
    return new NettyEmbeddedServletContainer(nettyContainerConfig, context);
}
 
Example #3
Source File: NettyEmbeddedServletContainerFactory.java    From spring-boot-starter-netty with Apache License 2.0 6 votes vote down vote up
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
    ClassLoader parentClassLoader = resourceLoader != null ? resourceLoader.getClassLoader() : ClassUtils.getDefaultClassLoader();
    Package nettyPackage = Bootstrap.class.getPackage();
    String title = nettyPackage.getImplementationTitle();
    String version = nettyPackage.getImplementationVersion();
    logger.info("Running with " + title + " " + version);
    NettyEmbeddedContext context = new NettyEmbeddedContext(getContextPath(), new URLClassLoader(new URL[]{}, parentClassLoader), SERVER_INFO);
    if (isRegisterDefaultServlet()) {
        logger.warn("This container does not support a default servlet");
    }
    if (isRegisterJspServlet()) {
        logger.warn("This container does not support a JSP servlet");
    }
    for (ServletContextInitializer initializer : initializers) {
        try {
            initializer.onStartup(context);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }
    int port = getPort() > 0 ? getPort() : new Random().nextInt(65535 - 1024) + 1024;
    InetSocketAddress address = new InetSocketAddress(port);
    logger.info("Server initialized with port: " + port);
    return new NettyEmbeddedServletContainer(address, context);
}
 
Example #4
Source File: TomcatMetricsBinder.java    From foremast with Apache License 2.0 5 votes vote down vote up
private Manager findManager(ApplicationContext applicationContext) {
    if (applicationContext instanceof EmbeddedWebApplicationContext) {
        EmbeddedServletContainer container = ((EmbeddedWebApplicationContext) applicationContext).getEmbeddedServletContainer();
        if (container instanceof TomcatEmbeddedServletContainer) {
            Context context = findContext((TomcatEmbeddedServletContainer) container);
            if (context != null) {
                return context.getManager();
            }
        }
    }
    return null;
}
 
Example #5
Source File: EmbeddedNettyFactory.java    From spring-boot-starter-netty with GNU General Public License v3.0 5 votes vote down vote up
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
    ClassLoader parentClassLoader = resourceLoader != null ? resourceLoader.getClassLoader() : ClassUtils.getDefaultClassLoader();
    //Netty启动环境相关信息
    Package nettyPackage = Bootstrap.class.getPackage();
    String title = nettyPackage.getImplementationTitle();
    String version = nettyPackage.getImplementationVersion();
    log.info("Running with " + title + " " + version);
    //是否支持默认Servlet
    if (isRegisterDefaultServlet()) {
        log.warn("This container does not support a default servlet");
    }
    //上下文
    NettyContext context = new NettyContext(getContextPath(), new URLClassLoader(new URL[]{}, parentClassLoader), SERVER_INFO);
    for (ServletContextInitializer initializer : initializers) {
        try {
            initializer.onStartup(context);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }
    //从SpringBoot配置中获取端口,如果没有则随机生成
    int port = getPort() > 0 ? getPort() : new Random().nextInt(65535 - 1024) + 1024;
    InetSocketAddress address = new InetSocketAddress(port);
    log.info("Server initialized with port: " + port);
    return new NettyContainer(address, context); //初始化容器并返回
}
 
Example #6
Source File: ServerlessServletEmbeddedServerFactoryTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void getContainer_populatesInitializers() {
    ServerlessServletEmbeddedServerFactory factory = new ServerlessServletEmbeddedServerFactory();
    TestServlet initializer = new TestServlet(false);
    EmbeddedServletContainer container = factory.getEmbeddedServletContainer(initializer);
    assertNotNull(((ServerlessServletEmbeddedServerFactory)container).getInitializers());
    assertEquals(1, ((ServerlessServletEmbeddedServerFactory)container).getInitializers().length);
    assertEquals(initializer, ((ServerlessServletEmbeddedServerFactory)container).getInitializers()[0]);
    container.stop(); // calling stop just once to get the test coverage since there's no code in it
}
 
Example #7
Source File: ServerlessServletEmbeddedServerFactoryTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void start_throwsException() {
    ServerlessServletEmbeddedServerFactory factory = new ServerlessServletEmbeddedServerFactory();
    TestServlet initializer = new TestServlet(true);
    EmbeddedServletContainer container = factory.getEmbeddedServletContainer(initializer);
    try {
        container.start();
    } catch (EmbeddedServletContainerException e) {
        assertTrue(ServletException.class.isAssignableFrom(e.getCause().getClass()));
        assertEquals(TestServlet.EXCEPTION_MESSAGE, e.getCause().getMessage());
        return;
    }
    fail("Did not throw the expected exception");
}
 
Example #8
Source File: ServerlessServletEmbeddedServerFactoryTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void start_withoutException_setsServletContext() {
    ServerlessServletEmbeddedServerFactory factory = new ServerlessServletEmbeddedServerFactory();
    TestServlet initializer = new TestServlet(false);
    EmbeddedServletContainer container = factory.getEmbeddedServletContainer(initializer);
    container.start();
    assertNotNull(initializer.getCtx());
    assertEquals(h.getServletContext(), initializer.getCtx());
}
 
Example #9
Source File: MockServer.java    From funcatron with Apache License 2.0 5 votes vote down vote up
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
        ServletContextInitializer... initializers) {
    this.container = new MockEmbeddedServletContainer(
            mergeInitializers(initializers), getPort());
    return this.container;
}
 
Example #10
Source File: ServerlessServletEmbeddedServerFactory.java    From aws-serverless-java-container with Apache License 2.0 4 votes vote down vote up
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... servletContextInitializers) {
    initializers = servletContextInitializers;

    return this;
}