001: /*
002: * Created on Nov 29, 2003
003: *
004: /*
005: Copyright (c) 2003 eInnovation Inc. All rights reserved
006:
007: This library is free software; you can redistribute it and/or modify it under the terms
008: of the GNU Lesser General Public License as published by the Free Software Foundation;
009: either version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
012: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013: See the GNU Lesser General Public License for more details.
014: */
015: package com.openedit.modules.reflect;
016:
017: import java.beans.IntrospectionException;
018: import java.beans.Introspector;
019: import java.beans.MethodDescriptor;
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Set;
026:
027: import com.openedit.OpenEditException;
028: import com.openedit.WebPageRequest;
029: import com.openedit.modules.BaseModule;
030:
031: /**
032: * @author Matt Avery, mavery@einnovation.com
033: *
034: * Could make this an immutable object and eliminate the WebPageContext argument
035: * to the public methods.
036: */
037: public class Reflector extends BaseModule {
038: public void reflectOnSessionObjects(WebPageRequest inContext)
039: throws OpenEditException {
040:
041: for (Iterator iter = inContext.getParameterMap().keySet()
042: .iterator(); iter.hasNext();) {
043: String key = (String) iter.next();
044: int dotIndex = key.indexOf('.');
045: if (dotIndex > 0) {
046: String sessionObjectKey = key.substring(0, dotIndex);
047: Object sessionObject = inContext
048: .getSessionValue(sessionObjectKey);
049: if (sessionObject == null) {
050: return;
051: }
052: String methodName = key.substring(dotIndex + 1, key
053: .length());
054: List methods = findMethods(sessionObject, methodName);
055: for (Iterator iterator = methods.iterator(); iterator
056: .hasNext();) {
057: Method method = (Method) iterator.next();
058: try {
059: invokeMethod(inContext, method, sessionObject);
060: } catch (Exception ex) {
061: //log.error( ex );
062: if (ex instanceof OpenEditException) {
063: throw (OpenEditException) ex;
064: }
065: throw new OpenEditException(ex);
066: }
067: }
068: }
069: }
070:
071: }
072:
073: /** Two methods could have the same name but different signatures
074: *
075: * @param inObject
076: * @param inMethodName
077: * @return
078: */
079: public List findMethods(Object inObject, String inMethodName) {
080: List methods = new ArrayList();
081: MethodDescriptor[] methodDescriptors;
082: try {
083: methodDescriptors = Introspector.getBeanInfo(
084: inObject.getClass()).getMethodDescriptors();
085: } catch (IntrospectionException e) {
086: return methods;
087: }
088: for (int i = 0; i < methodDescriptors.length; i++) {
089: if (methodDescriptors[i].getName().equals(inMethodName)) {
090: methods.add(methodDescriptors[i].getMethod());
091: }
092: }
093: return methods;
094: }
095:
096: protected void invokeMethod(WebPageRequest inContext,
097: Method method, Object sessionObject) throws Exception {
098: try {
099: method.invoke(sessionObject, new Object[] { inContext });
100: } catch (InvocationTargetException ite) {
101: Throwable throwable = ite.getTargetException();
102: if (throwable instanceof Exception) {
103: throw (Exception) throwable;
104: }
105: /*
106: else if (throwable instanceof Message)
107: {
108: throw (Message) throwable;
109: }
110: */
111: else if (throwable instanceof Error) {
112: throw (Error) throwable;
113: } else {
114: throw new OpenEditException(throwable.getMessage());
115: }
116: }
117:
118: }
119:
120: public void reflectOnRequestParameters(WebPageRequest inContext,
121: Object inObject) throws Exception {
122: Set keys = inContext.getParameterMap().keySet();
123: for (Iterator iter = keys.iterator(); iter.hasNext();) {
124: String key = (String) iter.next();
125: Object value;
126: List methods = findMethods(inObject, key);
127:
128: for (Iterator iterator = methods.iterator(); iterator
129: .hasNext();) {
130: Method method = (Method) iterator.next();
131:
132: Class[] parameterTypes = method.getParameterTypes();
133: if (parameterTypes.length == 1) {
134: Class parameterType = parameterTypes[0];
135: if (parameterType.equals(String.class)) {
136: value = inContext.getRequestParameter(key);
137: } else if (parameterType.equals(String[].class)) {
138: value = inContext.getRequestParameters(key);
139: } else if (parameterType.equals(int.class)) {
140: value = new Integer(inContext
141: .getRequestParameter(key));
142: } else if (parameterType.equals(long.class)) {
143: value = new Long(inContext
144: .getRequestParameter(key));
145: } else if (parameterType.equals(double.class)) {
146: value = new Double(inContext
147: .getRequestParameter(key));
148: } else if (parameterType.equals(byte.class)) {
149: value = new Byte(inContext
150: .getRequestParameter(key));
151: } else if (parameterTypes.equals(boolean.class)) {
152: value = new Boolean(inContext
153: .getRequestParameter(key));
154: } else {
155: continue;
156: }
157:
158: method.invoke(inObject, new Object[] { value });
159: }
160: }
161: }
162: }
163:
164: }
|