001: /*
002: * hammurapi-rules @mesopotamia.version@
003: * Hammurapi rules engine.
004: * Copyright (C) 2005 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://http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.dispatch;
024:
025: import java.lang.reflect.Method;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Collections;
029: import java.util.List;
030:
031: /**
032: * This class introspects an object passed to the constructor and creates
033: * invocation handlers for methods with one argument and the name provided in
034: * the constructor.
035: * Invocation handler would not invoke target method with incompatible argument.
036: * @author Pavel Vlasov
037: * @revision $Revision$
038: */
039: public class IntrospectingInvocationTarget implements InvocationTarget {
040:
041: List handlers = new ArrayList();
042:
043: /**
044: * Introspects the target and creates an array of invocation handlers.
045: * @param targetInstance Target instance
046: * @param methodName Method name
047: * @param parameterType Invocation handlers shall accept arguments of this type. Can be null.
048: */
049: public IntrospectingInvocationTarget(final Object targetInstance,
050: final String methodName, Class parameterType) {
051: super ();
052:
053: Method[] methods = targetInstance.getClass().getMethods();
054: for (int i = 0; i < methods.length; i++) {
055: if (methods[i].getParameterTypes().length == 1) {
056: if (methodName.equals(methods[i].getName())) {
057: final Method method = methods[i];
058: final Class actualParameterType = method
059: .getParameterTypes()[0];
060:
061: // Make sure that target method will accept parameterType arguments
062: if ((parameterType == null || actualParameterType
063: .isAssignableFrom(parameterType))) {
064: handlers.add(new InvocationHandler() {
065:
066: public void invoke(Object arg,
067: ResultConsumer resultConsumer)
068: throws Throwable {
069: // Invoke only with compatible parameters.
070: if (actualParameterType.isInstance(arg)) {
071: Object ret = method.invoke(
072: targetInstance,
073: new Object[] { arg });
074: if (ret != null
075: && resultConsumer != null) {
076: resultConsumer.consume(ret);
077: }
078: }
079: }
080:
081: public Class getParameterType() {
082: return actualParameterType;
083: }
084:
085: public String toString() {
086: return "[" + methodName
087: + " handler] Target method: "
088: + method;
089: }
090:
091: });
092: }
093: }
094: }
095: }
096:
097: }
098:
099: public Collection getInvocationHandlers() {
100: return Collections.unmodifiableCollection(handlers);
101: }
102:
103: }
|