01: /*
02: * hgcommons 7
03: * Hammurapi Group Common Library
04: * Copyright (C) 2003 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.config;
24:
25: import java.lang.reflect.Field;
26: import java.lang.reflect.InvocationTargetException;
27: import java.lang.reflect.Method;
28:
29: import biz.hammurapi.convert.CompositeConverter;
30:
31: /**
32: * Populates a bean with parameters from HttpRequest.
33: * @author Pavel Vlasov
34: *
35: * @version $Revision: 1.2 $
36: */
37: public class Bean2ContextConfigurableAdapter implements
38: ContextConfigurable {
39: private Object bean;
40:
41: public Bean2ContextConfigurableAdapter(Object bean) {
42: super ();
43: this .bean = bean;
44: }
45:
46: public void configure(Context context, CompositeConverter converter)
47: throws ConfigurationException {
48: Method[] ma = bean.getClass().getMethods();
49: for (int i = 0; i < ma.length; i++) {
50: String mname = ma[i].getName();
51: if (mname.startsWith("set") && mname.length() >= 4
52: && ma[i].getParameterTypes().length == 1
53: && ma[i].getReturnType().equals(void.class)) {
54: String pName = mname.length() == 4 ? mname
55: .toLowerCase().substring(3) : mname.substring(
56: 3, 4).toLowerCase()
57: + mname.substring(4);
58: Object value = context.get(pName);
59: if (value != null) {
60: try {
61: ma[i].invoke(bean,
62: new Object[] { converter.convert(value,
63: ma[i].getParameterTypes()[0],
64: false) });
65: } catch (IllegalAccessException e) {
66: throw new ConfigurationException(
67: "Cannot invoke method " + ma[i]
68: + " to set parameter value "
69: + value, e);
70: } catch (InvocationTargetException e) {
71: throw new ConfigurationException(
72: "Cannot invoke method " + ma[i]
73: + " to set parameter value "
74: + value, e);
75: }
76: }
77: }
78: }
79:
80: Field[] fa = bean.getClass().getFields();
81: for (int i = 0; i < fa.length; i++) {
82: Object value = context.get(fa[i].getName());
83: if (value != null) {
84: try {
85: fa[i].set(bean, converter.convert(value, fa[i]
86: .getType(), false));
87: } catch (IllegalAccessException e) {
88: throw new ConfigurationException(
89: "Cannot set field " + fa[i] + " to value "
90: + value, e);
91: }
92: }
93: }
94: }
95: }
|