01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.bind.factory;
16:
17: import java.beans.PropertyDescriptor;
18: import java.lang.annotation.Annotation;
19: import java.lang.reflect.Method;
20:
21: import org.strecks.bind.annotation.BindSimple;
22: import org.strecks.bind.handler.BindHandler;
23: import org.strecks.bind.handler.BindSimpleHandler;
24: import org.strecks.converter.Converter;
25: import org.strecks.converter.handler.ConversionHandler;
26: import org.strecks.exceptions.ApplicationConfigurationException;
27: import org.strecks.util.BeanUtils;
28: import org.strecks.util.ReflectHelper;
29:
30: /**
31: * <code>BindHandlerFactory</code> implementation for the <code>BindSimple</code> annotation
32: * @see org.strecks.bind.annotation.BindSimple
33: * @author Phil Zoio
34: */
35: public class BindSimpleFactory implements BindHandlerFactory {
36:
37: public BindHandler createHandler(Annotation annotation,
38: Method getterMethod, Converter converter,
39: ConversionHandler conversionHandler) {
40:
41: BindSimple bindAnnotation = (BindSimple) annotation;
42: BindSimpleHandler handler = new BindSimpleHandler();
43:
44: String expression = bindAnnotation.expression();
45:
46: String beanLocatingExpression = null;
47: String propertyName = null;
48:
49: int lastDot = expression.lastIndexOf('.');
50: if (lastDot == -1) {
51: propertyName = expression;
52: } else {
53: beanLocatingExpression = expression.substring(0, lastDot);
54: propertyName = expression.substring(lastDot + 1);
55: }
56:
57: handler.setBeanLocatingExpression(beanLocatingExpression);
58: handler.setBeanPropertyName(propertyName);
59:
60: if (converter != null) {
61: handler.setConverter(converter);
62: } else {
63: Class<? extends Converter> converterClass = bindAnnotation
64: .converter();
65: converter = ReflectHelper.createInstance(converterClass,
66: Converter.class);
67: handler.setConverter(converter);
68: }
69:
70: PropertyDescriptor property = BeanUtils
71: .findPropertyForMethod(getterMethod);
72: if (property == null) {
73: throw new ApplicationConfigurationException("Method "
74: + getterMethod
75: + " is not a valid JavaBean property");
76: }
77: handler.setPropertyDescriptor(property);
78: handler.setConversionHandler(conversionHandler);
79:
80: return handler;
81:
82: }
83:
84: }
|