Search in sources :

Example 1 with EncodedResource

use of cn.taketoday.core.io.EncodedResource in project today-infrastructure by TAKETODAY.

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.commentPrefixes, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter);
    }
}
Also used : EncodedResource(cn.taketoday.core.io.EncodedResource) Resource(cn.taketoday.core.io.Resource) EncodedResource(cn.taketoday.core.io.EncodedResource)

Example 2 with EncodedResource

use of cn.taketoday.core.io.EncodedResource in project today-infrastructure by TAKETODAY.

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.isTraceEnabled()) {
        logger.trace("Loading XML bean definitions from {}", encodedResource);
    }
    Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
    if (!currentResources.add(encodedResource)) {
        throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");
    }
    try (InputStream inputStream = encodedResource.getResource().getInputStream()) {
        InputSource inputSource = new InputSource(inputStream);
        if (encodedResource.getEncoding() != null) {
            inputSource.setEncoding(encodedResource.getEncoding());
        }
        return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
    } 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(cn.taketoday.beans.factory.BeanDefinitionStoreException) InputStream(java.io.InputStream) IOException(java.io.IOException) EncodedResource(cn.taketoday.core.io.EncodedResource)

Example 3 with EncodedResource

use of cn.taketoday.core.io.EncodedResource in project today-infrastructure by TAKETODAY.

the class XmlBeanFactoryTests method refToSingleton.

@Test
void refToSingleton() {
    StandardBeanFactory xbf = new StandardBeanFactory();
    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();
    assertThat(davesJen == jenksJen).as("1 jen instance").isTrue();
    assertThat(davesJen == jen).as("1 jen instance").isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) EncodedResource(cn.taketoday.core.io.EncodedResource) Test(org.junit.jupiter.api.Test)

Example 4 with EncodedResource

use of cn.taketoday.core.io.EncodedResource in project today-framework by TAKETODAY.

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.commentPrefixes, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter);
    }
}
Also used : EncodedResource(cn.taketoday.core.io.EncodedResource) Resource(cn.taketoday.core.io.Resource) EncodedResource(cn.taketoday.core.io.EncodedResource)

Example 5 with EncodedResource

use of cn.taketoday.core.io.EncodedResource in project today-framework by TAKETODAY.

the class XmlBeanFactoryTests method refToSingleton.

@Test
void refToSingleton() {
    StandardBeanFactory xbf = new StandardBeanFactory();
    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();
    assertThat(davesJen == jenksJen).as("1 jen instance").isTrue();
    assertThat(davesJen == jen).as("1 jen instance").isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) EncodedResource(cn.taketoday.core.io.EncodedResource) Test(org.junit.jupiter.api.Test)

Aggregations

EncodedResource (cn.taketoday.core.io.EncodedResource)10 Resource (cn.taketoday.core.io.Resource)6 IOException (java.io.IOException)4 PropertyValues (cn.taketoday.beans.PropertyValues)2 BeanDefinitionStoreException (cn.taketoday.beans.factory.BeanDefinitionStoreException)2 PropertiesBeanDefinitionReader (cn.taketoday.beans.factory.support.PropertiesBeanDefinitionReader)2 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 ResourceTestBean (cn.taketoday.tests.sample.beans.ResourceTestBean)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Test (org.junit.jupiter.api.Test)2 InputSource (org.xml.sax.InputSource)2