001: /*
002: * $Id: ReflectionEntryPointResolver.java 10529 2008-01-25 05:58:36Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010: package org.mule.model.resolvers;
011:
012: import org.mule.api.MuleEventContext;
013: import org.mule.api.model.InvocationResult;
014: import org.mule.routing.filters.WildcardFilter;
015: import org.mule.util.ClassUtils;
016: import org.mule.util.StringMessageUtils;
017: import org.mule.util.StringUtils;
018:
019: import java.lang.reflect.Method;
020: import java.util.Arrays;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.HashSet;
024: import java.util.List;
025: import java.util.Set;
026:
027: /**
028: * <code>ReflectEntryPointResolver</code> is used to determine the entry point on a service
029: * after an event has been received for it. The entrypoint is discovered using
030: * the event payload type(s) as the argument using reflection. An entry point will try and match for
031: * different argument types, so it's possible to have multiple entry points on a
032: * single service.
033: * <p/>
034: * For multiple parameters the payload of context.getMessage().getPayload() should be an Array of objects.
035: * If the message payload is of type {@link org.mule.transport.NullPayload} the resolver will look for a no-argument
036: * method to call that doesn't match the set of ignoredMethods on the resolver.
037: * <p/>
038: * Also a set of 'ignored' methods are available (and the use can add others) to tell the resolver to not
039: * resolve to these methods. The default ones are:
040: * <ul>
041: * <li>{@link #toString()}
042: * <li>{@link #getClass()}
043: * <li>{@link #notify}
044: * <li>{@link #notifyAll}
045: * <li>{@link #hashCode}
046: * <li>{@link #wait}
047: * <li>{@link java.lang.reflect.Proxy#getInvocationHandler}
048: * <li>{@link Cloneable#clone()}
049: * <li>'is*'
050: * <li>'get*'.
051: * <li>'set*'.
052: * </ul>
053: * <p/> Note that wildcard expressions can be used.
054: */
055: public class ReflectionEntryPointResolver extends
056: AbstractEntryPointResolver {
057:
058: // we don't want to match these methods when looking for a service method
059: private Set ignoredMethods = new HashSet(Arrays
060: .asList(new String[] { "equals", "getInvocationHandler",
061: "set*", "toString", "getClass", "notify",
062: "notifyAll", "wait", "hashCode", "clone", "is*",
063: "get*" }));
064:
065: protected WildcardFilter filter;
066:
067: public ReflectionEntryPointResolver() {
068: updateFilter();
069: }
070:
071: private void updateFilter() {
072: filter = new WildcardFilter(StringUtils.join(ignoredMethods,
073: ','));
074: }
075:
076: /**
077: * Returns an unmodifable Set of ignoredMethods on this resolver
078: * To add method to the resolver use {@link #addIgnoredMethod(String)}
079: *
080: * @return unmodifiable set of method names set on this resolver
081: */
082: public Collection getIgnoredMethods() {
083: return Collections.unmodifiableSet(ignoredMethods);
084: }
085:
086: public void setIgnoredMethods(Collection methods) {
087: this .ignoredMethods = new HashSet(methods);
088: updateFilter();
089: }
090:
091: public void addIgnoredMethod(String name) {
092: this .ignoredMethods.add(name);
093: updateFilter();
094: }
095:
096: public boolean removeIgnoredMethod(String name) {
097: boolean result = this .ignoredMethods.remove(name);
098: updateFilter();
099: return result;
100: }
101:
102: /**
103: * Will discover the entrypoint on the service using the payload type to figure out the method to call.
104: * For multiple parameters the payload of context.getMessage().geTPayload() should be an Array of objects.
105: * If the message payload is of type {@link org.mule.transport.NullPayload} the resolver will look for a no-argument
106: * method to call that doesn't match the set of ignoredMethods on the resover.
107: *
108: * @param service
109: * @param context
110: * @return
111: * @throws Exception
112: */
113: public InvocationResult invoke(Object component,
114: MuleEventContext context) throws Exception {
115: Object[] payload = getPayloadFromMessage(context);
116:
117: Method method;
118: InvocationResult result;
119:
120: method = this .getMethodByArguments(component, payload);
121:
122: if (method != null) {
123: return invokeMethod(component, method, payload);
124: }
125:
126: Class[] types = ClassUtils.getClassTypes(payload);
127:
128: // do any methods on the service accept a context?
129: List methods = ClassUtils.getSatisfiableMethods(component
130: .getClass(), types, isAcceptVoidMethods(), false,
131: ignoredMethods, filter);
132:
133: int numMethods = methods.size();
134: if (numMethods > 1) {
135: result = new InvocationResult(
136: InvocationResult.STATE_INVOKED_FAILED);
137: // too many methods match the context argument
138: result.setErrorTooManyMatchingMethods(component, types,
139: StringMessageUtils.toString(methods), this );
140: return result;
141:
142: } else if (numMethods == 1) {
143: // found exact match for method with context argument
144: method = this .addMethodByArguments(component,
145: (Method) methods.get(0), payload);
146: } else {
147: methods = ClassUtils.getSatisfiableMethods(component
148: .getClass(), ClassUtils.getClassTypes(payload),
149: true, true, ignoredMethods);
150:
151: numMethods = methods.size();
152:
153: if (numMethods > 1) {
154: result = new InvocationResult(
155: InvocationResult.STATE_INVOKED_FAILED);
156: // too many methods match the context argument
157: result.setErrorTooManyMatchingMethods(component, types,
158: StringMessageUtils.toString(methods), this );
159: return result;
160: } else if (numMethods == 1) {
161: // found exact match for payload argument
162: method = this .addMethodByArguments(component,
163: (Method) methods.get(0), payload);
164: } else {
165: result = new InvocationResult(
166: InvocationResult.STATE_INVOKED_FAILED);
167: // no method for payload argument either - bail out
168: result.setErrorNoMatchingMethods(component, ClassUtils
169: .getClassTypes(payload), this );
170: return result;
171: }
172: }
173:
174: return invokeMethod(component, method, payload);
175: }
176:
177: public String toString() {
178: final StringBuffer sb = new StringBuffer();
179: sb.append("ReflectionEntryPointResolver");
180: sb.append("{ignoredMethods=").append(
181: StringMessageUtils.toString(ignoredMethods));
182: sb.append("{transformFirst=").append(isTransformFirst());
183: sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
184: sb.append('}');
185: return sb.toString();
186: }
187: }
|