Search in sources :

Example 16 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-boot by spring-projects.

the class ConfigDataEnvironmentPostProcessorTests method postProcessEnvironmentWhenCustomLoaderUsesSpecifiedLoaderInstance.

@Test
void postProcessEnvironmentWhenCustomLoaderUsesSpecifiedLoaderInstance() {
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    this.application.setResourceLoader(resourceLoader);
    willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
    this.postProcessor.postProcessEnvironment(this.environment, this.application);
    then(this.postProcessor).should().getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
    then(this.configDataEnvironment).should().processAndApply();
    assertThat(this.resourceLoaderCaptor.getValue()).isSameAs(resourceLoader);
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Test(org.junit.jupiter.api.Test)

Example 17 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-framework by spring-projects.

the class TestPropertySourceUtilsTests method addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile.

@Test
void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() {
    ConfigurableEnvironment environment = new MockEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
    assertThat(propertySources.size()).isEqualTo(0);
    String pair = "key = value";
    ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair);
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    given(resourceLoader.getResource(anyString())).willReturn(resource);
    addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS);
    assertThat(propertySources.size()).isEqualTo(1);
    assertThat(environment.getProperty("key")).isEqualTo("value");
}
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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 18 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project engine by craftercms.

the class SiteContextFactory method createContext.

public SiteContext createContext(String siteName) {
    Map<String, String> macroValues = new HashMap<>();
    macroValues.put(siteNameMacroName, siteName);
    if (publishingTargetResolver instanceof SiteAwarePublishingTargetResolver) {
        String target = ((SiteAwarePublishingTargetResolver) publishingTargetResolver).getPublishingTarget(siteName);
        macroValues.put(publishingTargetMacroName, target);
    }
    String resolvedRootFolderPath = macroResolver.resolveMacros(rootFolderPath, macroValues);
    Context context = storeService.getContext(UUID.randomUUID().toString(), storeType, resolvedRootFolderPath, mergingOn, cacheOn, maxAllowedItemsInCache, ignoreHiddenFiles);
    try {
        SiteContext siteContext = new SiteContext();
        siteContext.setInitTimeout(initTimeout);
        siteContext.setStoreService(storeService);
        siteContext.setCacheTemplate(cacheTemplate);
        siteContext.setSiteName(siteName);
        siteContext.setContext(context);
        siteContext.setStaticAssetsPath(staticAssetsPath);
        siteContext.setTemplatesPath(templatesPath);
        siteContext.setInitScriptPath(initScriptPath);
        siteContext.setFreeMarkerConfig(freeMarkerConfigFactory.getObject());
        siteContext.setUrlTransformationEngine(urlTransformationEngine);
        siteContext.setRestScriptsPath(restScriptsPath);
        siteContext.setControllerScriptsPath(controllerScriptsPath);
        siteContext.setGraphQLFactory(graphQLFactory);
        siteContext.setShutdownTimeout(shutdownTimeout);
        if (disableVariableRestrictions) {
            siteContext.setServletContext(servletContext);
        }
        if (cacheWarmUpEnabled) {
            siteContext.setCacheWarmer(cacheWarmer);
        }
        String[] resolvedConfigPaths = new String[configPaths.length];
        for (int i = 0; i < configPaths.length; i++) {
            resolvedConfigPaths[i] = macroResolver.resolveMacros(configPaths[i], macroValues);
        }
        String[] resolvedAppContextPaths = new String[applicationContextPaths.length];
        for (int i = 0; i < applicationContextPaths.length; i++) {
            resolvedAppContextPaths[i] = macroResolver.resolveMacros(applicationContextPaths[i], macroValues);
        }
        String[] resolvedUrlRewriteConfPaths = new String[urlRewriteConfPaths.length];
        for (int i = 0; i < urlRewriteConfPaths.length; i++) {
            resolvedUrlRewriteConfPaths[i] = macroResolver.resolveMacros(urlRewriteConfPaths[i], macroValues);
        }
        List<String> resolvedProxyConfPaths = Stream.of(proxyConfigPaths).map(path -> macroResolver.resolveMacros(path, macroValues)).collect(toList());
        ResourceLoader resourceLoader = new ContentStoreResourceLoader(siteContext);
        HierarchicalConfiguration<?> config = getConfig(siteContext, resolvedConfigPaths, resourceLoader);
        configureScriptSandbox(siteContext, resourceLoader);
        URLClassLoader classLoader = getClassLoader(siteContext);
        ScriptFactory scriptFactory = getScriptFactory(siteContext, classLoader);
        ConfigurableApplicationContext appContext = getApplicationContext(siteContext, classLoader, config, resolvedAppContextPaths, resourceLoader);
        UrlRewriter urlRewriter = getUrlRewriter(siteContext, resolvedUrlRewriteConfPaths, resourceLoader);
        HierarchicalConfiguration proxyConfig = getProxyConfig(siteContext, resolvedProxyConfPaths, resourceLoader);
        siteContext.setScriptFactory(scriptFactory);
        siteContext.setConfig(config);
        siteContext.setGlobalApplicationContext(globalApplicationContext);
        siteContext.setApplicationContext(appContext);
        siteContext.setClassLoader(classLoader);
        siteContext.setUrlRewriter(urlRewriter);
        siteContext.setProxyConfig(proxyConfig);
        if (config != null) {
            siteContext.setAllowedTemplatePaths(config.getStringArray(CONFIG_KEY_ALLOWED_TEMPLATE_PATHS));
        }
        Scheduler scheduler = scheduleJobs(siteContext);
        siteContext.setScheduler(scheduler);
        return siteContext;
    } catch (Exception e) {
        logger.error("Error creating context for site '" + siteName + "'", e);
        // Destroy context if the site context creation failed
        storeService.destroyContext(context);
        throw e;
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Context(org.craftercms.core.service.Context) JobContext(org.craftercms.engine.util.quartz.JobContext) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RestrictedApplicationContext(org.craftercms.engine.util.spring.context.RestrictedApplicationContext) ServletContext(javax.servlet.ServletContext) ServletContextAware(org.springframework.web.context.ServletContextAware) FreeMarkerConfig(org.springframework.web.servlet.view.freemarker.FreeMarkerConfig) PublishingTargetResolver(org.craftercms.commons.config.PublishingTargetResolver) Scheduler(org.quartz.Scheduler) Collections.singletonList(java.util.Collections.singletonList) ContentStoreService(org.craftercms.core.service.ContentStoreService) URLClassLoader(java.net.URLClassLoader) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) ContentStoreGroovyResourceLoader(org.craftercms.engine.util.groovy.ContentStoreGroovyResourceLoader) Resource(org.springframework.core.io.Resource) MacroResolver(org.craftercms.engine.macro.MacroResolver) ResourceLoader(org.springframework.core.io.ResourceLoader) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Context(org.craftercms.core.service.Context) CacheTemplate(org.craftercms.core.util.cache.CacheTemplate) ConfigurationException(org.craftercms.commons.config.ConfigurationException) Stream(java.util.stream.Stream) ContentStoreResourceConnector(org.craftercms.engine.util.groovy.ContentStoreResourceConnector) GroovyScriptUtils.getCompilerConfiguration(org.craftercms.engine.util.GroovyScriptUtils.getCompilerConfiguration) Dom4jExtension(org.craftercms.engine.util.groovy.Dom4jExtension) SiteCacheWarmer(org.craftercms.engine.cache.SiteCacheWarmer) PermitAllWhitelist(org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.PermitAllWhitelist) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) LogFactory(org.apache.commons.logging.LogFactory) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GroovyClassLoader(groovy.lang.GroovyClassLoader) ScriptJobResolver(org.craftercms.engine.scripting.ScriptJobResolver) ApplicationContextAware(org.springframework.context.ApplicationContextAware) java.util(java.util) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) CollectionUtils(org.apache.commons.collections4.CollectionUtils) GraphQLFactory(org.craftercms.engine.graphql.GraphQLFactory) JobContext(org.craftercms.engine.util.quartz.JobContext) ApacheCommonsConfiguration2PropertySource(org.craftercms.commons.spring.ApacheCommonsConfiguration2PropertySource) SiteAwarePublishingTargetResolver(org.craftercms.engine.util.config.SiteAwarePublishingTargetResolver) EncryptionAwareConfigurationReader(org.craftercms.commons.config.EncryptionAwareConfigurationReader) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) SchedulingUtils(org.craftercms.engine.util.SchedulingUtils) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Executor(java.util.concurrent.Executor) Conf(org.tuckey.web.filters.urlrewrite.Conf) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) Blacklist(org.jenkinsci.plugins.scriptsecurity.sandbox.blacklists.Blacklist) ApplicationContext(org.springframework.context.ApplicationContext) InputStreamReader(java.io.InputStreamReader) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ObjectFactory(org.springframework.beans.factory.ObjectFactory) Whitelist(org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist) Collectors.toList(java.util.stream.Collectors.toList) UrlTransformationEngine(org.craftercms.core.url.UrlTransformationEngine) RestrictedApplicationContext(org.craftercms.engine.util.spring.context.RestrictedApplicationContext) Log(org.apache.commons.logging.Log) SandboxInterceptor(org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor) ServletContext(javax.servlet.ServletContext) Required(org.springframework.beans.factory.annotation.Required) InputStream(java.io.InputStream) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ContentStoreGroovyResourceLoader(org.craftercms.engine.util.groovy.ContentStoreGroovyResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) Scheduler(org.quartz.Scheduler) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) ConfigurationException(org.craftercms.commons.config.ConfigurationException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) SiteAwarePublishingTargetResolver(org.craftercms.engine.util.config.SiteAwarePublishingTargetResolver) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) URLClassLoader(java.net.URLClassLoader) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 19 with ResourceLoader

use of org.springframework.core.io.ResourceLoader 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 20 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project grails-core by grails.

the class DefaultResourceLocator method setSearchLocation.

public void setSearchLocation(String searchLocation) {
    ResourceLoader resourceLoader = getDefaultResourceLoader();
    patchMatchingResolver = new CachingPathMatchingResourcePatternResolver(resourceLoader);
    initializeForSearchLocation(searchLocation);
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader)

Aggregations

ResourceLoader (org.springframework.core.io.ResourceLoader)90 Resource (org.springframework.core.io.Resource)51 Test (org.junit.Test)35 ClassRelativeResourceLoader (org.springframework.core.io.ClassRelativeResourceLoader)34 File (java.io.File)33 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)26 Test (org.junit.jupiter.api.Test)15 IOException (java.io.IOException)9 Environment (org.springframework.core.env.Environment)9 ByteArrayResource (org.springframework.core.io.ByteArrayResource)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 FileSystemResource (org.springframework.core.io.FileSystemResource)6 HashMap (java.util.HashMap)5 Configuration (freemarker.template.Configuration)4 Template (freemarker.template.Template)4 InputStream (java.io.InputStream)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 InputStreamReader (java.io.InputStreamReader)3 Properties (java.util.Properties)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3