use of cn.taketoday.core.io.ResourceLoader in project today-framework by TAKETODAY.
the class ApplicationTests method customResourceLoaderFromConstructor.
@Test
void customResourceLoaderFromConstructor() {
ResourceLoader resourceLoader = new DefaultResourceLoader();
TestApplication application = new TestApplication(resourceLoader, ExampleWebConfig.class);
this.context = application.run();
then(application.getLoader()).should().setResourceLoader(resourceLoader);
}
use of cn.taketoday.core.io.ResourceLoader in project today-framework by TAKETODAY.
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 = getFilterConfigPropertyValues(filterConfig, requiredProperties);
if (!pvs.isEmpty()) {
try {
BeanWrapper bw = BeanWrapper.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 '{}' configured for use", filterConfig.getFilterName());
}
}
use of cn.taketoday.core.io.ResourceLoader in project today-framework by TAKETODAY.
the class FreeMarkerConfigurerTests method freeMarkerConfigurerWithNonFileResourceLoaderPath.
@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurerWithNonFileResourceLoaderPath() throws Exception {
freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
Properties settings = new Properties();
settings.setProperty("localized_lookup", "false");
freeMarkerConfigurer.setFreemarkerSettings(settings);
freeMarkerConfigurer.setResourceLoader(new ResourceLoader() {
@Override
public Resource getResource(String location) {
if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
throw new IllegalArgumentException(location);
}
return new ByteArrayResource("test".getBytes(), "test");
}
@Override
public ClassLoader getClassLoader() {
return getClass().getClassLoader();
}
});
freeMarkerConfigurer.afterPropertiesSet();
assertThat(freeMarkerConfigurer.getConfiguration()).isInstanceOf(Configuration.class);
Configuration fc = freeMarkerConfigurer.getConfiguration();
Template ft = fc.getTemplate("test");
assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test");
}
use of cn.taketoday.core.io.ResourceLoader in project today-infrastructure by TAKETODAY.
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 = getFilterConfigPropertyValues(filterConfig, requiredProperties);
if (!pvs.isEmpty()) {
try {
BeanWrapper bw = BeanWrapper.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 '{}' configured for use", filterConfig.getFilterName());
}
}
use of cn.taketoday.core.io.ResourceLoader in project today-infrastructure by TAKETODAY.
the class ConfigDataEnvironmentPostProcessorIntegrationTests method runWhenUsingCustomResourceLoader.
@Test
void runWhenUsingCustomResourceLoader() {
this.application.setResourceLoader(new ResourceLoader() {
@Override
public Resource getResource(String location) {
if (location.equals("classpath:/custom.properties")) {
return new ByteArrayResource("the.property: fromcustom".getBytes(), location);
}
return new ClassPathResource("doesnotexist");
}
@Override
public ClassLoader getClassLoader() {
return getClass().getClassLoader();
}
});
ConfigurableApplicationContext context = this.application.run("--context.config.name=custom");
String property = context.getEnvironment().getProperty("the.property");
assertThat(property).isEqualTo("fromcustom");
}
Aggregations