001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.config;
024:
025: import java.lang.reflect.Field;
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import biz.hammurapi.convert.CompositeConverter;
030:
031: /**
032: * Populates a bean with parameters from HttpRequest.
033: * @author Pavel Vlasov
034: *
035: * @version $Revision: 1.3 $
036: */
037: public class BeanContext implements Context {
038: private Object bean;
039:
040: public BeanContext(Object bean) {
041: super ();
042: this .bean = bean;
043: }
044:
045: public Object get(String name) {
046: String[] ni = split(name);
047: Method[] ma = bean.getClass().getMethods();
048:
049: for (int i = 0; i < ma.length; i++) {
050: Method candidate = ma[i];
051: Class[] cpt = candidate.getParameterTypes();
052: if (cpt.length == ni.length - 1
053: && !candidate.getReturnType().equals(void.class)) {
054: String mname = candidate.getName();
055: if (mname.startsWith("get") && mname.length() >= 4) {
056: String pName = mname.length() == 4 ? mname
057: .substring(3).toLowerCase() : mname
058: .substring(3, 4).toLowerCase()
059: + mname.substring(4);
060: if (ni[0].equals(pName)) {
061: try {
062: CompositeConverter converter = CompositeConverter
063: .getDefaultConverter();
064: Object[] args = new Object[ni.length - 1];
065: for (int j = 0; j < args.length; j++) {
066: args[j] = converter.convert(ni[j + 1],
067: cpt[j], false);
068: }
069: return candidate.invoke(bean, args);
070: } catch (IllegalAccessException e) {
071: throw new RuntimeConfigurationException(
072: "Cannot invoke method " + candidate,
073: e);
074: } catch (InvocationTargetException e) {
075: throw new RuntimeConfigurationException(
076: "Cannot invoke method " + candidate,
077: e);
078: }
079: }
080: }
081: }
082: }
083:
084: if (ni.length == 1) {
085: Field[] fa = bean.getClass().getFields();
086: for (int i = 0; i < fa.length; i++) {
087: if (fa[i].getName().equals(ni[0])) {
088: try {
089: return fa[i].get(bean);
090: } catch (IllegalAccessException e) {
091: throw new RuntimeConfigurationException(
092: "Cannot get field " + fa[i] + " value",
093: e);
094: }
095: }
096: }
097: }
098:
099: return null;
100: }
101:
102: /**
103: * Splits property name into getter name and indexes.
104: * Override this method to achieve desired functionality.
105: * @param name Property name.
106: * @return Name split into getter name and indexes, new String[] {name} if not overriden.
107: */
108: protected String[] split(String name) {
109: return new String[] { name };
110: }
111:
112: }
|