org.springframework.beans.PropertyValue Java Examples

The following examples show how to use org.springframework.beans.PropertyValue. 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
/**
 * Parses the given XML element containing the properties of the cache manager
 * to register in the given registry of bean definitions.
 * 
 * @param element
 *          the XML element to parse
 * @param registry
 *          the registry of bean definitions
 * 
 * @see AbstractCacheProviderFacadeParser#doParse(String, Element,
 *      BeanDefinitionRegistry)
 */
protected final void doParse(String cacheProviderFacadeId, Element element,
    BeanDefinitionRegistry registry) {
  String id = "cacheManager";
  Class clazz = getCacheManagerClass();
  RootBeanDefinition cacheManager = new RootBeanDefinition(clazz);
  MutablePropertyValues cacheManagerProperties = new MutablePropertyValues();
  cacheManager.setPropertyValues(cacheManagerProperties);

  PropertyValue configLocation = parseConfigLocationProperty(element);
  cacheManagerProperties.addPropertyValue(configLocation);
  registry.registerBeanDefinition(id, cacheManager);

  BeanDefinition cacheProviderFacade = registry
      .getBeanDefinition(cacheProviderFacadeId);
  cacheProviderFacade.getPropertyValues().addPropertyValue("cacheManager",
      new RuntimeBeanReference(id));
}
 
Example #2
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
	BeanDefinitionHolder holder = (BeanDefinitionHolder) source;
	BeanDefinition definition = holder.getBeanDefinition();
	writer.addAttribute("class", definition.getBeanClassName());
	writer.addAttribute("name", holder.getBeanName());
	for (PropertyValue property : definition.getPropertyValues().getPropertyValueList()) {
		writer.startNode("property");
		writer.addAttribute("name", property.getName());
		if (property.getValue().getClass().equals(TypedStringValue.class)) {
			context.convertAnother(property.getValue());
		} else {
			writeItem(property.getValue(), context, writer);
		}
		writer.endNode();
	}
}
 
Example #3
Source Project: spring-analysis-note   Author: Vip-Augus   File: DataBinderFieldAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void bindingNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
	pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue"));

	binder.bind(pvs);
	binder.close();

	assertTrue("changed name correctly", rod.getName().equals("Rod"));
	assertTrue("changed age correctly", rod.getAge() == 32);

	Map<?, ?> m = binder.getBindingResult().getModel();
	assertTrue("There is one element in map", m.size() == 2);
	FieldAccessBean tb = (FieldAccessBean) m.get("person");
	assertTrue("Same object", tb.equals(rod));
}
 
Example #4
Source Project: spring-analysis-note   Author: Vip-Augus   File: DataBinderFieldAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void nestedBindingWithDefaultConversionNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
	pvs.addPropertyValue(new PropertyValue("spouse.jedi", "on"));

	binder.bind(pvs);
	binder.close();

	assertEquals("Kerry", rod.getSpouse().getName());
	assertTrue((rod.getSpouse()).isJedi());
}
 
Example #5
Source Project: spring-analysis-note   Author: Vip-Augus   File: AspectJAutoProxyCreatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() {
	ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");

	GenericApplicationContext childAc = new GenericApplicationContext(ac);
	// Create a child factory with a bean that should be woven
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.getPropertyValues().addPropertyValue(new PropertyValue("name", "Adrian"))
			.addPropertyValue(new PropertyValue("age", 34));
	childAc.registerBeanDefinition("adrian2", bd);
	// Register the advisor auto proxy creator with subclass
	childAc.registerBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class.getName(), new RootBeanDefinition(
			AnnotationAwareAspectJAutoProxyCreator.class));
	childAc.refresh();

	ITestBean beanFromChildContextThatShouldBeWeaved = (ITestBean) childAc.getBean("adrian2");
	//testAspectsAndAdvisorAreApplied(childAc, (ITestBean) ac.getBean("adrian"));
	doTestAspectsAndAdvisorAreApplied(childAc, beanFromChildContextThatShouldBeWeaved);
}
 
Example #6
Source Project: spring-analysis-note   Author: Vip-Augus   File: HttpServletBean.java    License: MIT License 6 votes vote down vote up
/**
 * Create new ServletConfigPropertyValues.
 * @param config the ServletConfig we'll use to take PropertyValues from
 * @param requiredProperties set of property names we need, where
 * we can't accept default values
 * @throws ServletException if any required properties are missing
 */
public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)
		throws ServletException {

	Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
			new HashSet<>(requiredProperties) : null);

	Enumeration<String> paramNames = config.getInitParameterNames();
	while (paramNames.hasMoreElements()) {
		String property = paramNames.nextElement();
		Object value = config.getInitParameter(property);
		addPropertyValue(new PropertyValue(property, value));
		if (missingProps != null) {
			missingProps.remove(property);
		}
	}

	// Fail if we are still missing properties.
	if (!CollectionUtils.isEmpty(missingProps)) {
		throw new ServletException(
				"Initialization from ServletConfig for servlet '" + config.getServletName() +
				"' failed; the following required properties were missing: " +
				StringUtils.collectionToDelimitedString(missingProps, ", "));
	}
}
 
Example #7
Source Project: spring4-understanding   Author: langtianya   File: ServletRequestDataBinderTests.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Must contain: forname=Tony surname=Blair age=50
 */
protected void doTestTony(PropertyValues pvs) throws Exception {
	assertTrue("Contains 3", pvs.getPropertyValues().length == 3);
	assertTrue("Contains forname", pvs.contains("forname"));
	assertTrue("Contains surname", pvs.contains("surname"));
	assertTrue("Contains age", pvs.contains("age"));
	assertTrue("Doesn't contain tory", !pvs.contains("tory"));

	PropertyValue[] ps = pvs.getPropertyValues();
	Map<String, String> m = new HashMap<String, String>();
	m.put("forname", "Tony");
	m.put("surname", "Blair");
	m.put("age", "50");
	for (int i = 0; i < ps.length; i++) {
		Object val = m.get(ps[i].getName());
		assertTrue("Can't have unexpected value", val != null);
		assertTrue("Val i string", val instanceof String);
		assertTrue("val matches expected", val.equals(ps[i].getValue()));
		m.remove(ps[i].getName());
	}
	assertTrue("Map size is 0", m.size() == 0);
}
 
Example #8
Source Project: AlgoTrader   Author: curtiszimmerman   File: GrailsDataBinder.java    License: GNU General Public License v2.0 6 votes vote down vote up
private PropertyValues filterPropertyValues(PropertyValues propertyValues, String prefix) {
	if (prefix == null || prefix.length() == 0)
		return propertyValues;

	PropertyValue[] valueArray = propertyValues.getPropertyValues();
	MutablePropertyValues newValues = new MutablePropertyValues();
	for (PropertyValue propertyValue : valueArray) {
		String name = propertyValue.getName();
		final String prefixWithDot = prefix + PREFIX_SEPERATOR;
		if (name.startsWith(prefixWithDot)) {
			name = name.substring(prefixWithDot.length(), name.length());
			newValues.addPropertyValue(name, propertyValue.getValue());
		}
	}
	return newValues;
}
 
Example #9
Source Project: spring-analysis-note   Author: Vip-Augus   File: WebDataBinder.java    License: MIT License 6 votes vote down vote up
/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	String fieldMarkerPrefix = getFieldMarkerPrefix();
	if (fieldMarkerPrefix != null) {
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldMarkerPrefix)) {
				String field = pv.getName().substring(fieldMarkerPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
					mpvs.add(field, getEmptyValue(field, fieldType));
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example #10
Source Project: spring-analysis-note   Author: Vip-Augus   File: GenericFilterBean.java    License: MIT License 6 votes vote down vote up
/**
 * Create new FilterConfigPropertyValues.
 * @param config the FilterConfig we'll use to take PropertyValues from
 * @param requiredProperties set of property names we need, where
 * we can't accept default values
 * @throws ServletException if any required properties are missing
 */
public FilterConfigPropertyValues(FilterConfig config, Set<String> requiredProperties)
		throws ServletException {

	Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
			new HashSet<>(requiredProperties) : null);

	Enumeration<String> paramNames = config.getInitParameterNames();
	while (paramNames.hasMoreElements()) {
		String property = paramNames.nextElement();
		Object value = config.getInitParameter(property);
		addPropertyValue(new PropertyValue(property, value));
		if (missingProps != null) {
			missingProps.remove(property);
		}
	}

	// Fail if we are still missing properties.
	if (!CollectionUtils.isEmpty(missingProps)) {
		throw new ServletException(
				"Initialization from FilterConfig for filter '" + config.getFilterName() +
				"' failed; the following required properties were missing: " +
				StringUtils.collectionToDelimitedString(missingProps, ", "));
	}
}
 
Example #11
Source Project: spring-analysis-note   Author: Vip-Augus   File: WebRequestDataBinderTests.java    License: MIT License 6 votes vote down vote up
/**
 * Must contain: forname=Tony surname=Blair age=50
 */
protected void doTestTony(PropertyValues pvs) throws Exception {
	assertTrue("Contains 3", pvs.getPropertyValues().length == 3);
	assertTrue("Contains forname", pvs.contains("forname"));
	assertTrue("Contains surname", pvs.contains("surname"));
	assertTrue("Contains age", pvs.contains("age"));
	assertTrue("Doesn't contain tory", !pvs.contains("tory"));

	PropertyValue[] pvArray = pvs.getPropertyValues();
	Map<String, String> m = new HashMap<>();
	m.put("forname", "Tony");
	m.put("surname", "Blair");
	m.put("age", "50");
	for (PropertyValue pv : pvArray) {
		Object val = m.get(pv.getName());
		assertTrue("Can't have unexpected value", val != null);
		assertTrue("Val i string", val instanceof String);
		assertTrue("val matches expected", val.equals(pv.getValue()));
		m.remove(pv.getName());
	}
	assertTrue("Map size is 0", m.size() == 0);
}
 
Example #12
Source Project: spring4-understanding   Author: langtianya   File: DefaultListableBeanFactoryTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testExtensiveCircularReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	for (int i = 0; i < 1000; i++) {
		MutablePropertyValues pvs = new MutablePropertyValues();
		pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
		RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
		bd.setPropertyValues(pvs);
		lbf.registerBeanDefinition("bean" + i, bd);
	}
	lbf.preInstantiateSingletons();
	for (int i = 0; i < 1000; i++) {
		TestBean bean = (TestBean) lbf.getBean("bean" + i);
		TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0));
		assertTrue(bean.getSpouse() == otherBean);
	}
}
 
Example #13
Source Project: spring4-understanding   Author: langtianya   File: WebDataBinder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	if (getFieldMarkerPrefix() != null) {
		String fieldMarkerPrefix = getFieldMarkerPrefix();
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldMarkerPrefix)) {
				String field = pv.getName().substring(fieldMarkerPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
					mpvs.add(field, getEmptyValue(field, fieldType));
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example #14
Source Project: spring-analysis-note   Author: Vip-Augus   File: CustomEditorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testComplexObject() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));
	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
}
 
Example #15
Source Project: spring4-understanding   Author: langtianya   File: CustomEditorTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testComplexObjectWithOldValueAccess() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.setExtractOldValueForEditor(true);
	bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));

	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
	ITestBean spouse = tb.getSpouse();

	bw.setPropertyValues(pvs);
	assertSame("Should have remained same object", spouse, tb.getSpouse());
}
 
Example #16
Source Project: spring-analysis-note   Author: Vip-Augus   File: DefaultListableBeanFactoryTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testExtensiveCircularReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	for (int i = 0; i < 1000; i++) {
		MutablePropertyValues pvs = new MutablePropertyValues();
		pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
		RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
		bd.setPropertyValues(pvs);
		lbf.registerBeanDefinition("bean" + i, bd);
	}
	lbf.preInstantiateSingletons();
	for (int i = 0; i < 1000; i++) {
		TestBean bean = (TestBean) lbf.getBean("bean" + i);
		TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0));
		assertTrue(bean.getSpouse() == otherBean);
	}
}
 
Example #17
Source Project: AlgoTrader   Author: curtiszimmerman   File: GrailsDataBinder.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void filterBlankValuesWhenTargetIsNullable(MutablePropertyValues mpvs) {
	Object target = getTarget();
	Map constrainedProperties = resolveConstrainedProperties(target, this.domainClass);
	if (constrainedProperties == null) {
		return;
	}

	PropertyValue[] valueArray = mpvs.getPropertyValues();
	for (PropertyValue propertyValue : valueArray) {
		if (BLANK.equals(propertyValue.getValue())) {
			ConstrainedProperty cp = getConstrainedPropertyForPropertyValue(constrainedProperties, propertyValue);
			if (shouldNullifyBlankString(propertyValue, cp)) {
				propertyValue.setConvertedValue(null);
			}
		}
	}
}
 
Example #18
Source Project: spring4-understanding   Author: langtianya   File: BeanDefinitionParserDelegate.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parse a property element.
 */
public void parsePropertyElement(Element ele, BeanDefinition bd) {
	String propertyName = ele.getAttribute(NAME_ATTRIBUTE);
	if (!StringUtils.hasLength(propertyName)) {
		error("Tag 'property' must have a 'name' attribute", ele);
		return;
	}
	this.parseState.push(new PropertyEntry(propertyName));
	try {
		if (bd.getPropertyValues().contains(propertyName)) {
			error("Multiple 'property' definitions for property '" + propertyName + "'", ele);
			return;
		}
		Object val = parsePropertyValue(ele, bd, propertyName);
		PropertyValue pv = new PropertyValue(propertyName, val);
		parseMetaElements(ele, pv);
		pv.setSource(extractSource(ele));
		bd.getPropertyValues().addPropertyValue(pv);
	}
	finally {
		this.parseState.pop();
	}
}
 
Example #19
Source Project: java-technology-stack   Author: codeEngraver   File: DataBinderFieldAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void bindingNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
	pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue"));

	binder.bind(pvs);
	binder.close();

	assertTrue("changed name correctly", rod.getName().equals("Rod"));
	assertTrue("changed age correctly", rod.getAge() == 32);

	Map<?, ?> m = binder.getBindingResult().getModel();
	assertTrue("There is one element in map", m.size() == 2);
	FieldAccessBean tb = (FieldAccessBean) m.get("person");
	assertTrue("Same object", tb.equals(rod));
}
 
Example #20
Source Project: spring4-understanding   Author: langtianya   File: GenericFilterBean.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create new FilterConfigPropertyValues.
 * @param config FilterConfig we'll use to take PropertyValues from
 * @param requiredProperties set of property names we need, where
 * we can't accept default values
 * @throws ServletException if any required properties are missing
 */
public FilterConfigPropertyValues(FilterConfig config, Set<String> requiredProperties)
	throws ServletException {

	Set<String> missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ?
			new HashSet<String>(requiredProperties) : null;

	Enumeration<?> en = config.getInitParameterNames();
	while (en.hasMoreElements()) {
		String property = (String) en.nextElement();
		Object value = config.getInitParameter(property);
		addPropertyValue(new PropertyValue(property, value));
		if (missingProps != null) {
			missingProps.remove(property);
		}
	}

	// Fail if we are still missing properties.
	if (missingProps != null && missingProps.size() > 0) {
		throw new ServletException(
			"Initialization from FilterConfig for filter '" + config.getFilterName() +
			"' failed; the following required properties were missing: " +
			StringUtils.collectionToDelimitedString(missingProps, ", "));
	}
}
 
Example #21
Source Project: java-technology-stack   Author: codeEngraver   File: AspectJAutoProxyCreatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() {
	ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");

	GenericApplicationContext childAc = new GenericApplicationContext(ac);
	// Create a child factory with a bean that should be woven
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.getPropertyValues().addPropertyValue(new PropertyValue("name", "Adrian"))
			.addPropertyValue(new PropertyValue("age", 34));
	childAc.registerBeanDefinition("adrian2", bd);
	// Register the advisor auto proxy creator with subclass
	childAc.registerBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class.getName(), new RootBeanDefinition(
			AnnotationAwareAspectJAutoProxyCreator.class));
	childAc.refresh();

	ITestBean beanFromChildContextThatShouldBeWeaved = (ITestBean) childAc.getBean("adrian2");
	//testAspectsAndAdvisorAreApplied(childAc, (ITestBean) ac.getBean("adrian"));
	doTestAspectsAndAdvisorAreApplied(childAc, beanFromChildContextThatShouldBeWeaved);
}
 
Example #22
Source Project: java-technology-stack   Author: codeEngraver   File: HttpServletBean.java    License: MIT License 6 votes vote down vote up
/**
 * Create new ServletConfigPropertyValues.
 * @param config the ServletConfig we'll use to take PropertyValues from
 * @param requiredProperties set of property names we need, where
 * we can't accept default values
 * @throws ServletException if any required properties are missing
 */
public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)
		throws ServletException {

	Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
			new HashSet<>(requiredProperties) : null);

	Enumeration<String> paramNames = config.getInitParameterNames();
	while (paramNames.hasMoreElements()) {
		String property = paramNames.nextElement();
		Object value = config.getInitParameter(property);
		addPropertyValue(new PropertyValue(property, value));
		if (missingProps != null) {
			missingProps.remove(property);
		}
	}

	// Fail if we are still missing properties.
	if (!CollectionUtils.isEmpty(missingProps)) {
		throw new ServletException(
				"Initialization from ServletConfig for servlet '" + config.getServletName() +
				"' failed; the following required properties were missing: " +
				StringUtils.collectionToDelimitedString(missingProps, ", "));
	}
}
 
Example #23
Source Project: spring4-understanding   Author: langtianya   File: CustomEditorTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testComplexObject() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));
	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
}
 
Example #24
Source Project: blog_demos   Author: zq2599   File: BeanDefinitionParserDelegate.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parse a property element.
 */
public void parsePropertyElement(Element ele, BeanDefinition bd) {
	String propertyName = ele.getAttribute(NAME_ATTRIBUTE);
	if (!StringUtils.hasLength(propertyName)) {
		error("Tag 'property' must have a 'name' attribute", ele);
		return;
	}
	this.parseState.push(new PropertyEntry(propertyName));
	try {
		if (bd.getPropertyValues().contains(propertyName)) {
			error("Multiple 'property' definitions for property '" + propertyName + "'", ele);
			return;
		}
		Object val = parsePropertyValue(ele, bd, propertyName);
		PropertyValue pv = new PropertyValue(propertyName, val);
		parseMetaElements(ele, pv);
		pv.setSource(extractSource(ele));
		bd.getPropertyValues().addPropertyValue(pv);
	}
	finally {
		this.parseState.pop();
	}
}
 
Example #25
Source Project: spring4-understanding   Author: langtianya   File: BeanComponentDefinition.java    License: Apache License 2.0 6 votes vote down vote up
private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
	List<BeanDefinition> innerBeans = new ArrayList<BeanDefinition>();
	List<BeanReference> references = new ArrayList<BeanReference>();
	PropertyValues propertyValues = beanDefinition.getPropertyValues();
	for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
		PropertyValue propertyValue = propertyValues.getPropertyValues()[i];
		Object value = propertyValue.getValue();
		if (value instanceof BeanDefinitionHolder) {
			innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
		}
		else if (value instanceof BeanDefinition) {
			innerBeans.add((BeanDefinition) value);
		}
		else if (value instanceof BeanReference) {
			references.add((BeanReference) value);
		}
	}
	this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
	this.beanReferences = references.toArray(new BeanReference[references.size()]);
}
 
Example #26
Source Project: java-technology-stack   Author: codeEngraver   File: WebDataBinder.java    License: MIT License 6 votes vote down vote up
/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	String fieldMarkerPrefix = getFieldMarkerPrefix();
	if (fieldMarkerPrefix != null) {
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldMarkerPrefix)) {
				String field = pv.getName().substring(fieldMarkerPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
					mpvs.add(field, getEmptyValue(field, fieldType));
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example #27
Source Project: spring4-understanding   Author: langtianya   File: DataBinderFieldAccessTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void bindingNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
	pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue"));

	binder.bind(pvs);
	binder.close();

	assertTrue("changed name correctly", rod.getName().equals("Rod"));
	assertTrue("changed age correctly", rod.getAge() == 32);

	Map<?, ?> m = binder.getBindingResult().getModel();
	assertTrue("There is one element in map", m.size() == 2);
	FieldAccessBean tb = (FieldAccessBean) m.get("person");
	assertTrue("Same object", tb.equals(rod));
}
 
Example #28
Source Project: Mapper   Author: abel533   File: MapperScannerConfigurer.java    License: MIT License 6 votes vote down vote up
private String updatePropertyValue(String propertyName, PropertyValues values) {
    PropertyValue property = values.getPropertyValue(propertyName);

    if (property == null) {
        return null;
    }

    Object value = property.getValue();

    if (value == null) {
        return null;
    } else if (value instanceof String) {
        return value.toString();
    } else if (value instanceof TypedStringValue) {
        return ((TypedStringValue) value).getValue();
    } else {
        return null;
    }
}
 
Example #29
Source Project: java-technology-stack   Author: codeEngraver   File: ServletRequestDataBinderTests.java    License: MIT License 6 votes vote down vote up
/**
 * Must contain: forname=Tony surname=Blair age=50
 */
protected void doTestTony(PropertyValues pvs) throws Exception {
	assertTrue("Contains 3", pvs.getPropertyValues().length == 3);
	assertTrue("Contains forname", pvs.contains("forname"));
	assertTrue("Contains surname", pvs.contains("surname"));
	assertTrue("Contains age", pvs.contains("age"));
	assertTrue("Doesn't contain tory", !pvs.contains("tory"));

	PropertyValue[] ps = pvs.getPropertyValues();
	Map<String, String> m = new HashMap<>();
	m.put("forname", "Tony");
	m.put("surname", "Blair");
	m.put("age", "50");
	for (int i = 0; i < ps.length; i++) {
		Object val = m.get(ps[i].getName());
		assertTrue("Can't have unexpected value", val != null);
		assertTrue("Val i string", val instanceof String);
		assertTrue("val matches expected", val.equals(ps[i].getValue()));
		m.remove(ps[i].getName());
	}
	assertTrue("Map size is 0", m.size() == 0);
}
 
Example #30
Source Project: java-technology-stack   Author: codeEngraver   File: BeanComponentDefinition.java    License: MIT License 6 votes vote down vote up
/**
 * Create a new BeanComponentDefinition for the given bean.
 * @param beanDefinitionHolder the BeanDefinitionHolder encapsulating
 * the bean definition as well as the name of the bean
 */
public BeanComponentDefinition(BeanDefinitionHolder beanDefinitionHolder) {
	super(beanDefinitionHolder);

	List<BeanDefinition> innerBeans = new ArrayList<>();
	List<BeanReference> references = new ArrayList<>();
	PropertyValues propertyValues = beanDefinitionHolder.getBeanDefinition().getPropertyValues();
	for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
		Object value = propertyValue.getValue();
		if (value instanceof BeanDefinitionHolder) {
			innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
		}
		else if (value instanceof BeanDefinition) {
			innerBeans.add((BeanDefinition) value);
		}
		else if (value instanceof BeanReference) {
			references.add((BeanReference) value);
		}
	}
	this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[0]);
	this.beanReferences = references.toArray(new BeanReference[0]);
}