Search in sources :

Example 1 with Problem

use of org.springframework.beans.factory.parsing.Problem in project spring-framework by spring-projects.

the class GroovyBeanDefinitionReader method loadBeanDefinitions.

/**
	 * Load bean definitions from the specified Groovy script or XML file.
	 * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
	 * of resources will be parsed as Groovy scripts.
	 * @param encodedResource the resource descriptor for the Groovy script or XML file,
	 * allowing specification of 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 {
    // Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
    String filename = encodedResource.getResource().getFilename();
    if (StringUtils.endsWithIgnoreCase(filename, ".xml")) {
        return this.standardXmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
    }
    Closure beans = new Closure(this) {

        public Object call(Object[] args) {
            invokeBeanDefiningClosure((Closure) args[0]);
            return null;
        }
    };
    Binding binding = new Binding() {

        @Override
        public void setVariable(String name, Object value) {
            if (currentBeanDefinition != null) {
                applyPropertyToBeanDefinition(name, value);
            } else {
                super.setVariable(name, value);
            }
        }
    };
    binding.setVariable("beans", beans);
    int countBefore = getRegistry().getBeanDefinitionCount();
    try {
        GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
        shell.evaluate(encodedResource.getReader(), "beans");
    } catch (Throwable ex) {
        throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(), new Location(encodedResource.getResource()), null, ex));
    }
    return getRegistry().getBeanDefinitionCount() - countBefore;
}
Also used : Binding(groovy.lang.Binding) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) Closure(groovy.lang.Closure) GroovyObject(groovy.lang.GroovyObject) Problem(org.springframework.beans.factory.parsing.Problem) GString(groovy.lang.GString) GroovyShell(groovy.lang.GroovyShell) Location(org.springframework.beans.factory.parsing.Location)

Example 2 with Problem

use of org.springframework.beans.factory.parsing.Problem in project grails-core by grails.

the class BeanBuilder method xmlns.

/**
     * Defines a Spring namespace definition to use.
     *
     * @param definition The definition
     */
public void xmlns(Map<String, String> definition) {
    Assert.notNull(namespaceHandlerResolver, "You cannot define a Spring namespace without a [namespaceHandlerResolver] set");
    if (definition.isEmpty()) {
        return;
    }
    for (Map.Entry<String, String> entry : definition.entrySet()) {
        String namespace = entry.getKey();
        String uri = entry.getValue() == null ? null : entry.getValue();
        Assert.notNull(uri, "Namespace definition cannot supply a null URI");
        final NamespaceHandler namespaceHandler = namespaceHandlerResolver.resolve(uri);
        if (namespaceHandler == null) {
            throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri, new Location(readerContext.getResource())));
        }
        namespaceHandlers.put(namespace, namespaceHandler);
        namespaces.put(namespace, uri);
    }
}
Also used : BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) NamespaceHandler(org.springframework.beans.factory.xml.NamespaceHandler) Problem(org.springframework.beans.factory.parsing.Problem) GString(groovy.lang.GString) Map(java.util.Map) HashMap(java.util.HashMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Location(org.springframework.beans.factory.parsing.Location)

Example 3 with Problem

use of org.springframework.beans.factory.parsing.Problem in project grails-core by grails.

the class BeanBuilder method loadBeans.

/**
     * Loads a set of given beans
     * @param resources The resources to load
     * @throws IOException Thrown if there is an error reading one of the passes resources
     */
public void loadBeans(Resource[] resources) {
    @SuppressWarnings("rawtypes") Closure beans = new Closure(this) {

        private static final long serialVersionUID = -2778328821635253740L;

        @Override
        public Object call(Object... args) {
            invokeBeanDefiningClosure((Closure) args[0]);
            return null;
        }
    };
    Binding b = new Binding() {

        @Override
        public void setVariable(String name, Object value) {
            if (currentBeanConfig == null) {
                super.setVariable(name, value);
            } else {
                setPropertyOnBeanConfig(name, value);
            }
        }
    };
    b.setVariable("beans", beans);
    for (Resource resource : resources) {
        try {
            GroovyShell shell = classLoader == null ? new GroovyShell(b) : new GroovyShell(classLoader, b);
            shell.evaluate(new InputStreamReader(resource.getInputStream(), "UTF-8"));
        } catch (Throwable e) {
            throw new BeanDefinitionParsingException(new Problem("Error evaluating bean definition script: " + e.getMessage(), new Location(resource), null, e));
        }
    }
}
Also used : Binding(groovy.lang.Binding) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) Closure(groovy.lang.Closure) InputStreamReader(java.io.InputStreamReader) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) GroovyObject(groovy.lang.GroovyObject) Problem(org.springframework.beans.factory.parsing.Problem) GString(groovy.lang.GString) GroovyShell(groovy.lang.GroovyShell) Location(org.springframework.beans.factory.parsing.Location)

Example 4 with Problem

use of org.springframework.beans.factory.parsing.Problem in project spring-framework by spring-projects.

the class GroovyBeanDefinitionReader method xmlns.

/**
	 * Define a Spring XML namespace definition to use.
	 * @param definition the namespace definition
	 */
public void xmlns(Map<String, String> definition) {
    if (!definition.isEmpty()) {
        for (Map.Entry<String, String> entry : definition.entrySet()) {
            String namespace = entry.getKey();
            String uri = entry.getValue();
            if (uri == null) {
                throw new IllegalArgumentException("Namespace definition must supply a non-null URI");
            }
            NamespaceHandler namespaceHandler = this.groovyDslXmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri);
            if (namespaceHandler == null) {
                throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri, new Location(new DescriptiveResource(("Groovy")))));
            }
            this.namespaces.put(namespace, uri);
        }
    }
}
Also used : BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) NamespaceHandler(org.springframework.beans.factory.xml.NamespaceHandler) DescriptiveResource(org.springframework.core.io.DescriptiveResource) Problem(org.springframework.beans.factory.parsing.Problem) GString(groovy.lang.GString) HashMap(java.util.HashMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Location(org.springframework.beans.factory.parsing.Location)

Aggregations

GString (groovy.lang.GString)4 BeanDefinitionParsingException (org.springframework.beans.factory.parsing.BeanDefinitionParsingException)4 Location (org.springframework.beans.factory.parsing.Location)4 Problem (org.springframework.beans.factory.parsing.Problem)4 Binding (groovy.lang.Binding)2 Closure (groovy.lang.Closure)2 GroovyObject (groovy.lang.GroovyObject)2 GroovyShell (groovy.lang.GroovyShell)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ManagedMap (org.springframework.beans.factory.support.ManagedMap)2 NamespaceHandler (org.springframework.beans.factory.xml.NamespaceHandler)2 InputStreamReader (java.io.InputStreamReader)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 DescriptiveResource (org.springframework.core.io.DescriptiveResource)1 Resource (org.springframework.core.io.Resource)1