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.factory.annotation;
018:
019: import java.beans.PropertyDescriptor;
020: import java.lang.annotation.Annotation;
021: import java.lang.reflect.Method;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.springframework.beans.BeansException;
026: import org.springframework.beans.PropertyValues;
027: import org.springframework.beans.factory.BeanInitializationException;
028: import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
029: import org.springframework.core.annotation.AnnotationUtils;
030: import org.springframework.util.Assert;
031:
032: /**
033: * {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
034: * that enforces required JavaBean properties to have been configured.
035: * Required bean properties are detected through a Java 5 annotation:
036: * by default, Spring's {@link Required} annotation.
037: *
038: * <p>The motivation for the existence of this BeanPostProcessor is to allow
039: * developers to annotate the setter properties of their own classes with an
040: * arbitrary JDK 1.5 annotation to indicate that the container must check
041: * for the configuration of a dependency injected value. This neatly pushes
042: * responsibility for such checking onto the container (where it arguably belongs),
043: * and obviates the need (<b>in part</b>) for a developer to code a method that
044: * simply checks that all required properties have actually been set.
045: *
046: * <p>Please note that an 'init' method may still need to implemented (and may
047: * still be desirable), because all that this class does is enforce that a
048: * 'required' property has actually been configured with a value. It does
049: * <b>not</b> check anything else... In particular, it does not check that a
050: * configured value is not <code>null</code>.
051: *
052: * @author Rob Harrop
053: * @author Juergen Hoeller
054: * @since 2.0
055: * @see #setRequiredAnnotationType
056: * @see Required
057: */
058: public class RequiredAnnotationBeanPostProcessor extends
059: InstantiationAwareBeanPostProcessorAdapter {
060:
061: private Class<? extends Annotation> requiredAnnotationType = Required.class;
062:
063: /**
064: * Set the 'required' annotation type, to be used on bean property
065: * setter methods.
066: * <p>The default required annotation type is the Spring-provided
067: * {@link Required} annotation.
068: * <p>This setter property exists so that developers can provide their own
069: * (non-Spring-specific) annotation type to indicate that a property value
070: * is required.
071: */
072: public void setRequiredAnnotationType(
073: Class<? extends Annotation> requiredAnnotationType) {
074: Assert.notNull(requiredAnnotationType,
075: "'requiredAnnotationType' must not be null");
076: this .requiredAnnotationType = requiredAnnotationType;
077: }
078:
079: /**
080: * Return the 'required' annotation type.
081: */
082: protected Class<? extends Annotation> getRequiredAnnotationType() {
083: return this .requiredAnnotationType;
084: }
085:
086: public PropertyValues postProcessPropertyValues(PropertyValues pvs,
087: PropertyDescriptor[] pds, Object bean, String beanName)
088: throws BeansException {
089:
090: List<String> invalidProperties = new ArrayList<String>();
091: for (PropertyDescriptor pd : pds) {
092: if (isRequiredProperty(pd) && !pvs.contains(pd.getName())) {
093: invalidProperties.add(pd.getName());
094: }
095: }
096: if (!invalidProperties.isEmpty()) {
097: throw new BeanInitializationException(
098: buildExceptionMessage(invalidProperties, beanName));
099: }
100: return pvs;
101: }
102:
103: /**
104: * Is the supplied property required to have a value (that is, to be dependency-injected)?
105: * <p>This implementation looks for the existence of a
106: * {@link #setRequiredAnnotationType "required" annotation}
107: * on the supplied {@link PropertyDescriptor property}.
108: * @param propertyDescriptor the target PropertyDescriptor (never <code>null</code>)
109: * @return <code>true</code> if the supplied property has been marked as being required;
110: * <code>false</code> if not, or if the supplied property does not have a setter method
111: */
112: protected boolean isRequiredProperty(
113: PropertyDescriptor propertyDescriptor) {
114: Method setter = propertyDescriptor.getWriteMethod();
115: return (setter != null && AnnotationUtils.getAnnotation(setter,
116: getRequiredAnnotationType()) != null);
117: }
118:
119: /**
120: * Build an exception message for the given list of invalid properties.
121: * @param invalidProperties the list of names of invalid properties
122: * @param beanName the name of the bean
123: * @return the exception message
124: */
125: private String buildExceptionMessage(
126: List<String> invalidProperties, String beanName) {
127: int size = invalidProperties.size();
128: StringBuilder sb = new StringBuilder();
129: sb.append(size == 1 ? "Property" : "Properties");
130: for (int i = 0; i < size; i++) {
131: String propertyName = invalidProperties.get(i);
132: if (i > 0) {
133: if (i == (size - 1)) {
134: sb.append(" and");
135: } else {
136: sb.append(",");
137: }
138: }
139: sb.append(" '").append(propertyName).append("'");
140: }
141: sb.append(size == 1 ? " is" : " are");
142: sb.append(" required for bean '").append(beanName).append("'");
143: return sb.toString();
144: }
145:
146: }
|