Search in sources :

Example 11 with ResourceLoader

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

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '" + filterConfig.getFilterName() + "' configured for use");
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) NestedServletException(org.springframework.web.util.NestedServletException) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) Environment(org.springframework.core.env.Environment) ResourceEditor(org.springframework.core.io.ResourceEditor) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 12 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project pinpoint by naver.

the class ExternalEnvironment method processEnvironment.

public void processEnvironment(ConfigurableEnvironment environment) {
    final String profile = environment.getProperty(PinpointProfileEnvironment.PINPOINT_ACTIVE_PROFILE);
    if (profile == null) {
        throw new IllegalStateException("profile is not set");
    }
    ResourceLoader resourceLoader = defaultResourceLoader();
    Map<String, Object> systemEnvironment = environment.getSystemProperties();
    final String externalConfigLocation = getString(systemEnvironment, externalConfigurationKey);
    if (externalConfigLocation == null) {
        logger.info(String.format("-D%s is not set", externalConfigurationKey));
        return;
    }
    logger.info("load PropertySource name:{}", getName());
    PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader(profile, resourceLoader);
    String sourceName = resolveSourceName(externalConfigurationKey, externalConfigLocation);
    String resourcePath = String.format("file:%s", externalConfigLocation);
    PropertiesPropertySource propertySource = loader.loadPropertySource(sourceName, Collections.singletonList(resourcePath));
    logger.info("Add PropertySource name:{}", sourceName);
    environment.getPropertySources().addLast(propertySource);
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource)

Example 13 with ResourceLoader

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

the class OnWarDeploymentCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    ResourceLoader resourceLoader = context.getResourceLoader();
    if (resourceLoader instanceof WebApplicationContext) {
        WebApplicationContext applicationContext = (WebApplicationContext) resourceLoader;
        ServletContext servletContext = applicationContext.getServletContext();
        if (servletContext != null) {
            return ConditionOutcome.match("Application is deployed as a WAR file.");
        }
    }
    return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnWarDeployment.class).because("the application is not deployed as a WAR file."));
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContext(jakarta.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 14 with ResourceLoader

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

the class ClassLoaderFilesResourcePatternResolver method retrieveResourceLoader.

private ResourceLoader retrieveResourceLoader(ApplicationContext applicationContext) {
    Field field = ReflectionUtils.findField(applicationContext.getClass(), "resourceLoader", ResourceLoader.class);
    if (field == null) {
        return null;
    }
    ReflectionUtils.makeAccessible(field);
    return (ResourceLoader) ReflectionUtils.getField(field, applicationContext);
}
Also used : Field(java.lang.reflect.Field) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader)

Example 15 with ResourceLoader

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

the class SpringBootContextLoader method prepareEnvironment.

private void prepareEnvironment(MergedContextConfiguration config, SpringApplication application, ConfigurableEnvironment environment, boolean applicationEnvironment) {
    setActiveProfiles(environment, config.getActiveProfiles(), applicationEnvironment);
    ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader() : new DefaultResourceLoader(null);
    TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader, config.getPropertySourceLocations());
    TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
}
Also used : DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

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