gpt4 book ai didi

org.cloudfoundry.identity.uaa.impl.config.YamlServletProfileInitializer类的使用及代码示例

转载 作者:知者 更新时间:2024-03-16 17:13:31 26 4
gpt4 key购买 nike

本文整理了Java中org.cloudfoundry.identity.uaa.impl.config.YamlServletProfileInitializer类的一些代码示例,展示了YamlServletProfileInitializer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YamlServletProfileInitializer类的具体详情如下:
包路径:org.cloudfoundry.identity.uaa.impl.config.YamlServletProfileInitializer
类名称:YamlServletProfileInitializer

YamlServletProfileInitializer介绍

[英]An ApplicationContextInitializer for a web application to enable it to externalize the environment and logging configuration. A YAML config file is loaded if present and inserted into the environment. In addition if the YAML contains some special properties, some initialization is carried out:

  • spring_profiles - then the active profiles are set
  • logging.config - then log4j is initialized from that location (if it exists)
    [中]web应用程序的ApplicationContextInitializer,使其能够外部化环境和日志配置。如果存在YAML配置文件,则会将其加载并插入到环境中。此外,如果YAML包含一些特殊属性,则会执行一些初始化:
    *spring_profiles-然后设置活动配置文件
    *logging.config-然后从该位置初始化log4j(如果存在)

代码示例

代码示例来源:origin: cloudfoundry/uaa

resources.addAll(getResource(servletContext, applicationContext, locations));
Resource yamlFromEnv = getYamlFromEnvironmentVariable();
if (yamlFromEnv!=null) {
  resources.add(yamlFromEnv);
  servletContext.log("No YAML environment properties from servlet.  Defaulting to servlet context.");
  locations = servletContext.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS);
  resources.addAll(getResource(servletContext, applicationContext, locations));
  NestedMapPropertySource properties = new NestedMapPropertySource("servletConfigYaml", map);
  applicationContext.getEnvironment().getPropertySources().addLast(properties);
  applySpringProfiles(applicationContext.getEnvironment(), servletContext);
  applyLog4jConfiguration(applicationContext.getEnvironment(), servletContext);

代码示例来源:origin: cloudfoundry/uaa

protected Resource getYamlFromEnvironmentVariable() {
  if (getEnvironmentAccessor()!=null){
    String data = getEnvironmentAccessor().getEnvironmentVariable(getYamlEnvironmentVariableName());
    if (hasText(data)) {
      //validate the Yaml? We don't do that for the others
      return new InMemoryResource(data);
    }
  }
  return null;
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testLog4jPathFromYaml() throws Exception {
  Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn(
          new ByteArrayResource("logging:\n  path: /tmp/log/bar".getBytes()));
  initializer.initialize(context);
  assertEquals("/tmp/log/bar", System.getProperty("LOG_PATH"));
}

代码示例来源:origin: cloudfoundry/uaa

public void initializeContext(ConfigurableWebApplicationContext context, String yamlPath) {
    MockServletContext servletContext = new MockServletContext() {
      @Override
      public <Type extends EventListener> void addListener(Type t) {
        //no op
      }
    };
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    servletConfig.addInitParameter("environmentConfigDefaults", yamlPath);
    context.setServletContext(servletContext);
    context.setServletConfig(servletConfig);
    new YamlServletProfileInitializer().initialize(context);
  }
}

代码示例来源:origin: cloudfoundry/uaa

public void testReadingYamlFromEnvironment(String variableName) throws Exception {
  if (hasText(variableName)) {
    initializer.setYamlEnvironmentVariableName(variableName);
  }
  SystemEnvironmentAccessor env = new SystemEnvironmentAccessor() {
    @Override
    public String getEnvironmentVariable(String name) {
      return name.equals(initializer.getYamlEnvironmentVariableName()) ?
        "uaa.url: http://uaa.test.url/\n" +
        "login.url: http://login.test.url/\n" +
        "smtp:\n" +
        "  host: mail.server.host\n" +
        "  port: 3535\n" :
        null;
    }
  };
  initializer.setEnvironmentAccessor(env);
  initializer.initialize(context);
  assertEquals("mail.server.host", environment.getProperty("smtp.host"));
  assertEquals("3535", environment.getProperty("smtp.port"));
  assertEquals("http://uaa.test.url/", environment.getProperty("uaa.url"));
  assertEquals("http://login.test.url/", environment.getProperty("login.url"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void if_no_profiles_are_set_use_hsqldb() {
  System.clearProperty("spring.profiles.active");
  initializer.applySpringProfiles(environment, context);
  assertArrayEquals(new String[] {"hsqldb"}, environment.getActiveProfiles());
}

代码示例来源:origin: cloudfoundry/uaa

@Override
  public String getEnvironmentVariable(String name) {
    return name.equals(initializer.getYamlEnvironmentVariableName()) ?
      "uaa.url: http://uaa.test.url/\n" +
      "login.url: http://login.test.url/\n" +
      "smtp:\n" +
      "  host: mail.server.host\n" +
      "  port: 3535\n" :
      null;
  }
};

代码示例来源:origin: cloudfoundry/uaa

@Before
public void setup() {
  initializer = new YamlServletProfileInitializer();
  environment = new MockEnvironment();
  context = new MockServletContext();
}

代码示例来源:origin: cloudfoundry/uaa

context.setServletConfig(servletConfig);
YamlServletProfileInitializer initializer = new YamlServletProfileInitializer();
initializer.initialize(context);

代码示例来源:origin: cloudfoundry/uaa

@Test
public void if_profiles_are_set_use_them() {
  System.setProperty("spring.profiles.active", "hsqldb,default");
  initializer.applySpringProfiles(environment, context);
  assertArrayEquals(new String[] {"hsqldb", "default"}, environment.getActiveProfiles());
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testLog4jConfigurationFromYaml() throws Exception {
  Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn(
    new ByteArrayResource("logging:\n  config: bar".getBytes()));
  initializer.initialize(context);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
  public void yaml_configured_profiles_are_used() {
    System.setProperty("spring.profiles.active", "hsqldb,default");
    environment.setProperty("spring_profiles", "mysql,default");
    initializer.applySpringProfiles(environment, context);
    assertArrayEquals(new String[] {"mysql", "default"}, environment.getActiveProfiles());
  }
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testLog4jFileFromYaml() throws Exception {
  Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn(
          new ByteArrayResource("logging:\n  file: /tmp/bar.log".getBytes()));
  initializer.initialize(context);
  assertEquals("/tmp/bar.log", System.getProperty("LOG_FILE"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void default_profile_unset() {
  System.setProperty("spring.profiles.active", "hsqldb");
  initializer.applySpringProfiles(environment, context);
  assertArrayEquals(new String[] {"hsqldb"}, environment.getActiveProfiles());
  assertArrayEquals(new String[0], environment.getDefaultProfiles());
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testActiveProfiles() throws Exception {
  System.setProperty("spring.profiles.active", "foo");
  Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn(
    new ByteArrayResource("spring_profiles: bar".getBytes()));
  initializer.initialize(context);
  assertEquals("bar", environment.getActiveProfiles()[0]);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testLoadSessionEventPublisher() throws Exception {
  Mockito.when(context.getResource(ArgumentMatchers.contains("${APPLICATION_CONFIG_URL}"))).thenReturn(
    new ByteArrayResource("foo: bar\nspam:\n  foo: baz".getBytes()));
  initializer.initialize(context);
  ArgumentCaptor<HttpSessionEventPublisher> httpSessionEventPublisherArgumentCaptor = ArgumentCaptor.forClass(HttpSessionEventPublisher.class);
  verify(servletContext, atLeastOnce()).addListener(httpSessionEventPublisherArgumentCaptor.capture());
  assertNotNull(httpSessionEventPublisherArgumentCaptor.getValue());
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testActiveProfilesFromYaml() throws Exception {
  Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn(
          new ByteArrayResource("spring_profiles: bar".getBytes()));
  initializer.initialize(context);
  assertEquals("bar", environment.getActiveProfiles()[0]);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testLoadServletConfiguredResource() throws Exception {
  Mockito.when(servletConfig.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml");
  Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn(
          new ByteArrayResource("foo: bar\nspam:\n  foo: baz".getBytes()));
  initializer.initialize(context);
  assertEquals("bar", environment.getProperty("foo"));
  assertEquals("baz", environment.getProperty("spam.foo"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testLoadServletConfiguredFilename() throws Exception {
  Mockito.when(servletConfig.getInitParameter("APPLICATION_CONFIG_FILE")).thenReturn("/config/path/foo.yml");
  Mockito.when(context.getResource(ArgumentMatchers.eq("file:/config/path/foo.yml"))).thenReturn(
          new ByteArrayResource("foo: bar\nspam:\n  foo: baz".getBytes()));
  initializer.initialize(context);
  assertEquals("bar", environment.getProperty("foo"));
  assertEquals("baz", environment.getProperty("spam.foo"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testLoadContextConfiguredResource() throws Exception {
  Mockito.when(servletContext.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml");
  Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn(
          new ByteArrayResource("foo: bar\nspam:\n  foo: baz".getBytes()));
  initializer.initialize(context);
  assertEquals("bar", environment.getProperty("foo"));
  assertEquals("baz", environment.getProperty("spam.foo"));
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com