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.injection.factory;
16:
17: import java.beans.PropertyDescriptor;
18: import java.lang.annotation.Annotation;
19:
20: import org.strecks.converter.Converter;
21: import org.strecks.exceptions.ApplicationConfigurationException;
22: import org.strecks.exceptions.ApplicationRuntimeException;
23: import org.strecks.injection.annotation.InjectRequestParameter;
24: import org.strecks.injection.handler.InjectionHandler;
25: import org.strecks.injection.handler.RequestParameterInjectionHandler;
26: import org.strecks.util.ReflectHelper;
27:
28: /**
29: * Enables creation of <code>RequestParammeterInjectionHandler</code> for type converting and injecting named
30: * <code>HttpServletRequest</code> parameter
31: * @author Phil Zoio
32: */
33: public class RequestParameterFactory implements InjectionHandlerFactory {
34:
35: public InjectionHandler createInjectionHandler(
36: Annotation annotation, Class clazz,
37: PropertyDescriptor propertyDescriptor) {
38:
39: InjectRequestParameter input = (InjectRequestParameter) annotation;
40:
41: Class converterClass = input.converter();
42: Converter converter = RequestParameterFactory.createConverter(
43: propertyDescriptor.getPropertyType(), converterClass);
44: String parameterName = AnnotationFactoryUtils.getAttributeName(
45: propertyDescriptor.getName(), input.name());
46: boolean required = input.required();
47:
48: // make sure that converter has String generic type
49: boolean ok = ReflectHelper.checkGenericType(converterClass,
50: Converter.class, String.class);
51: if (!ok) {
52: Class sourceType = ReflectHelper.getGenericType(
53: converterClass, Converter.class);
54:
55: throw new ApplicationConfigurationException("@"
56: + input.annotationType().getSimpleName() + " in "
57: + clazz.getName() + " uses converter "
58: + converterClass.getName()
59: + " whose conversion source type is "
60: + sourceType.getName() + ", and not "
61: + String.class.getName());
62:
63: }
64:
65: InjectionHandler handler = new RequestParameterInjectionHandler(
66: parameterName, converter, required);
67: return handler;
68:
69: }
70:
71: /**
72: * Creates a converter
73: */
74: public static Converter createConverter(Class propertyType,
75: Class converterClass) {
76: Converter converter;
77: try {
78: Object newInstance = converterClass.newInstance();
79: converter = (Converter) newInstance;
80: converter.setTargetClass(propertyType);
81: } catch (Exception e) {
82: throw new ApplicationRuntimeException(
83: "Could not instantiate class "
84: + converterClass.getName(), e);
85: }
86: return converter;
87: }
88: }
|