Search in sources :

Example 51 with Environment

use of org.springframework.core.env.Environment in project cas by apereo.

the class DefaultCasConfigurationPropertiesSourceLocator method loadSettingsByApplicationProfiles.

/**
 * Property files processed in order of non-profiles first and then profiles, and profiles are first to last
 * with properties in the last profile overriding properties in previous profiles or non-profiles.
 *
 * @param environment Spring environment
 * @param config      Location of config files
 * @return Merged properties
 */
private PropertySource<?> loadSettingsByApplicationProfiles(final Environment environment, final File config) {
    val profiles = ConfigurationPropertiesLoaderFactory.getApplicationProfiles(environment);
    val resources = scanForConfigurationResources(environment, config, profiles);
    val composite = new CompositePropertySource("applicationProfilesCompositeProperties");
    LOGGER.info("Configuration files found at [{}] are [{}] under profile(s) [{}]", config, resources, profiles);
    resources.forEach(Unchecked.consumer(f -> {
        LOGGER.debug("Loading configuration file [{}]", f);
        val loader = configurationPropertiesLoaderFactory.getLoader(f, "applicationProfilesProperties-" + f.getFilename());
        composite.addFirstPropertySource(loader.load());
    }));
    return composite;
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Unchecked(org.jooq.lambda.Unchecked) ResourceLoader(org.springframework.core.io.ResourceLoader) PropertySource(org.springframework.core.env.PropertySource) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) FileSystemResource(org.springframework.core.io.FileSystemResource) Collectors(java.util.stream.Collectors) ConfigurationPropertiesLoaderFactory(org.apereo.cas.configuration.loader.ConfigurationPropertiesLoaderFactory) File(java.io.File) CompositePropertySource(org.springframework.core.env.CompositePropertySource) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Environment(org.springframework.core.env.Environment) CollectionUtils(org.apereo.cas.util.CollectionUtils) Optional(java.util.Optional) CasConfigurationPropertiesSourceLocator(org.apereo.cas.configuration.api.CasConfigurationPropertiesSourceLocator) Resource(org.springframework.core.io.Resource) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Example 52 with Environment

use of org.springframework.core.env.Environment in project cas by apereo.

the class DefaultCasConfigurationPropertiesSourceLocator method loadEmbeddedYamlOverriddenProperties.

private PropertySource<?> loadEmbeddedYamlOverriddenProperties(final ResourceLoader resourceLoader, final Environment environment) {
    val profiles = ConfigurationPropertiesLoaderFactory.getApplicationProfiles(environment);
    val yamlFiles = profiles.stream().map(profile -> String.format("classpath:/application-%s.yml", profile)).sorted().map(resourceLoader::getResource).collect(Collectors.toList());
    yamlFiles.add(resourceLoader.getResource("classpath:/application.yml"));
    LOGGER.debug("Loading embedded YAML configuration files [{}]", yamlFiles);
    val composite = new CompositePropertySource("embeddedYamlOverriddenCompositeProperties");
    yamlFiles.forEach(resource -> {
        LOGGER.trace("Loading YAML properties from [{}]", resource);
        val source = configurationPropertiesLoaderFactory.getLoader(resource, String.format("embeddedYamlOverriddenProperties-%s", resource.getFilename())).load();
        composite.addPropertySource(source);
    });
    return composite;
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Unchecked(org.jooq.lambda.Unchecked) ResourceLoader(org.springframework.core.io.ResourceLoader) PropertySource(org.springframework.core.env.PropertySource) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) FileSystemResource(org.springframework.core.io.FileSystemResource) Collectors(java.util.stream.Collectors) ConfigurationPropertiesLoaderFactory(org.apereo.cas.configuration.loader.ConfigurationPropertiesLoaderFactory) File(java.io.File) CompositePropertySource(org.springframework.core.env.CompositePropertySource) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Environment(org.springframework.core.env.Environment) CollectionUtils(org.apereo.cas.util.CollectionUtils) Optional(java.util.Optional) CasConfigurationPropertiesSourceLocator(org.apereo.cas.configuration.api.CasConfigurationPropertiesSourceLocator) Resource(org.springframework.core.io.Resource) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Example 53 with Environment

use of org.springframework.core.env.Environment in project canal by alibaba.

the class CanalAdapterLoader method loadAdapter.

private void loadAdapter(OuterAdapterConfig config, List<OuterAdapter> canalOutConnectors) {
    try {
        OuterAdapter adapter;
        adapter = loader.getExtension(config.getName(), StringUtils.trimToEmpty(config.getKey()));
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        // 替换ClassLoader
        Thread.currentThread().setContextClassLoader(adapter.getClass().getClassLoader());
        Environment env = (Environment) SpringContext.getBean(Environment.class);
        Properties evnProperties = null;
        if (env instanceof StandardEnvironment) {
            evnProperties = new Properties();
            for (PropertySource<?> propertySource : ((StandardEnvironment) env).getPropertySources()) {
                if (propertySource instanceof EnumerablePropertySource) {
                    String[] names = ((EnumerablePropertySource<?>) propertySource).getPropertyNames();
                    for (String name : names) {
                        Object val = env.getProperty(name);
                        if (val != null) {
                            evnProperties.put(name, val);
                        }
                    }
                }
            }
        }
        adapter.init(config, evnProperties);
        Thread.currentThread().setContextClassLoader(cl);
        canalOutConnectors.add(adapter);
        logger.info("Load canal adapter: {} succeed", config.getName());
    } catch (Exception e) {
        logger.error("Load canal adapter: {} failed", config.getName(), e);
    }
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.core.env.Environment) Properties(java.util.Properties) OuterAdapter(com.alibaba.otter.canal.client.adapter.OuterAdapter) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Example 54 with Environment

use of org.springframework.core.env.Environment in project Gaffer by gchq.

the class SwaggerConfigTest method shouldPullTitleFromEnvironment.

@Test
public void shouldPullTitleFromEnvironment() {
    // Given
    final String title = "My Gaffer Graph";
    SwaggerConfig swaggerConfig = new SwaggerConfig();
    Environment env = mock(Environment.class);
    when(env.getProperty(APP_TITLE, APP_TITLE_DEFAULT)).thenReturn(title);
    // When
    swaggerConfig.setEnvironment(env);
    ApiInfo apiInfo = swaggerConfig.apiInfo();
    // Then
    assertEquals(title, apiInfo.getTitle());
}
Also used : ApiInfo(springfox.documentation.service.ApiInfo) Environment(org.springframework.core.env.Environment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 55 with Environment

use of org.springframework.core.env.Environment in project Gaffer by gchq.

the class SwaggerConfigTest method shouldPullDescriptionFromEnvironment.

@Test
public void shouldPullDescriptionFromEnvironment() {
    // Given
    final String description = "My Gaffer Graph";
    SwaggerConfig swaggerConfig = new SwaggerConfig();
    Environment env = mock(Environment.class);
    when(env.getProperty(APP_DESCRIPTION, APP_DESCRIPTION_DEFAULT)).thenReturn(description);
    // When
    swaggerConfig.setEnvironment(env);
    ApiInfo apiInfo = swaggerConfig.apiInfo();
    // Then
    assertEquals(description, apiInfo.getDescription());
}
Also used : ApiInfo(springfox.documentation.service.ApiInfo) Environment(org.springframework.core.env.Environment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

Environment (org.springframework.core.env.Environment)161 Test (org.junit.jupiter.api.Test)68 StandardEnvironment (org.springframework.core.env.StandardEnvironment)30 EnvironmentVariableUtility (com.synopsys.integration.alert.environment.EnvironmentVariableUtility)26 MockEnvironment (org.springframework.mock.env.MockEnvironment)24 SpringApplication (org.springframework.boot.SpringApplication)21 EnvironmentProcessingResult (com.synopsys.integration.alert.environment.EnvironmentProcessingResult)19 EnvironmentVariableHandler (com.synopsys.integration.alert.environment.EnvironmentVariableHandler)19 EnvironmentVariableHandlerFactory (com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory)19 HashMap (java.util.HashMap)13 ResourceLoader (org.springframework.core.io.ResourceLoader)13 Collectors (java.util.stream.Collectors)11 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)10 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)9 MutablePropertySources (org.springframework.core.env.MutablePropertySources)8 AlertConstants (com.synopsys.integration.alert.api.common.model.AlertConstants)7 ChannelKeys (com.synopsys.integration.alert.descriptor.api.model.ChannelKeys)7 EnvironmentVariableMockingUtil (com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil)7 Map (java.util.Map)7 Predicate (java.util.function.Predicate)7