/*
* 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.factory;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.strecks.bind.annotation.BindSimple;
import org.strecks.bind.handler.BindHandler;
import org.strecks.bind.handler.BindSimpleHandler;
import org.strecks.converter.Converter;
import org.strecks.converter.handler.ConversionHandler;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.util.BeanUtils;
import org.strecks.util.ReflectHelper;
/**
* <code>BindHandlerFactory</code> implementation for the <code>BindSimple</code> annotation
* @see org.strecks.bind.annotation.BindSimple
* @author Phil Zoio
*/
public class BindSimpleFactory implements BindHandlerFactory
{
public BindHandler createHandler(Annotation annotation, Method getterMethod, Converter converter, ConversionHandler conversionHandler)
{
BindSimple bindAnnotation = (BindSimple) annotation;
BindSimpleHandler handler = new BindSimpleHandler();
String expression = bindAnnotation.expression();
String beanLocatingExpression = null;
String propertyName = null;
int lastDot = expression.lastIndexOf('.');
if (lastDot == -1)
{
propertyName = expression;
}
else
{
beanLocatingExpression = expression.substring(0, lastDot);
propertyName = expression.substring(lastDot + 1);
}
handler.setBeanLocatingExpression(beanLocatingExpression);
handler.setBeanPropertyName(propertyName);
if (converter != null)
{
handler.setConverter(converter);
}
else
{
Class<? extends Converter> converterClass = bindAnnotation.converter();
converter = ReflectHelper.createInstance(converterClass, Converter.class);
handler.setConverter(converter);
}
PropertyDescriptor property = BeanUtils.findPropertyForMethod(getterMethod);
if (property == null)
{
throw new ApplicationConfigurationException("Method " + getterMethod + " is not a valid JavaBean property");
}
handler.setPropertyDescriptor(property);
handler.setConversionHandler(conversionHandler);
return handler;
}
}