Package org.strecks.bind.handler

Source Code of org.strecks.bind.handler.AbstractBindHandler

/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.strecks.bind.handler;

import java.beans.PropertyDescriptor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.strecks.converter.Converter;
import org.strecks.converter.handler.ConversionHandler;
import org.strecks.exceptions.ConversionRuntimeException;
import org.strecks.util.Assert;
import org.strecks.util.ReflectHelper;

/**
* Abstract base class for <code>BindHandler</code>s. Implements convenience methods used by
* subclasses
*
* @author Phil Zoio
*/
public class AbstractBindHandler
{

  private static Log log = LogFactory.getLog(AbstractBindHandler.class);

  private PropertyDescriptor propertyDescriptor;

  private Converter converter;

  private ConversionHandler conversionHandler;

  /**
   * Instantiates converter using the <code>Class.newInstance()</code> method
   * @param propertyType
   *            the target class of the converter {@see Converter.setTargetClass() }
   * @param converterClass
   *            the name off the converter class to instantiate
   * @return a newly created instance of <code>Converter</code>
   */
  protected Converter createConverter(Class<?> propertyType, Class converterClass)
  {
    Assert.notNull(converterClass);

    Converter converter = ReflectHelper.createInstance(converterClass, Converter.class);
    converter.setTargetClass(propertyType);
    return converter;
  }

  public PropertyDescriptor getPropertyDescriptor()
  {
    return propertyDescriptor;
  }

  public void setPropertyDescriptor(PropertyDescriptor propertyDescriptor)
  {
    Assert.notNull(propertyDescriptor);
    this.propertyDescriptor = propertyDescriptor;
  }

  protected ConversionHandler getConversionHandler()
  {
    return conversionHandler;
  }

  public void setConversionHandler(ConversionHandler conversionHandler)
  {
    this.conversionHandler = conversionHandler;
  }

  /**
   * Handles conversion from the source property where the possibility exists that the type of the
   * source property to be converted may be incompatible with the <code>Converter</code>
   * parameterization type
   */
  @SuppressWarnings("unchecked")
  protected Object getAndConvertInwards(Object source, String sourcePropertyName, Converter converter)
  {
    try
    {
      return conversionHandler.getAndConvertInwards(source, sourcePropertyName, converter);
    }
    catch (ConversionRuntimeException e)
    {
      log.warn("Conversion error attempting to bind property " + source + " in object of class "
          + source.getClass().getName() + ". Have you validated this conversion?", e);
      return null;
    }
  }

  /**
   * Handles conversion from the target property where the possibility exists that the type of the
   * source property to be converted may be incompatible with the <code>Converter</code>
   * parameterization type
   */
  @SuppressWarnings("unchecked")
  protected Object getAndConvertOutwards(Object targetBean, String propertyName, Converter converter)
  {
    return conversionHandler.getAndConvertOutwards(targetBean, propertyName, converter);
  }

  public final Converter getConverter()
  {
    return converter;
  }

  public final void setConverter(Converter converter)
  {
    this.converter = converter;
  }

  public final Class getConverterClass()
  {
    return converter.getClass();
  }

}
TOP

Related Classes of org.strecks.bind.handler.AbstractBindHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.