Search in sources :

Example 1 with EncodedResource

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

the class XmlBeanDefinitionReader method loadBeanDefinitions.

/**
	 * Load bean definitions from the specified XML file.
	 * @param encodedResource the resource descriptor for the XML file,
	 * allowing to specify an encoding to use for parsing the file
	 * @return the number of bean definitions found
	 * @throws BeanDefinitionStoreException in case of loading or parsing errors
	 */
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
    Assert.notNull(encodedResource, "EncodedResource must not be null");
    if (logger.isInfoEnabled()) {
        logger.info("Loading XML bean definitions from " + encodedResource.getResource());
    }
    Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
    if (currentResources == null) {
        currentResources = new HashSet<>(4);
        this.resourcesCurrentlyBeingLoaded.set(currentResources);
    }
    if (!currentResources.add(encodedResource)) {
        throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");
    }
    try {
        InputStream inputStream = encodedResource.getResource().getInputStream();
        try {
            InputSource inputSource = new InputSource(inputStream);
            if (encodedResource.getEncoding() != null) {
                inputSource.setEncoding(encodedResource.getEncoding());
            }
            return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
        } finally {
            inputStream.close();
        }
    } catch (IOException ex) {
        throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), ex);
    } finally {
        currentResources.remove(encodedResource);
        if (currentResources.isEmpty()) {
            this.resourcesCurrentlyBeingLoaded.remove();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) InputStream(java.io.InputStream) IOException(java.io.IOException) EncodedResource(org.springframework.core.io.support.EncodedResource)

Example 2 with EncodedResource

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

the class XmlBeanFactoryTests method testRefToSingleton.

@Test
public void testRefToSingleton() throws Exception {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
    reader.loadBeanDefinitions(new EncodedResource(REFTYPES_CONTEXT, "ISO-8859-1"));
    TestBean jen = (TestBean) xbf.getBean("jenny");
    TestBean dave = (TestBean) xbf.getBean("david");
    TestBean jenks = (TestBean) xbf.getBean("jenks");
    ITestBean davesJen = dave.getSpouse();
    ITestBean jenksJen = jenks.getSpouse();
    assertTrue("1 jen instance", davesJen == jenksJen);
    assertTrue("1 jen instance", davesJen == jen);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) EncodedResource(org.springframework.core.io.support.EncodedResource) Test(org.junit.Test)

Example 3 with EncodedResource

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

the class ReaderEditor method setAsText.

@Override
public void setAsText(String text) throws IllegalArgumentException {
    this.resourceEditor.setAsText(text);
    Resource resource = (Resource) this.resourceEditor.getValue();
    try {
        setValue(resource != null ? new EncodedResource(resource).getReader() : null);
    } catch (IOException ex) {
        throw new IllegalArgumentException("Failed to retrieve Reader for " + resource, ex);
    }
}
Also used : EncodedResource(org.springframework.core.io.support.EncodedResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) EncodedResource(org.springframework.core.io.support.EncodedResource)

Example 4 with EncodedResource

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

the class ApplicationContextExpressionTests method resourceInjection.

@Test
public void resourceInjection() throws IOException {
    System.setProperty("logfile", "do_not_delete_me.txt");
    try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ResourceInjectionBean.class)) {
        ResourceInjectionBean resourceInjectionBean = ac.getBean(ResourceInjectionBean.class);
        Resource resource = new ClassPathResource("do_not_delete_me.txt");
        assertEquals(resource, resourceInjectionBean.resource);
        assertEquals(resource.getURL(), resourceInjectionBean.url);
        assertEquals(resource.getURI(), resourceInjectionBean.uri);
        assertEquals(resource.getFile(), resourceInjectionBean.file);
        assertArrayEquals(FileCopyUtils.copyToByteArray(resource.getInputStream()), FileCopyUtils.copyToByteArray(resourceInjectionBean.inputStream));
        assertEquals(FileCopyUtils.copyToString(new EncodedResource(resource).getReader()), FileCopyUtils.copyToString(resourceInjectionBean.reader));
    } finally {
        System.getProperties().remove("logfile");
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ClassPathResource(org.springframework.core.io.ClassPathResource) EncodedResource(org.springframework.core.io.support.EncodedResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) EncodedResource(org.springframework.core.io.support.EncodedResource) Test(org.junit.Test)

Example 5 with EncodedResource

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

the class ResourceDatabasePopulator method populate.

/**
	 * {@inheritDoc}
	 * @see #execute(DataSource)
	 */
@Override
public void populate(Connection connection) throws ScriptException {
    Assert.notNull(connection, "Connection must not be null");
    for (Resource script : this.scripts) {
        EncodedResource encodedScript = new EncodedResource(script, this.sqlScriptEncoding);
        ScriptUtils.executeSqlScript(connection, encodedScript, this.continueOnError, this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter);
    }
}
Also used : EncodedResource(org.springframework.core.io.support.EncodedResource) Resource(org.springframework.core.io.Resource) EncodedResource(org.springframework.core.io.support.EncodedResource)

Aggregations

EncodedResource (org.springframework.core.io.support.EncodedResource)7 Resource (org.springframework.core.io.Resource)5 IOException (java.io.IOException)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 PropertiesBeanDefinitionReader (org.springframework.beans.factory.support.PropertiesBeanDefinitionReader)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 DefaultPropertySourceFactory (org.springframework.core.io.support.DefaultPropertySourceFactory)1 PropertySourceFactory (org.springframework.core.io.support.PropertySourceFactory)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)1 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1