001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.bind.handler;
016:
017: import java.beans.PropertyDescriptor;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.strecks.converter.Converter;
022: import org.strecks.converter.handler.ConversionHandler;
023: import org.strecks.exceptions.ConversionRuntimeException;
024: import org.strecks.util.Assert;
025: import org.strecks.util.ReflectHelper;
026:
027: /**
028: * Abstract base class for <code>BindHandler</code>s. Implements convenience methods used by
029: * subclasses
030: *
031: * @author Phil Zoio
032: */
033: public class AbstractBindHandler {
034:
035: private static Log log = LogFactory
036: .getLog(AbstractBindHandler.class);
037:
038: private PropertyDescriptor propertyDescriptor;
039:
040: private Converter converter;
041:
042: private ConversionHandler conversionHandler;
043:
044: /**
045: * Instantiates converter using the <code>Class.newInstance()</code> method
046: * @param propertyType
047: * the target class of the converter {@see Converter.setTargetClass() }
048: * @param converterClass
049: * the name off the converter class to instantiate
050: * @return a newly created instance of <code>Converter</code>
051: */
052: protected Converter createConverter(Class<?> propertyType,
053: Class converterClass) {
054: Assert.notNull(converterClass);
055:
056: Converter converter = ReflectHelper.createInstance(
057: converterClass, Converter.class);
058: converter.setTargetClass(propertyType);
059: return converter;
060: }
061:
062: public PropertyDescriptor getPropertyDescriptor() {
063: return propertyDescriptor;
064: }
065:
066: public void setPropertyDescriptor(
067: PropertyDescriptor propertyDescriptor) {
068: Assert.notNull(propertyDescriptor);
069: this .propertyDescriptor = propertyDescriptor;
070: }
071:
072: protected ConversionHandler getConversionHandler() {
073: return conversionHandler;
074: }
075:
076: public void setConversionHandler(ConversionHandler conversionHandler) {
077: this .conversionHandler = conversionHandler;
078: }
079:
080: /**
081: * Handles conversion from the source property where the possibility exists that the type of the
082: * source property to be converted may be incompatible with the <code>Converter</code>
083: * parameterization type
084: */
085: @SuppressWarnings("unchecked")
086: protected Object getAndConvertInwards(Object source,
087: String sourcePropertyName, Converter converter) {
088: try {
089: return conversionHandler.getAndConvertInwards(source,
090: sourcePropertyName, converter);
091: } catch (ConversionRuntimeException e) {
092: log.warn("Conversion error attempting to bind property "
093: + source + " in object of class "
094: + source.getClass().getName()
095: + ". Have you validated this conversion?", e);
096: return null;
097: }
098: }
099:
100: /**
101: * Handles conversion from the target property where the possibility exists that the type of the
102: * source property to be converted may be incompatible with the <code>Converter</code>
103: * parameterization type
104: */
105: @SuppressWarnings("unchecked")
106: protected Object getAndConvertOutwards(Object targetBean,
107: String propertyName, Converter converter) {
108: return conversionHandler.getAndConvertOutwards(targetBean,
109: propertyName, converter);
110: }
111:
112: public final Converter getConverter() {
113: return converter;
114: }
115:
116: public final void setConverter(Converter converter) {
117: this .converter = converter;
118: }
119:
120: public final Class getConverterClass() {
121: return converter.getClass();
122: }
123:
124: }
|