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();
}
}
}
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);
}
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);
}
}
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");
}
}
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);
}
}
Aggregations