Java Code Examples for org.springframework.core.io.support.EncodedResource#getResource()

The following examples show how to use org.springframework.core.io.support.EncodedResource#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YamlPropertyLoaderFactory.java    From magic-starter with GNU Lesser General Public License v3.0 8 votes vote down vote up
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException {
	if (encodedResource == null) {
		return emptyPropertySource(name);
	}
	Resource resource = encodedResource.getResource();
	String fileName = resource.getFilename();
	List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(fileName, resource);
	if (sources.isEmpty()) {
		return emptyPropertySource(fileName);
	}
	// yaml 数据存储,合成一个 PropertySource
	Map<String, Object> ymlDataMap = new HashMap<>(32);
	for (PropertySource<?> source : sources) {
		ymlDataMap.putAll(((MapPropertySource) source).getSource());
	}
	return new OriginTrackedMapPropertySource(getSourceName(fileName, name), ymlDataMap);
}
 
Example 2
Source File: PropertiesBeanDefinitionReader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Load bean definitions from the specified properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, String prefix)
		throws BeanDefinitionStoreException {

	Properties props = new Properties();
	try {
		InputStream is = encodedResource.getResource().getInputStream();
		try {
			if (encodedResource.getEncoding() != null) {
				getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
			}
			else {
				getPropertiesPersister().load(props, is);
			}
		}
		finally {
			is.close();
		}
		return registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("Could not parse properties from " + encodedResource.getResource(), ex);
	}
}
 
Example 3
Source File: PropertiesBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Load bean definitions from the specified properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, String prefix)
		throws BeanDefinitionStoreException {

	Properties props = new Properties();
	try {
		InputStream is = encodedResource.getResource().getInputStream();
		try {
			if (encodedResource.getEncoding() != null) {
				getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
			}
			else {
				getPropertiesPersister().load(props, is);
			}
		}
		finally {
			is.close();
		}
		return registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("Could not parse properties from " + encodedResource.getResource(), ex);
	}
}
 
Example 4
Source File: PropertiesBeanDefinitionReader.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * Load bean definitions from the specified properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, String prefix)
		throws BeanDefinitionStoreException {

	Properties props = new Properties();
	try {
		InputStream is = encodedResource.getResource().getInputStream();
		try {
			if (encodedResource.getEncoding() != null) {
				getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
			}
			else {
				getPropertiesPersister().load(props, is);
			}
		}
		finally {
			is.close();
		}
		return registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("Could not parse properties from " + encodedResource.getResource(), ex);
	}
}
 
Example 5
Source File: ReloadablePropertySourceFactory.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public PropertySource<?> createPropertySource(String s, EncodedResource encodedResource) throws IOException {
    Resource internal = encodedResource.getResource();
    if (internal instanceof FileSystemResource) {
        return new ReloadablePropertySource(s, ((FileSystemResource) internal).getPath());
    }
    if (internal instanceof FileUrlResource) {
        return new ReloadablePropertySource(s, ((FileUrlResource) internal)
          .getURL()
          .getPath());
    }
    return super.createPropertySource(s, encodedResource);
}
 
Example 6
Source File: PropertiesBeanDefinitionReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Load bean definitions from the specified properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, @Nullable String prefix)
		throws BeanDefinitionStoreException {

	if (logger.isTraceEnabled()) {
		logger.trace("Loading properties bean definitions from " + encodedResource);
	}

	Properties props = new Properties();
	try {
		try (InputStream is = encodedResource.getResource().getInputStream()) {
			if (encodedResource.getEncoding() != null) {
				getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
			}
			else {
				getPropertiesPersister().load(props, is);
			}
		}

		int count = registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
		if (logger.isDebugEnabled()) {
			logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
		}
		return count;
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("Could not parse properties from " + encodedResource.getResource(), ex);
	}
}
 
Example 7
Source File: ConnectorResourcePropertySource.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ConnectorResourcePropertySource(String name, EncodedResource source, Map<String, String> aliases) {
	super(name, source);
	this.aliases.putAll(aliases);

	EncodedResource encoded = (EncodedResource) this.getSource();
	AbstractResource resource = (AbstractResource) encoded.getResource();
	String path = resource.getFilename();

	if (StringUtils.isBlank(path)) {
		return;
	}

	String[] values = path.split(":");
	if (values.length != 2) {
		return;
	}

	String protocol = values[0];
	String resName = values[1];
	if ("bytejta".equalsIgnoreCase(protocol) == false) {
		return;
	} else if ("connector.config".equalsIgnoreCase(resName) == false) {
		return;
	}

	this.enabled = true;
}
 
Example 8
Source File: TransactionPropertySource.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public TransactionPropertySource(String name, EncodedResource source) {
	super(name, source);

	EncodedResource encoded = (EncodedResource) this.getSource();
	AbstractResource resource = (AbstractResource) encoded.getResource();
	String path = resource.getFilename();

	if (StringUtils.isBlank(path)) {
		return;
	}

	String[] values = path.split(":");
	if (values.length != 2) {
		return;
	}

	String protocol = values[0];
	String resName = values[1];
	if ("bytejta".equalsIgnoreCase(protocol) == false) {
		return;
	} else if ("loadbalancer.config".equalsIgnoreCase(resName) == false) {
		return;
	}

	this.enabled = true;

}
 
Example 9
Source File: CompensablePropertySource.java    From ByteTCC with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CompensablePropertySource(String name, EncodedResource source) {
	super(name, source);

	EncodedResource encoded = (EncodedResource) this.getSource();
	AbstractResource resource = (AbstractResource) encoded.getResource();
	String path = resource.getFilename();

	if (StringUtils.isBlank(path)) {
		return;
	}

	String[] values = path.split(":");
	if (values.length != 2) {
		return;
	}

	String protocol = values[0];
	String resName = values[1];
	if ("bytetcc".equalsIgnoreCase(protocol) == false) {
		return;
	} else if ("loadbalancer.config".equalsIgnoreCase(resName) == false) {
		return;
	}

	this.enabled = true;

}
 
Example 10
Source File: XmlBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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<EncodedResource>(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();
		}
	}
}
 
Example 11
Source File: GroovyBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: GroovyBeanDefinitionReader.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * Load bean definitions from the specified Groovy script.
 * @param encodedResource the resource descriptor for the Groovy script,
 * 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 {
	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(), encodedResource.getResource().getFilename());
	}
	catch (Throwable ex) {
		throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
				new Location(encodedResource.getResource()), null, ex));
	}
	return getRegistry().getBeanDefinitionCount() - countBefore;
}
 
Example 13
Source File: XmlBeanDefinitionReader.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * 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<EncodedResource>(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();
		}
	}
}
 
Example 14
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: XmlBeanDefinitionReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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<EncodedResource>(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();
		}
	}
}
 
Example 16
Source File: XmlBeanDefinitionReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 == 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();
		}
	}
}
 
Example 17
Source File: PropertiesBeanDefinitionReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Load bean definitions from the specified properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, @Nullable String prefix)
		throws BeanDefinitionStoreException {

	if (logger.isTraceEnabled()) {
		logger.trace("Loading properties bean definitions from " + encodedResource);
	}

	Properties props = new Properties();
	try {
		try (InputStream is = encodedResource.getResource().getInputStream()) {
			if (encodedResource.getEncoding() != null) {
				getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
			}
			else {
				getPropertiesPersister().load(props, is);
			}
		}

		int count = registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
		if (logger.isDebugEnabled()) {
			logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
		}
		return count;
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("Could not parse properties from " + encodedResource.getResource(), ex);
	}
}
 
Example 18
Source File: GroovyBeanDefinitionReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * 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);
	}

	if (logger.isTraceEnabled()) {
		logger.trace("Loading Groovy bean definitions from " + encodedResource);
	}

	Closure beans = new Closure(this) {
		@Override
		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(getBeanClassLoader(), 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));
	}

	int count = getRegistry().getBeanDefinitionCount() - countBefore;
	if (logger.isDebugEnabled()) {
		logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
	}
	return count;
}
 
Example 19
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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);
	}

	if (logger.isTraceEnabled()) {
		logger.trace("Loading Groovy bean definitions from " + encodedResource);
	}

	Closure beans = new Closure(this) {
		@Override
		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(getBeanClassLoader(), 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));
	}

	int count = getRegistry().getBeanDefinitionCount() - countBefore;
	if (logger.isDebugEnabled()) {
		logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
	}
	return count;
}
 
Example 20
Source File: XmlBeanDefinitionReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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 == 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 {
		// 注释 1.7 从资源文件中获取输入流
		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();
		}
	}
}