Java Code Examples for org.springframework.core.io.DescriptiveResource

The following examples show how to use org.springframework.core.io.DescriptiveResource. These examples are extracted from open source projects. 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
@Test
public void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	beanFactory.registerBeanDefinition("bar", rbd);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	beanFactory.setAllowBeanDefinitionOverriding(false);
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	try {
		pp.postProcessBeanFactory(beanFactory);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		assertTrue(ex.getMessage().contains("bar"));
		assertTrue(ex.getMessage().contains("SingletonBeanConfig"));
		assertTrue(ex.getMessage().contains(TestBean.class.getName()));
	}
}
 
Example 2
Source Project: spring-analysis-note   Source File: GroovyBeanDefinitionReader.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example 3
Source Project: spring-analysis-note   Source File: GroovyBeanDefinitionReader.java    License: MIT License 6 votes vote down vote up
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
	XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(new DescriptiveResource(
		"Groovy"));
	BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
	boolean decorating = (this.currentBeanDefinition != null);
	if (!decorating) {
		this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
	}
	return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {
		@Override
		protected void afterInvocation() {
			if (!this.decorating) {
				currentBeanDefinition = null;
			}
		}
	};
}
 
Example 4
@Test
public void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	beanFactory.registerBeanDefinition("bar", rbd);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	beanFactory.setAllowBeanDefinitionOverriding(false);
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	try {
		pp.postProcessBeanFactory(beanFactory);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		assertTrue(ex.getMessage().contains("bar"));
		assertTrue(ex.getMessage().contains("SingletonBeanConfig"));
		assertTrue(ex.getMessage().contains(TestBean.class.getName()));
	}
}
 
Example 5
Source Project: java-technology-stack   Source File: GroovyBeanDefinitionReader.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example 6
Source Project: java-technology-stack   Source File: GroovyBeanDefinitionReader.java    License: MIT License 6 votes vote down vote up
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
	XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(new DescriptiveResource(
		"Groovy"));
	BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
	boolean decorating = (this.currentBeanDefinition != null);
	if (!decorating) {
		this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
	}
	return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {
		@Override
		protected void afterInvocation() {
			if (!this.decorating) {
				currentBeanDefinition = null;
			}
		}
	};
}
 
Example 7
/**
 * 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);
		}
	}
}
 
Example 8
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
	XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(new DescriptiveResource(
		"Groovy"));
	BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
	boolean decorating = (this.currentBeanDefinition != null);
	if (!decorating) {
		this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
	}
	return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {
		@Override
		protected void afterInvocation() {
			if (!this.decorating) {
				currentBeanDefinition = null;
			}
		}
	};
}
 
Example 9
Source Project: blog_demos   Source File: GroovyBeanDefinitionReader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Define a Spring 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.xmlBeanDefinitionReader.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);
		}
	}
}
 
Example 10
@Test
public void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	beanFactory.registerBeanDefinition("bar", rbd);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	beanFactory.setAllowBeanDefinitionOverriding(false);
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	try {
		pp.postProcessBeanFactory(beanFactory);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		assertTrue(ex.getMessage().contains("bar"));
		assertTrue(ex.getMessage().contains("SingletonBeanConfig"));
		assertTrue(ex.getMessage().contains(TestBean.class.getName()));
	}
}
 
Example 11
/**
 * 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);
		}
	}
}
 
Example 12
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
	XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(new DescriptiveResource(
		"Groovy"));
	BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
	boolean decorating = (this.currentBeanDefinition != null);
	if (!decorating) {
		this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
	}
	return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {
		@Override
		protected void afterInvocation() {
			if (!this.decorating) {
				currentBeanDefinition = null;
			}
		}
	};
}
 
Example 13
@Test
@SuppressWarnings("deprecation")
public void velocityConfigurerWithCsvPathAndNonFileAccess() throws IOException, VelocityException {
	VelocityConfigurer vc = new VelocityConfigurer();
	vc.setResourceLoaderPath("file:/mydir,file:/yourdir");
	vc.setResourceLoader(new ResourceLoader() {
		@Override
		public Resource getResource(String location) {
			if ("file:/yourdir/test".equals(location)) {
				return new DescriptiveResource("");
			}
			return new ByteArrayResource("test".getBytes(), "test");
		}
		@Override
		public ClassLoader getClassLoader() {
			return getClass().getClassLoader();
		}
	});
	vc.setPreferFileSystemAccess(false);
	vc.afterPropertiesSet();
	assertThat(vc.createVelocityEngine(), instanceOf(VelocityEngine.class));
	VelocityEngine ve = vc.createVelocityEngine();
	assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", Collections.emptyMap()));
}
 
Example 14
Source Project: spring-analysis-note   Source File: ConfigurationClass.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param clazz the underlying {@link Class} to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(Class<?> clazz, String beanName) {
	Assert.notNull(beanName, "Bean name must not be null");
	this.metadata = AnnotationMetadata.introspect(clazz);
	this.resource = new DescriptiveResource(clazz.getName());
	this.beanName = beanName;
}
 
Example 15
Source Project: spring-analysis-note   Source File: ConfigurationClass.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param metadata the metadata for the underlying class to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(AnnotationMetadata metadata, String beanName) {
	Assert.notNull(beanName, "Bean name must not be null");
	this.metadata = metadata;
	this.resource = new DescriptiveResource(metadata.getClassName());
	this.beanName = beanName;
}
 
Example 16
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitions() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	beanFactory.registerBeanDefinition("bar", rbd);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 17
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(
			new BeanDefinitionHolder(rbd, "bar"), beanFactory, true);
	beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition());
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 18
@Test
public void testWarn() throws Exception {
	Problem problem = new Problem("VGER", new Location(new DescriptiveResource("here")),
			null, new IllegalArgumentException());

	Log log = mock(Log.class);

	FailFastProblemReporter reporter = new FailFastProblemReporter();
	reporter.setLogger(log);
	reporter.warning(problem);

	verify(log).warn(any(), isA(IllegalArgumentException.class));
}
 
Example 19
Source Project: java-technology-stack   Source File: ConfigurationClass.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param clazz the underlying {@link Class} to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(Class<?> clazz, String beanName) {
	Assert.notNull(beanName, "Bean name must not be null");
	this.metadata = new StandardAnnotationMetadata(clazz, true);
	this.resource = new DescriptiveResource(clazz.getName());
	this.beanName = beanName;
}
 
Example 20
Source Project: java-technology-stack   Source File: ConfigurationClass.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param metadata the metadata for the underlying class to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(AnnotationMetadata metadata, String beanName) {
	Assert.notNull(beanName, "Bean name must not be null");
	this.metadata = metadata;
	this.resource = new DescriptiveResource(metadata.getClassName());
	this.beanName = beanName;
}
 
Example 21
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitions() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	beanFactory.registerBeanDefinition("bar", rbd);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 22
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(
			new BeanDefinitionHolder(rbd, "bar"), beanFactory, true);
	beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition());
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 23
@Test
public void testWarn() throws Exception {
	Problem problem = new Problem("VGER", new Location(new DescriptiveResource("here")),
			null, new IllegalArgumentException());

	Log log = mock(Log.class);

	FailFastProblemReporter reporter = new FailFastProblemReporter();
	reporter.setLogger(log);
	reporter.warning(problem);

	verify(log).warn(any(), isA(IllegalArgumentException.class));
}
 
Example 24
Source Project: lams   Source File: ConfigurationClass.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param clazz the underlying {@link Class} to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(Class<?> clazz, String beanName) {
	Assert.hasText(beanName, "Bean name must not be null");
	this.metadata = new StandardAnnotationMetadata(clazz, true);
	this.resource = new DescriptiveResource(clazz.getName());
	this.beanName = beanName;
}
 
Example 25
Source Project: lams   Source File: ConfigurationClass.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param metadata the metadata for the underlying class to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(AnnotationMetadata metadata, String beanName) {
	Assert.hasText(beanName, "Bean name must not be null");
	this.metadata = metadata;
	this.resource = new DescriptiveResource(metadata.getClassName());
	this.beanName = beanName;
}
 
Example 26
Source Project: spring4-understanding   Source File: ConfigurationClass.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param clazz the underlying {@link Class} to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(Class<?> clazz, String beanName) {
	Assert.hasText(beanName, "Bean name must not be null");
	this.metadata = new StandardAnnotationMetadata(clazz, true);
	this.resource = new DescriptiveResource(clazz.toString());
	this.beanName = beanName;
}
 
Example 27
Source Project: spring4-understanding   Source File: ConfigurationClass.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClass} with the given name.
 * @param metadata the metadata for the underlying class to represent
 * @param beanName name of the {@code @Configuration} class bean
 * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
 */
public ConfigurationClass(AnnotationMetadata metadata, String beanName) {
	Assert.hasText(beanName, "Bean name must not be null");
	this.metadata = metadata;
	this.resource = new DescriptiveResource(metadata.getClassName());
	this.beanName = beanName;
}
 
Example 28
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitions() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	beanFactory.registerBeanDefinition("bar", rbd);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 29
@Test
public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() {
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setResource(new DescriptiveResource("XML or something"));
	BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(
			new BeanDefinitionHolder(rbd, "bar"), beanFactory, true);
	beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition());
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.getBean("foo", Foo.class);
	beanFactory.getBean("bar", TestBean.class);
}
 
Example 30
@Test
public void testWarn() throws Exception {
	Problem problem = new Problem("VGER", new Location(new DescriptiveResource("here")),
			null, new IllegalArgumentException());

	Log log = mock(Log.class);

	FailFastProblemReporter reporter = new FailFastProblemReporter();
	reporter.setLogger(log);
	reporter.warning(problem);

	verify(log).warn(any(), isA(IllegalArgumentException.class));
}