Search in sources :

Example 21 with ResourceLoader

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

the class AbstractBeanDefinitionReader method loadBeanDefinitions.

/**
 * Load bean definitions from the specified resource location.
 * <p>The location can also be a location pattern, provided that the
 * ResourceLoader of this bean definition reader is a ResourcePatternResolver.
 * @param location the resource location, to be loaded with the ResourceLoader
 * (or ResourcePatternResolver) of this bean definition reader
 * @param actualResources a Set to be filled with the actual Resource objects
 * that have been resolved during the loading process. May be {@code null}
 * to indicate that the caller is not interested in those Resource objects.
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 * @see #getResourceLoader()
 * @see #loadBeanDefinitions(org.springframework.core.io.Resource)
 * @see #loadBeanDefinitions(org.springframework.core.io.Resource[])
 */
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
    ResourceLoader resourceLoader = getResourceLoader();
    if (resourceLoader == null) {
        throw new BeanDefinitionStoreException("Cannot load bean definitions from location [" + location + "]: no ResourceLoader available");
    }
    if (resourceLoader instanceof ResourcePatternResolver) {
        // Resource pattern matching available.
        try {
            Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
            int count = loadBeanDefinitions(resources);
            if (actualResources != null) {
                Collections.addAll(actualResources, resources);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Loaded " + count + " bean definitions from location pattern [" + location + "]");
            }
            return count;
        } catch (IOException ex) {
            throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", ex);
        }
    } else {
        // Can only load single resources by absolute URL.
        Resource resource = resourceLoader.getResource(location);
        int count = loadBeanDefinitions(resource);
        if (actualResources != null) {
            actualResources.add(resource);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Loaded " + count + " bean definitions from location [" + location + "]");
        }
        return count;
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException)

Example 22 with ResourceLoader

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

the class BeanDefinitionRegistrarTests method registerWithConstructorInstantiation.

@Test
void registerWithConstructorInstantiation() {
    ResourceLoader resourceLoader = new DefaultResourceLoader();
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerResolvableDependency(ResourceLoader.class, resourceLoader);
    BeanDefinitionRegistrar.of("test", ConstructorSample.class).withConstructor(ResourceLoader.class).instanceSupplier(instanceContext -> instanceContext.create(beanFactory, attributes -> new ConstructorSample(attributes.get(0)))).register(beanFactory);
    assertBeanFactory(beanFactory, () -> {
        assertThat(beanFactory.containsBean("test")).isTrue();
        assertThat(beanFactory.getBean(ConstructorSample.class).resourceLoader).isEqualTo(resourceLoader);
    });
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) InOrder(org.mockito.InOrder) ResourceLoader(org.springframework.core.io.ResourceLoader) FatalBeanException(org.springframework.beans.FatalBeanException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanInstanceContext(org.springframework.beans.factory.generator.config.BeanDefinitionRegistrar.BeanInstanceContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Field(java.lang.reflect.Field) Test(org.junit.jupiter.api.Test) Value(org.springframework.beans.factory.annotation.Value) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Environment(org.springframework.core.env.Environment) ReflectionUtils(org.springframework.util.ReflectionUtils) Mockito.inOrder(org.mockito.Mockito.inOrder) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ResolvableType(org.springframework.core.ResolvableType) Mockito.mock(org.mockito.Mockito.mock) ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Test(org.junit.jupiter.api.Test)

Example 23 with ResourceLoader

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

the class FreeMarkerConfigurationFactoryBeanTests method freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath.

@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
    fcfb.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    fcfb.setFreemarkerSettings(settings);
    fcfb.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();
        }
    });
    fcfb.afterPropertiesSet();
    assertThat(fcfb.getObject()).isInstanceOf(Configuration.class);
    Configuration fc = fcfb.getObject();
    Template ft = fc.getTemplate("test");
    assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test");
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test)

Example 24 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project webapp by elimu-ai.

the class EPubImageExtractionHelperTest method testExtractImageReferenceFromChapterFile_BEN_GDL_761.

@Test
public void testExtractImageReferenceFromChapterFile_BEN_GDL_761() throws IOException {
    ResourceLoader resourceLoader = new ClassRelativeResourceLoader(EPubImageExtractionHelper.class);
    Resource resource = resourceLoader.getResource("ben-gdl-761.epub_chapter-2.xhtml");
    File xhtmlFile = resource.getFile();
    logger.debug("xhtmlFile: " + xhtmlFile);
    String imageReference = EPubImageExtractionHelper.extractImageReferenceFromChapterFile(xhtmlFile);
    assertThat(imageReference, is("1e8e58cc7d627a7896737cfb3eba8270.jpg"));
}
Also used : ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) Resource(org.springframework.core.io.Resource) File(java.io.File) Test(org.junit.Test)

Example 25 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project webapp by elimu-ai.

the class EPubParagraphExtractionHelperTest method testExtractParagraphsFromChapterFile_SWA_GDL_30.

@Test
public void testExtractParagraphsFromChapterFile_SWA_GDL_30() throws IOException {
    ResourceLoader resourceLoader = new ClassRelativeResourceLoader(EPubParagraphExtractionHelper.class);
    Resource resource = resourceLoader.getResource("swa-gdl-30.epub_chapter-2.xhtml");
    File xhtmlFile = resource.getFile();
    logger.debug("xhtmlFile: " + xhtmlFile);
    List<String> storyBookParagraphs = EPubParagraphExtractionHelper.extractParagraphsFromChapterFile(xhtmlFile);
    assertThat(storyBookParagraphs.size(), is(1));
    assertThat(storyBookParagraphs.get(0), is("Kuku na Jongoo walikuwa marafiki."));
}
Also used : ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) Resource(org.springframework.core.io.Resource) File(java.io.File) Test(org.junit.Test)

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