01: /*
02: * $Id: ExplicitMethodEntryPointResolver.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10: package org.mule.model.resolvers;
11:
12: import org.mule.api.MuleEventContext;
13: import org.mule.api.model.InvocationResult;
14: import org.mule.config.i18n.CoreMessages;
15: import org.mule.util.ClassUtils;
16: import org.mule.util.StringMessageUtils;
17:
18: import java.lang.reflect.Method;
19: import java.util.Collection;
20: import java.util.Iterator;
21: import java.util.LinkedHashSet;
22: import java.util.Set;
23:
24: /**
25: * An Entrypoint resolver that allows the user to set one or more acceptiple methd names to look for.
26: * For each method reflection will be used to see if the method accepts the current payload types (
27: * the results are cached to improve performance).
28: * There has to be at least one method name set on this resolver
29: */
30: public class ExplicitMethodEntryPointResolver extends
31: AbstractEntryPointResolver {
32:
33: private Set methods = new LinkedHashSet(2);
34:
35: public void setMethods(Collection methods) {
36: this .methods = new LinkedHashSet(methods);
37: }
38:
39: public void addMethod(String name) {
40: this .methods.add(name);
41: }
42:
43: public boolean removeMethod(String name) {
44: return this .methods.remove(name);
45: }
46:
47: public InvocationResult invoke(Object component,
48: MuleEventContext context) throws Exception {
49: if (methods == null || methods.size() == 0) {
50: throw new IllegalStateException(CoreMessages.objectIsNull(
51: "methods").toString());
52: }
53:
54: Object[] payload = getPayloadFromMessage(context);
55: Class[] classTypes = ClassUtils.getClassTypes(payload);
56: Method method = null;
57: for (Iterator iterator = methods.iterator(); iterator.hasNext();) {
58: String methodName = (String) iterator.next();
59: method = getMethodByName(methodName, context);
60:
61: if (method == null) {
62: method = ClassUtils.getMethod(component.getClass(),
63: methodName, classTypes);
64: }
65: if (method != null) {
66: addMethodByName(method, context);
67: break;
68: }
69: }
70:
71: if (method == null) {
72: InvocationResult result = new InvocationResult(
73: InvocationResult.STATE_INVOKED_FAILED);
74: result.setErrorNoMatchingMethods(component, classTypes,
75: this );
76: return result;
77: }
78: return invokeMethod(component, method, payload);
79: }
80:
81: public String toString() {
82: final StringBuffer sb = new StringBuffer();
83: sb.append("ExplicitEntryPointResolver");
84: sb.append("{methods=").append(
85: StringMessageUtils.toString(methods));
86: sb.append("{transformFirst=").append(isTransformFirst());
87: sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
88: sb.append('}');
89: return sb.toString();
90: }
91:
92: }
|