Java 类org.springframework.boot.context.embedded.EmbeddedWebApplicationContext 实例源码

项目:simple-hostel-management    文件:Utils.java   
/**
 * /!\ Work only if application run on embedded Tomcat server
 *
 * @return
 * @throws Exception
 */
public static String getBaseUrlForEmbeddedTomcat() throws Exception {

    // get embedded tomcat
    EmbeddedWebApplicationContext appContext = (EmbeddedWebApplicationContext) new ApplicationContextProvider().getApplicationContext();
    Tomcat tomcat = ((TomcatEmbeddedServletContainer) appContext.getEmbeddedServletContainer()).getTomcat();
    Connector connector = tomcat.getConnector();

    // compose address
    String scheme = connector.getScheme();
    String hostName = tomcat.getHost().getName();
    int port = connector.getPort();
    String contextPath = appContext.getServletContext().getContextPath();

    return scheme + "://" + hostName + ":" + port + contextPath;
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:EmbeddedServerPortFileWriter.java   
/**
 * Return the actual port file that should be written for the given application
 * context. The default implementation builds a file from the source file and the
 * application context namespace.
 * @param applicationContext the source application context
 * @return the file that should be written
 */
protected File getPortFile(EmbeddedWebApplicationContext applicationContext) {
    String contextName = applicationContext.getNamespace();
    if (StringUtils.isEmpty(contextName)) {
        return this.file;
    }
    String name = this.file.getName();
    String extension = StringUtils.getFilenameExtension(this.file.getName());
    name = name.substring(0, name.length() - extension.length() - 1);
    if (isUpperCase(name)) {
        name = name + "-" + contextName.toUpperCase();
    }
    else {
        name = name + "-" + contextName.toLowerCase();
    }
    if (StringUtils.hasLength(extension)) {
        name = name + "." + extension;
    }
    return new File(this.file.getParentFile(), name);
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:SpringApplicationAdminJmxAutoConfigurationTests.java   
@Test
public void registerWithSimpleWebApp() throws Exception {
    this.context = new SpringApplicationBuilder()
            .sources(EmbeddedServletContainerAutoConfiguration.class,
                    ServerPropertiesAutoConfiguration.class,
                    DispatcherServletAutoConfiguration.class,
                    JmxAutoConfiguration.class,
                    SpringApplicationAdminJmxAutoConfiguration.class)
            .run("--" + ENABLE_ADMIN_PROP, "--server.port=0");
    assertThat(this.context).isInstanceOf(EmbeddedWebApplicationContext.class);
    assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
            "EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
    int expected = ((EmbeddedWebApplicationContext) this.context)
            .getEmbeddedServletContainer().getPort();
    String actual = getProperty(createDefaultObjectName(), "local.server.port");
    assertThat(actual).isEqualTo(String.valueOf(expected));
}
项目:spring-boot-concourse    文件:EndpointWebMvcAutoConfiguration.java   
@Override
public void afterSingletonsInstantiated() {
    ManagementServerPort managementPort = ManagementServerPort.DIFFERENT;
    if (this.applicationContext instanceof WebApplicationContext) {
        managementPort = ManagementServerPort
                .get(this.applicationContext.getEnvironment(), this.beanFactory);
    }
    if (managementPort == ManagementServerPort.DIFFERENT) {
        if (this.applicationContext instanceof EmbeddedWebApplicationContext
                && ((EmbeddedWebApplicationContext) this.applicationContext)
                        .getEmbeddedServletContainer() != null) {
            createChildManagementContext();
        }
        else {
            logger.warn("Could not start embedded management container on "
                    + "different port (management endpoints are still available "
                    + "through JMX)");
        }
    }
    if (managementPort == ManagementServerPort.SAME && this.applicationContext
            .getEnvironment() instanceof ConfigurableEnvironment) {
        addLocalManagementPortPropertyAlias(
                (ConfigurableEnvironment) this.applicationContext.getEnvironment());
    }
}
项目:spring-boot-concourse    文件:EmbeddedServerPortFileWriter.java   
/**
 * Return the actual port file that should be written for the given application
 * context. The default implementation builds a file from the source file and the
 * application context namespace.
 * @param applicationContext the source application context
 * @return the file that should be written
 */
protected File getPortFile(EmbeddedWebApplicationContext applicationContext) {
    String contextName = applicationContext.getNamespace();
    if (StringUtils.isEmpty(contextName)) {
        return this.file;
    }
    String name = this.file.getName();
    String extension = StringUtils.getFilenameExtension(this.file.getName());
    name = name.substring(0, name.length() - extension.length() - 1);
    if (isUpperCase(name)) {
        name = name + "-" + contextName.toUpperCase();
    }
    else {
        name = name + "-" + contextName.toLowerCase();
    }
    if (StringUtils.hasLength(extension)) {
        name = name + "." + extension;
    }
    return new File(this.file.getParentFile(), name);
}
项目:spring-boot-concourse    文件:SpringApplicationAdminJmxAutoConfigurationTests.java   
@Test
public void registerWithSimpleWebApp() throws Exception {
    this.context = new SpringApplicationBuilder()
            .sources(EmbeddedServletContainerAutoConfiguration.class,
                    ServerPropertiesAutoConfiguration.class,
                    DispatcherServletAutoConfiguration.class,
                    JmxAutoConfiguration.class,
                    SpringApplicationAdminJmxAutoConfiguration.class)
            .run("--" + ENABLE_ADMIN_PROP, "--server.port=0");
    assertThat(this.context).isInstanceOf(EmbeddedWebApplicationContext.class);
    assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
            "EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
    int expected = ((EmbeddedWebApplicationContext) this.context)
            .getEmbeddedServletContainer().getPort();
    String actual = getProperty(createDefaultObjectName(), "local.server.port");
    assertThat(actual).isEqualTo(String.valueOf(expected));
}
项目:Kafdrop    文件:ServiceDiscoveryConfiguration.java   
private Integer getServicePort(EmbeddedWebApplicationContext webContext)
{
   Integer port = null;
   if (webContext != null)
   {
      final EmbeddedServletContainer container = webContext.getEmbeddedServletContainer();

      if (container instanceof TomcatEmbeddedServletContainer)
      {
         // Work around an issue with the tomcat container, which uses the local port
         // as the port (-1) instead of the registered port
         port = ((TomcatEmbeddedServletContainer) container).getTomcat().getConnector().getPort();
      }
      else
      {
         port = container.getPort();
      }
   }
   return port;
}
项目:contestparser    文件:EndpointWebMvcAutoConfiguration.java   
@Override
public void afterSingletonsInstantiated() {
    ManagementServerPort managementPort = ManagementServerPort.DIFFERENT;
    if (this.applicationContext instanceof WebApplicationContext) {
        managementPort = ManagementServerPort
                .get(this.applicationContext.getEnvironment(), this.beanFactory);
    }
    if (managementPort == ManagementServerPort.DIFFERENT) {
        if (this.applicationContext instanceof EmbeddedWebApplicationContext
                && ((EmbeddedWebApplicationContext) this.applicationContext)
                        .getEmbeddedServletContainer() != null) {
            createChildManagementContext();
        }
        else {
            logger.warn("Could not start embedded management container on "
                    + "different port (management endpoints are still available "
                    + "through JMX)");
        }
    }
    if (managementPort == ManagementServerPort.SAME && this.applicationContext
            .getEnvironment() instanceof ConfigurableEnvironment) {
        addLocalManagementPortPropertyAlias(
                (ConfigurableEnvironment) this.applicationContext.getEnvironment());
    }
}
项目:contestparser    文件:EmbeddedServerPortFileWriter.java   
/**
 * Return the actual port file that should be written for the given application
 * context. The default implementation builds a file from the source file and the
 * application context namespace.
 * @param applicationContext the source application context
 * @return the file that should be written
 */
protected File getPortFile(EmbeddedWebApplicationContext applicationContext) {
    String contextName = applicationContext.getNamespace();
    if (StringUtils.isEmpty(contextName)) {
        return this.file;
    }
    String name = this.file.getName();
    String extension = StringUtils.getFilenameExtension(this.file.getName());
    name = name.substring(0, name.length() - extension.length() - 1);
    if (isUpperCase(name)) {
        name = name + "-" + contextName.toUpperCase();
    }
    else {
        name = name + "-" + contextName.toLowerCase();
    }
    if (StringUtils.hasLength(extension)) {
        name = name + "." + extension;
    }
    return new File(this.file.getParentFile(), name);
}
项目:contestparser    文件:SpringApplicationAdminJmxAutoConfigurationTests.java   
@Test
public void registerWithSimpleWebApp() throws Exception {
    this.context = new SpringApplicationBuilder()
            .sources(EmbeddedServletContainerAutoConfiguration.class,
                    ServerPropertiesAutoConfiguration.class,
                    DispatcherServletAutoConfiguration.class,
                    JmxAutoConfiguration.class,
                    SpringApplicationAdminJmxAutoConfiguration.class)
            .run("--" + ENABLE_ADMIN_PROP, "--server.port=0");
    assertTrue(this.context instanceof EmbeddedWebApplicationContext);
    assertEquals(true, this.mBeanServer.getAttribute(createDefaultObjectName(),
            "EmbeddedWebApplication"));
    int expected = ((EmbeddedWebApplicationContext) this.context)
            .getEmbeddedServletContainer().getPort();
    String actual = getProperty(createDefaultObjectName(), "local.server.port");
    assertEquals(String.valueOf(expected), actual);
}
项目:mywx    文件:Bootstrap.java   
/**
 * spring boot 服务主入口
 *
 * @param args
 */
public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(Bootstrap.class, args);
    if (context instanceof EmbeddedWebApplicationContext) {
        int port = ((EmbeddedWebApplicationContext) context).getEmbeddedServletContainer().getPort();
        String contextPath = context.getApplicationName();
        String url = String.format(Locale.US, "http://localhost:%d%s", port, contextPath);
        if (log.isInfoEnabled()) {
            //提示项目用到的相关配置文件
            log.info(" =========== ${user.dir}={} ===========  ", System.getProperty("user.dir"));
            log.info(" =========== ${java.io.tmpdir}={} ===========  ", System.getProperty("java.io.tmpdir"));

            String dashes = "------------------------------------------------------------------------";
            log.info("Access URLs:\n{}\n\tLocal: \t\t{}\n{}", dashes, url, dashes);
        }
    }
}
项目:micrometer    文件:TomcatMetricsConfiguration.java   
@Bean
public TomcatMetrics metrics(ApplicationContext applicationContext) {
    Manager manager = null;
    if (applicationContext instanceof EmbeddedWebApplicationContext) {
        manager = getManagerFromContext((EmbeddedWebApplicationContext) applicationContext);
    }
    return new TomcatMetrics(manager, Collections.emptyList());
}
项目:micrometer    文件:TomcatMetricsConfiguration.java   
private Manager getManagerFromContext(EmbeddedWebApplicationContext applicationContext) {
    EmbeddedServletContainer embeddedServletContainer = applicationContext.getEmbeddedServletContainer();
    if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) {
        return getManagerFromContainer((TomcatEmbeddedServletContainer) embeddedServletContainer);
    }
    return null;
}
项目:judge    文件:Application.java   
public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(Application.class, args);
    if (log.isInfoEnabled() && context instanceof EmbeddedWebApplicationContext) {
        int port = ((EmbeddedWebApplicationContext) context).getEmbeddedServletContainer().getPort();
        String contextPath = context.getApplicationName();
        final String dashes = Strings.repeat("-", 72);
        log.info("Access URLs:\n{}\n\tLocal:\t\thttp://localhost:{}{}\n{}", dashes, port, contextPath, dashes);
    }
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:EndpointWebMvcAutoConfiguration.java   
@Override
public void afterSingletonsInstantiated() {
    ManagementServerPort managementPort = ManagementServerPort.DIFFERENT;
    if (this.applicationContext instanceof WebApplicationContext) {
        managementPort = ManagementServerPort
                .get(this.applicationContext.getEnvironment(), this.beanFactory);
    }
    if (managementPort == ManagementServerPort.DIFFERENT) {
        if (this.applicationContext instanceof EmbeddedWebApplicationContext
                && ((EmbeddedWebApplicationContext) this.applicationContext)
                        .getEmbeddedServletContainer() != null) {
            createChildManagementContext();
        }
        else {
            logger.warn("Could not start embedded management container on "
                    + "different port (management endpoints are still available "
                    + "through JMX)");
        }
    }
    if (managementPort == ManagementServerPort.SAME) {
        if (new RelaxedPropertyResolver(this.applicationContext.getEnvironment(),
                "management.ssl.").getProperty("enabled") != null) {
            throw new IllegalStateException(
                    "Management-specific SSL cannot be configured as the management "
                            + "server is not listening on a separate port");
        }
        if (this.applicationContext
                .getEnvironment() instanceof ConfigurableEnvironment) {
            addLocalManagementPortPropertyAlias(
                    (ConfigurableEnvironment) this.applicationContext
                            .getEnvironment());
        }
    }
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:TomcatPublicMetrics.java   
@Override
public Collection<Metric<?>> metrics() {
    if (this.applicationContext instanceof EmbeddedWebApplicationContext) {
        Manager manager = getManager(
                (EmbeddedWebApplicationContext) this.applicationContext);
        if (manager != null) {
            return metrics(manager);
        }
    }
    return Collections.emptySet();
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:TomcatPublicMetrics.java   
private Manager getManager(EmbeddedWebApplicationContext applicationContext) {
    EmbeddedServletContainer embeddedServletContainer = applicationContext
            .getEmbeddedServletContainer();
    if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) {
        return getManager((TomcatEmbeddedServletContainer) embeddedServletContainer);
    }
    return null;
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:EmbeddedServerPortFileWriterTests.java   
private EmbeddedServletContainerInitializedEvent mockEvent(String name, int port) {
    EmbeddedWebApplicationContext applicationContext = mock(
            EmbeddedWebApplicationContext.class);
    EmbeddedServletContainer source = mock(EmbeddedServletContainer.class);
    given(applicationContext.getNamespace()).willReturn(name);
    given(source.getPort()).willReturn(port);
    EmbeddedServletContainerInitializedEvent event = new EmbeddedServletContainerInitializedEvent(
            applicationContext, source);
    return event;
}
项目:spring-boot-concourse    文件:TomcatPublicMetrics.java   
@Override
public Collection<Metric<?>> metrics() {
    if (this.applicationContext instanceof EmbeddedWebApplicationContext) {
        Manager manager = getManager(
                (EmbeddedWebApplicationContext) this.applicationContext);
        if (manager != null) {
            return metrics(manager);
        }
    }
    return Collections.emptySet();
}
项目:spring-boot-concourse    文件:TomcatPublicMetrics.java   
private Manager getManager(EmbeddedWebApplicationContext applicationContext) {
    EmbeddedServletContainer embeddedServletContainer = applicationContext
            .getEmbeddedServletContainer();
    if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) {
        return getManager((TomcatEmbeddedServletContainer) embeddedServletContainer);
    }
    return null;
}
项目:spring-boot-concourse    文件:EmbeddedServerPortFileWriterTests.java   
private EmbeddedServletContainerInitializedEvent mockEvent(String name, int port) {
    EmbeddedWebApplicationContext applicationContext = mock(
            EmbeddedWebApplicationContext.class);
    EmbeddedServletContainer source = mock(EmbeddedServletContainer.class);
    given(applicationContext.getNamespace()).willReturn(name);
    given(source.getPort()).willReturn(port);
    EmbeddedServletContainerInitializedEvent event = new EmbeddedServletContainerInitializedEvent(
            applicationContext, source);
    return event;
}
项目:contestparser    文件:ServerPortInfoApplicationContextInitializer.java   
protected String getPropertyName(EmbeddedWebApplicationContext context) {
    String name = context.getNamespace();
    if (StringUtils.isEmpty(name)) {
        name = "server";
    }
    return "local." + name + ".port";
}
项目:contestparser    文件:TomcatPublicMetrics.java   
@Override
public Collection<Metric<?>> metrics() {
    if (this.applicationContext instanceof EmbeddedWebApplicationContext) {
        Manager manager = getManager(
                (EmbeddedWebApplicationContext) this.applicationContext);
        if (manager != null) {
            return metrics(manager);
        }
    }
    return Collections.emptySet();
}
项目:contestparser    文件:TomcatPublicMetrics.java   
private Manager getManager(EmbeddedWebApplicationContext applicationContext) {
    EmbeddedServletContainer embeddedServletContainer = applicationContext
            .getEmbeddedServletContainer();
    if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) {
        return getManager((TomcatEmbeddedServletContainer) embeddedServletContainer);
    }
    return null;
}
项目:contestparser    文件:EmbeddedServerPortFileWriterTests.java   
private EmbeddedServletContainerInitializedEvent mockEvent(String name, int port) {
    EmbeddedWebApplicationContext applicationContext = mock(
            EmbeddedWebApplicationContext.class);
    EmbeddedServletContainer source = mock(EmbeddedServletContainer.class);
    given(applicationContext.getNamespace()).willReturn(name);
    given(source.getPort()).willReturn(port);
    EmbeddedServletContainerInitializedEvent event = new EmbeddedServletContainerInitializedEvent(
            applicationContext, source);
    return event;
}
项目:minicubes    文件:DiscardListener.java   
@Override
public void finished(ConfigurableApplicationContext context,
        Throwable exception) {

    applicationContext = (EmbeddedWebApplicationContext) context;
    cdl.countDown();
}
项目:pebble-spring-boot-starter    文件:ServletComponenRegisteringPostProcessor.java   
private boolean isRunningInEmbeddedContainer() {
    return this.applicationContext instanceof EmbeddedWebApplicationContext
            && ((EmbeddedWebApplicationContext) this.applicationContext).getServletContext() == null;
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:SpringApplicationAdminMXBeanRegistrar.java   
@Override
public boolean isEmbeddedWebApplication() {
    return (SpringApplicationAdminMXBeanRegistrar.this.applicationContext != null
            && SpringApplicationAdminMXBeanRegistrar.this.applicationContext instanceof EmbeddedWebApplicationContext);
}
项目:spring-boot-concourse    文件:SpringApplicationAdminMXBeanRegistrar.java   
@Override
public boolean isEmbeddedWebApplication() {
    return (SpringApplicationAdminMXBeanRegistrar.this.applicationContext != null
            && SpringApplicationAdminMXBeanRegistrar.this.applicationContext instanceof EmbeddedWebApplicationContext);
}
项目:contestparser    文件:ServletComponentRegisteringPostProcessor.java   
private boolean isRunningInEmbeddedContainer() {
    return this.applicationContext instanceof EmbeddedWebApplicationContext
            && ((EmbeddedWebApplicationContext) this.applicationContext)
                    .getServletContext() == null;
}
项目:contestparser    文件:SpringApplicationAdminMXBeanRegistrar.java   
@Override
public boolean isEmbeddedWebApplication() {
    return (SpringApplicationAdminMXBeanRegistrar.this.applicationContext != null
            && SpringApplicationAdminMXBeanRegistrar.this.applicationContext instanceof EmbeddedWebApplicationContext);
}