Search in sources :

Example 21 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-framework by spring-projects.

the class TestPropertySourceUtilsTests method addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile.

@Test
public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() {
    ConfigurableEnvironment environment = new MockEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
    assertEquals(0, propertySources.size());
    String pair = "key = value";
    ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair);
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    when(resourceLoader.getResource(anyString())).thenReturn(resource);
    addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS);
    assertEquals(1, propertySources.size());
    assertEquals("value", environment.getProperty("key"));
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MockEnvironment(org.springframework.mock.env.MockEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.Test)

Example 22 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project camel by apache.

the class CamelAutoConfiguration method camelContext.

/**
     * Spring-aware Camel context for the application. Auto-detects and loads all routes available in the Spring context.
     */
@Bean
@ConditionalOnMissingBean(CamelContext.class)
CamelContext camelContext(ApplicationContext applicationContext, CamelConfigurationProperties config) {
    if (ObjectHelper.isNotEmpty(config.getFileConfigurations())) {
        Environment env = applicationContext.getEnvironment();
        if (env instanceof ConfigurableEnvironment) {
            MutablePropertySources sources = ((ConfigurableEnvironment) env).getPropertySources();
            if (sources != null) {
                if (!sources.contains("camel-file-configuration")) {
                    sources.addFirst(new FilePropertySource("camel-file-configuration", applicationContext, config.getFileConfigurations()));
                }
            }
        }
    }
    CamelContext camelContext = new SpringCamelContext(applicationContext);
    SpringCamelContext.setNoStart(true);
    if (!config.isJmxEnabled()) {
        camelContext.disableJMX();
    }
    if (config.getName() != null) {
        ((SpringCamelContext) camelContext).setName(config.getName());
    }
    if (config.getShutdownTimeout() > 0) {
        camelContext.getShutdownStrategy().setTimeout(config.getShutdownTimeout());
    }
    camelContext.getShutdownStrategy().setSuppressLoggingOnTimeout(config.isShutdownSuppressLoggingOnTimeout());
    camelContext.getShutdownStrategy().setShutdownNowOnTimeout(config.isShutdownNowOnTimeout());
    camelContext.getShutdownStrategy().setShutdownRoutesInReverseOrder(config.isShutdownRoutesInReverseOrder());
    camelContext.getShutdownStrategy().setLogInflightExchangesOnTimeout(config.isShutdownLogInflightExchangesOnTimeout());
    if (config.getLogDebugMaxChars() > 0) {
        camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "" + config.getLogDebugMaxChars());
    }
    // stream caching
    camelContext.setStreamCaching(config.isStreamCachingEnabled());
    camelContext.getStreamCachingStrategy().setAnySpoolRules(config.isStreamCachingAnySpoolRules());
    camelContext.getStreamCachingStrategy().setBufferSize(config.getStreamCachingBufferSize());
    camelContext.getStreamCachingStrategy().setRemoveSpoolDirectoryWhenStopping(config.isStreamCachingRemoveSpoolDirectoryWhenStopping());
    camelContext.getStreamCachingStrategy().setSpoolChiper(config.getStreamCachingSpoolChiper());
    if (config.getStreamCachingSpoolDirectory() != null) {
        camelContext.getStreamCachingStrategy().setSpoolDirectory(config.getStreamCachingSpoolDirectory());
    }
    if (config.getStreamCachingSpoolThreshold() != 0) {
        camelContext.getStreamCachingStrategy().setSpoolThreshold(config.getStreamCachingSpoolThreshold());
    }
    if (config.getStreamCachingSpoolUsedHeapMemoryLimit() != null) {
        StreamCachingStrategy.SpoolUsedHeapMemoryLimit limit;
        if ("Committed".equalsIgnoreCase(config.getStreamCachingSpoolUsedHeapMemoryLimit())) {
            limit = StreamCachingStrategy.SpoolUsedHeapMemoryLimit.Committed;
        } else if ("Max".equalsIgnoreCase(config.getStreamCachingSpoolUsedHeapMemoryLimit())) {
            limit = StreamCachingStrategy.SpoolUsedHeapMemoryLimit.Max;
        } else {
            throw new IllegalArgumentException("Invalid option " + config.getStreamCachingSpoolUsedHeapMemoryLimit() + " must either be Committed or Max");
        }
        camelContext.getStreamCachingStrategy().setSpoolUsedHeapMemoryLimit(limit);
    }
    if (config.getStreamCachingSpoolUsedHeapMemoryThreshold() != 0) {
        camelContext.getStreamCachingStrategy().setSpoolUsedHeapMemoryThreshold(config.getStreamCachingSpoolUsedHeapMemoryThreshold());
    }
    camelContext.setMessageHistory(config.isMessageHistory());
    camelContext.setLogExhaustedMessageBody(config.isLogExhaustedMessageBody());
    camelContext.setHandleFault(config.isHandleFault());
    camelContext.setAutoStartup(config.isAutoStartup());
    camelContext.setAllowUseOriginalMessage(config.isAllowUseOriginalMessage());
    if (camelContext.getManagementStrategy().getManagementAgent() != null) {
        camelContext.getManagementStrategy().getManagementAgent().setEndpointRuntimeStatisticsEnabled(config.isEndpointRuntimeStatisticsEnabled());
        camelContext.getManagementStrategy().getManagementAgent().setStatisticsLevel(config.getJmxManagementStatisticsLevel());
        camelContext.getManagementStrategy().getManagementAgent().setManagementNamePattern(config.getJmxManagementNamePattern());
        camelContext.getManagementStrategy().getManagementAgent().setCreateConnector(config.isJmxCreateConnector());
    }
    camelContext.setPackageScanClassResolver(new FatJarPackageScanClassResolver());
    // tracing
    camelContext.setTracing(config.isTracing());
    if (camelContext.getDefaultTracer() instanceof Tracer) {
        Tracer tracer = (Tracer) camelContext.getDefaultTracer();
        if (tracer.getDefaultTraceFormatter() != null) {
            DefaultTraceFormatter formatter = tracer.getDefaultTraceFormatter();
            if (config.getTracerFormatterBreadCrumbLength() != null) {
                formatter.setBreadCrumbLength(config.getTracerFormatterBreadCrumbLength());
            }
            if (config.getTracerFormatterMaxChars() != null) {
                formatter.setMaxChars(config.getTracerFormatterMaxChars());
            }
            if (config.getTracerFormatterNodeLength() != null) {
                formatter.setNodeLength(config.getTracerFormatterNodeLength());
            }
            formatter.setShowBody(config.isTraceFormatterShowBody());
            formatter.setShowBodyType(config.isTracerFormatterShowBodyType());
            formatter.setShowBreadCrumb(config.isTraceFormatterShowBreadCrumb());
            formatter.setShowException(config.isTraceFormatterShowException());
            formatter.setShowExchangeId(config.isTraceFormatterShowExchangeId());
            formatter.setShowExchangePattern(config.isTraceFormatterShowExchangePattern());
            formatter.setShowHeaders(config.isTraceFormatterShowHeaders());
            formatter.setShowNode(config.isTraceFormatterShowNode());
            formatter.setShowProperties(config.isTraceFormatterShowProperties());
            formatter.setShowRouteId(config.isTraceFormatterShowRouteId());
            formatter.setShowShortExchangeId(config.isTraceFormatterShowShortExchangeId());
        }
    }
    if (config.getXmlRoutesReloadDirectory() != null) {
        ReloadStrategy reload = new FileWatcherReloadStrategy(config.getXmlRoutesReloadDirectory());
        camelContext.setReloadStrategy(reload);
    }
    // additional advanced configuration which is not configured using CamelConfigurationProperties
    afterPropertiesSet(applicationContext, camelContext);
    return camelContext;
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) CamelContext(org.apache.camel.CamelContext) DefaultTraceFormatter(org.apache.camel.processor.interceptor.DefaultTraceFormatter) FileWatcherReloadStrategy(org.apache.camel.impl.FileWatcherReloadStrategy) Tracer(org.apache.camel.processor.interceptor.Tracer) BacklogTracer(org.apache.camel.processor.interceptor.BacklogTracer) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Environment(org.springframework.core.env.Environment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) StreamCachingStrategy(org.apache.camel.spi.StreamCachingStrategy) ReloadStrategy(org.apache.camel.spi.ReloadStrategy) FileWatcherReloadStrategy(org.apache.camel.impl.FileWatcherReloadStrategy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 23 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testOverridingDefaultTemplateViaConfig.

public void testOverridingDefaultTemplateViaConfig() throws Exception {
    ConfigObject config = new ConfigSlurper().parse("grails.sitemesh.default.layout='otherApplication'");
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("grails", config));
    GrailsWebRequest webRequest = buildMockRequest(new PropertySourcesConfig(propertySources));
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
    appCtx.registerMockResource("/grails-app/views/layouts/otherApplication.gsp", "<html><body><h1>Other Default Layout</h1><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class FooController {\n" + "def controllerName = 'foo'\n" + "def actionUri = '/foo/fooAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/otherApplication.gsp", d.getPage());
    assertEquals("otherApplication", d.getName());
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) MapPropertySource(org.springframework.core.env.MapPropertySource) ServletContext(javax.servlet.ServletContext) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) ConfigObject(groovy.util.ConfigObject) ConfigSlurper(groovy.util.ConfigSlurper)

Example 24 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project grails-core by grails.

the class DefaultGrailsApplication method getConfig.

public Config getConfig() {
    if (config == null) {
        if (parentContext != null) {
            org.springframework.core.env.Environment environment = parentContext.getEnvironment();
            if (environment instanceof ConfigurableEnvironment) {
                MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
                this.config = new PropertySourcesConfig(propertySources);
            } else {
                this.config = new PropertySourcesConfig();
            }
        } else {
            this.config = new PropertySourcesConfig();
        }
        setConfig(this.config);
    }
    return config;
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 25 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project java-chassis by ServiceComb.

the class ConfigurableEnvironmentConfiguration method getPropertySources.

private Map<String, PropertySource<?>> getPropertySources() {
    Map<String, PropertySource<?>> map = new LinkedHashMap<>();
    MutablePropertySources sources = (this.environment != null ? this.environment.getPropertySources() : new StandardEnvironment().getPropertySources());
    for (PropertySource<?> source : sources) {
        extract("", map, source);
    }
    return map;
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources) LinkedHashMap(java.util.LinkedHashMap) CompositePropertySource(org.springframework.core.env.CompositePropertySource) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Aggregations

MutablePropertySources (org.springframework.core.env.MutablePropertySources)50 Test (org.junit.Test)19 MapPropertySource (org.springframework.core.env.MapPropertySource)11 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)10 PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)8 MockPropertySource (org.springframework.mock.env.MockPropertySource)8 Properties (java.util.Properties)6 PropertySourcesPropertyResolver (org.springframework.core.env.PropertySourcesPropertyResolver)6 CompositePropertySource (org.springframework.core.env.CompositePropertySource)5 ByteArrayResource (org.springframework.core.io.ByteArrayResource)4 LinkedHashMap (java.util.LinkedHashMap)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 RandomValuePropertySource (org.springframework.boot.env.RandomValuePropertySource)3 PropertySource (org.springframework.core.env.PropertySource)3 StandardEnvironment (org.springframework.core.env.StandardEnvironment)3 SystemEnvironmentPropertySource (org.springframework.core.env.SystemEnvironmentPropertySource)3 MockEnvironment (org.springframework.mock.env.MockEnvironment)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2