001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans;
018:
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.List;
023: import java.util.Map;
024:
025: /**
026: * Abstract implementation of the {@link PropertyAccessor} interface.
027: * Provides base implementations of all convenience methods, with the
028: * implementation of actual property access left to subclasses
029: *
030: * @author Juergen Hoeller
031: * @since 2.0
032: * @see #getPropertyValue
033: * @see #setPropertyValue
034: */
035: public abstract class AbstractPropertyAccessor extends
036: PropertyEditorRegistrySupport implements
037: ConfigurablePropertyAccessor {
038:
039: private boolean extractOldValueForEditor = false;
040:
041: public void setExtractOldValueForEditor(
042: boolean extractOldValueForEditor) {
043: this .extractOldValueForEditor = extractOldValueForEditor;
044: }
045:
046: public boolean isExtractOldValueForEditor() {
047: return extractOldValueForEditor;
048: }
049:
050: public void setPropertyValue(PropertyValue pv)
051: throws BeansException {
052: setPropertyValue(pv.getName(), pv.getValue());
053: }
054:
055: public void setPropertyValues(Map map) throws BeansException {
056: setPropertyValues(new MutablePropertyValues(map));
057: }
058:
059: public void setPropertyValues(PropertyValues pvs)
060: throws BeansException {
061: setPropertyValues(pvs, false, false);
062: }
063:
064: public void setPropertyValues(PropertyValues pvs,
065: boolean ignoreUnknown) throws BeansException {
066: setPropertyValues(pvs, ignoreUnknown, false);
067: }
068:
069: public void setPropertyValues(PropertyValues pvs,
070: boolean ignoreUnknown, boolean ignoreInvalid)
071: throws BeansException {
072:
073: List propertyAccessExceptions = null;
074: List propertyValues = (pvs instanceof MutablePropertyValues ? ((MutablePropertyValues) pvs)
075: .getPropertyValueList()
076: : Arrays.asList(pvs.getPropertyValues()));
077: for (Iterator it = propertyValues.iterator(); it.hasNext();) {
078: PropertyValue pv = (PropertyValue) it.next();
079: try {
080: // This method may throw any BeansException, which won't be caught
081: // here, if there is a critical failure such as no matching field.
082: // We can attempt to deal only with less serious exceptions.
083: setPropertyValue(pv);
084: } catch (NotWritablePropertyException ex) {
085: if (!ignoreUnknown) {
086: throw ex;
087: }
088: // Otherwise, just ignore it and continue...
089: } catch (NullValueInNestedPathException ex) {
090: if (!ignoreInvalid) {
091: throw ex;
092: }
093: // Otherwise, just ignore it and continue...
094: } catch (PropertyAccessException ex) {
095: if (propertyAccessExceptions == null) {
096: propertyAccessExceptions = new LinkedList();
097: }
098: propertyAccessExceptions.add(ex);
099: }
100: }
101:
102: // If we encountered individual exceptions, throw the composite exception.
103: if (propertyAccessExceptions != null) {
104: PropertyAccessException[] paeArray = (PropertyAccessException[]) propertyAccessExceptions
105: .toArray(new PropertyAccessException[propertyAccessExceptions
106: .size()]);
107: throw new PropertyBatchUpdateException(paeArray);
108: }
109: }
110:
111: // Redefined with public visibility.
112: public Class getPropertyType(String propertyPath) {
113: return null;
114: }
115:
116: /**
117: * Actually get the value of a property.
118: * @param propertyName name of the property to get the value of
119: * @return the value of the property
120: * @throws InvalidPropertyException if there is no such property or
121: * if the property isn't readable
122: * @throws PropertyAccessException if the property was valid but the
123: * accessor method failed
124: */
125: public abstract Object getPropertyValue(String propertyName)
126: throws BeansException;
127:
128: /**
129: * Actually set a property value.
130: * @param propertyName name of the property to set value of
131: * @param value the new value
132: * @throws InvalidPropertyException if there is no such property or
133: * if the property isn't writable
134: * @throws PropertyAccessException if the property was valid but the
135: * accessor method failed or a type mismatch occured
136: */
137: public abstract void setPropertyValue(String propertyName,
138: Object value) throws BeansException;
139:
140: }
|