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.BindSelect;
22: import org.strecks.bind.handler.BindHandler;
23: import org.strecks.bind.handler.BindSelectHandler;
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>BindSelect</code> annotation
32: * @see org.strecks.bind.annotation.BindSelect
33: * @author Phil Zoio
34: */
35: public class BindSelectFactory implements BindHandlerFactory {
36:
37: public BindHandler createHandler(Annotation annotation,
38: Method getterMethod, Converter converter,
39: ConversionHandler conversionHandler) {
40:
41: BindSelect bindAnnotation = (BindSelect) annotation;
42: BindSelectHandler handler = new BindSelectHandler();
43:
44: String expression = bindAnnotation.targetBeanExpression();
45: handler.setTargetBeanExpression(expression);
46:
47: String beanLocatingExpression = null;
48: String propertyName = null;
49:
50: int lastDot = expression.lastIndexOf('.');
51: if (lastDot == -1) {
52: propertyName = expression;
53: } else {
54: beanLocatingExpression = expression.substring(0, lastDot);
55: propertyName = expression.substring(lastDot + 1);
56: }
57:
58: handler.setBeanLocatingExpression(beanLocatingExpression);
59: handler.setBeanPropertyName(propertyName);
60: handler.setBeanPropertyClass(bindAnnotation.idClass());
61: handler.setBeanLookupExpression(bindAnnotation
62: .lookupMapExpression());
63: handler.setBeanPropertyIdName(bindAnnotation
64: .targetBeanIdProperty());
65:
66: if (converter != null) {
67: handler.setConverter(converter);
68: } else {
69: Class<? extends Converter> converterClass = bindAnnotation
70: .idConverter();
71: converter = ReflectHelper.createInstance(converterClass,
72: Converter.class);
73: converter.setTargetClass(bindAnnotation.idClass());
74: handler.setConverter(converter);
75: }
76:
77: PropertyDescriptor property = BeanUtils
78: .findPropertyForMethod(getterMethod);
79:
80: if (property == null) {
81: throw new ApplicationConfigurationException("Method "
82: + getterMethod
83: + " is not a valid JavaBean property");
84: }
85:
86: handler.setPropertyDescriptor(property);
87: handler.setConversionHandler(conversionHandler);
88:
89: return handler;
90: }
91: }
|