Search in sources :

Example 6 with EncodedResource

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

the class StaticApplicationContextMulticasterTests method createContext.

@Override
protected ConfigurableApplicationContext createContext() throws Exception {
    StaticApplicationContext parent = new StaticApplicationContext();
    Map<String, String> m = new HashMap<>();
    m.put("name", "Roderick");
    parent.registerPrototype("rod", TestBean.class, new PropertyValues(m));
    m.put("name", "Albert");
    parent.registerPrototype("father", TestBean.class, new PropertyValues(m));
    parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, TestApplicationEventMulticaster.class, null);
    parent.refresh();
    parent.addApplicationListener(parentListener);
    parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");
    this.sac = new StaticApplicationContext(parent);
    sac.registerSingleton("beanThatListens", BeanThatListens.class, new PropertyValues());
    sac.registerSingleton("aca", ACATester.class, new PropertyValues());
    sac.registerPrototype("aca-prototype", ACATester.class, new PropertyValues());
    PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getBeanFactory());
    Resource resource = new ClassPathResource("testBeans.properties", getClass());
    reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1"));
    sac.refresh();
    sac.addApplicationListener(listener);
    sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");
    return sac;
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) HashMap(java.util.HashMap) PropertiesBeanDefinitionReader(cn.taketoday.beans.factory.support.PropertiesBeanDefinitionReader) Resource(cn.taketoday.core.io.Resource) EncodedResource(cn.taketoday.core.io.EncodedResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) EncodedResource(cn.taketoday.core.io.EncodedResource)

Example 7 with EncodedResource

use of cn.taketoday.core.io.EncodedResource in project today-framework 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 8 with EncodedResource

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

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(cn.taketoday.core.io.EncodedResource) Resource(cn.taketoday.core.io.Resource) IOException(java.io.IOException) EncodedResource(cn.taketoday.core.io.EncodedResource)

Example 9 with EncodedResource

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

the class StaticApplicationContextMulticasterTests method createContext.

@Override
protected ConfigurableApplicationContext createContext() throws Exception {
    StaticApplicationContext parent = new StaticApplicationContext();
    Map<String, String> m = new HashMap<>();
    m.put("name", "Roderick");
    parent.registerPrototype("rod", TestBean.class, new PropertyValues(m));
    m.put("name", "Albert");
    parent.registerPrototype("father", TestBean.class, new PropertyValues(m));
    parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, TestApplicationEventMulticaster.class, null);
    parent.refresh();
    parent.addApplicationListener(parentListener);
    parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");
    this.sac = new StaticApplicationContext(parent);
    sac.registerSingleton("beanThatListens", BeanThatListens.class, new PropertyValues());
    sac.registerSingleton("aca", ACATester.class, new PropertyValues());
    sac.registerPrototype("aca-prototype", ACATester.class, new PropertyValues());
    PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getBeanFactory());
    Resource resource = new ClassPathResource("testBeans.properties", getClass());
    reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1"));
    sac.refresh();
    sac.addApplicationListener(listener);
    sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");
    return sac;
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) HashMap(java.util.HashMap) PropertiesBeanDefinitionReader(cn.taketoday.beans.factory.support.PropertiesBeanDefinitionReader) Resource(cn.taketoday.core.io.Resource) EncodedResource(cn.taketoday.core.io.EncodedResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) EncodedResource(cn.taketoday.core.io.EncodedResource)

Example 10 with EncodedResource

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

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(cn.taketoday.core.io.EncodedResource) Resource(cn.taketoday.core.io.Resource) IOException(java.io.IOException) EncodedResource(cn.taketoday.core.io.EncodedResource)

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