1 /*
2 * $Id: InvocationResult.java 10489 2008-01-23 17:53:38Z dfeist $
3 * --------------------------------------------------------------------------------------
4 * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5 *
6 * The software in this package is published under the terms of the CPAL v1.0
7 * license, a copy of which has been included with this distribution in the
8 * LICENSE.txt file.
9 */
10 package org.mule.api.model;
11
12 import org.mule.config.i18n.CoreMessages;
13
14 import java.lang.reflect.Method;
15
16 /** TODO */
17 public class InvocationResult
18 {
19 /** the resover performing the invocation knows that it cannot attempt to make the invocation */
20 public static final int STATE_INVOKE_NOT_SUPPORTED = 0;
21
22 /** the invocation was successful */
23 public static final int STATE_INVOKED_SUCESSFUL = 1;
24
25 /** The invocation was attempted but failed */
26 public static final int STATE_INVOKED_FAILED = 2;
27
28 private String errorMessage;
29
30 /** the name of the method called for this invocation */
31 private String methodCalled;
32
33 private Object result;
34
35 private int state;
36
37 /**
38 * Will construct an InvocationResult with a given state. The state must be either
39 * {@link #STATE_INVOKE_NOT_SUPPORTED} if the resover performing the invocation knows that it cannot
40 * attempt to make the invocation
41 * {@link #STATE_INVOKED_FAILED} If an invocation attempt is made but fails
42 * {@link #STATE_INVOKED_SUCESSFUL} If the invocation was successful
43 *
44 * @param state the state of the result
45 */
46 public InvocationResult(int state)
47 {
48 if (state < 0 || state > 2)
49 {
50 throw new IllegalArgumentException("state");
51 }
52 this.state = state;
53 }
54
55 /**
56 * Creates a result with the result payload set. The state of this result will be {@link #STATE_INVOKED_SUCESSFUL}
57 * since only in this state will a result be set.
58 *
59 * @param result the result of a successful invocation
60 */
61 public InvocationResult(Object result, Method method)
62 {
63
64 this.result = result;
65 this.state = STATE_INVOKED_SUCESSFUL;
66 this.methodCalled = method.getName();
67 }
68
69 /**
70 * Returns the name of the method invoked, this property is only set if the state of the invocation is
71 * {@link #STATE_INVOKED_SUCESSFUL}
72 *
73 * @return the name of the method invoked
74 */
75 public String getMethodCalled()
76 {
77 return methodCalled;
78 }
79
80 /**
81 * The result of this invocation
82 *
83 * @return an object or null if the result did not yeild a result or because the state of this invocation result
84 * is either {@link #STATE_INVOKE_NOT_SUPPORTED} or {@link #STATE_INVOKED_FAILED}.
85 */
86 public Object getResult()
87 {
88 return result;
89 }
90
91 /**
92 * Returns the state of this invocation. Possible values are:
93 * {@link #STATE_INVOKE_NOT_SUPPORTED} if the resover performing the invocation knows that it cannot
94 * attempt to make the invocation
95 * {@link #STATE_INVOKED_FAILED} If an invocation attempt is made but fails
96 * {@link #STATE_INVOKED_SUCESSFUL} If the invocation was successful
97 *
98 * @return
99 */
100 public int getState()
101 {
102 return state;
103 }
104
105 /**
106 * An optional error message can be set if the invocation state is not {@link #STATE_INVOKED_SUCESSFUL}
107 *
108 * @param message
109 */
110 public void setErrorMessage(String message)
111 {
112 if (state == STATE_INVOKED_SUCESSFUL)
113 {
114 throw new IllegalStateException(CoreMessages.invocationSuccessfulCantSetError().toString());
115 }
116 errorMessage = message;
117 }
118
119 /**
120 * Returns true if an error message has been set on this result, false otherwise
121 *
122 * @return true if an error message has been set on this result, false otherwise
123 */
124 public boolean hasError()
125 {
126 return errorMessage != null;
127 }
128
129 /**
130 * Returns the error message set on this result or null if none has been set
131 *
132 * @return the error message set on this result or null if none has been set
133 */
134 public String getErrorMessage()
135 {
136 return errorMessage;
137 }
138
139 public void setErrorTooManyMatchingMethods(Object component, Class[] argTypes, String methods, EntryPointResolver resolver)
140 {
141 setErrorMessage(CoreMessages.tooManyAcceptableMethodsOnObjectUsingResolverForTypes(
142 component.getClass().getName(), argTypes, resolver).toString());
143 }
144
145 public void setErrorTooManyMatchingMethods(Object component, Class[] argTypes, EntryPointResolver resolver)
146 {
147 setErrorMessage(CoreMessages.tooManyAcceptableMethodsOnObjectUsingResolverForTypes(
148 component.getClass().getName(), argTypes, resolver).toString());
149 }
150
151 public void setErrorNoMatchingMethods(Object component, Class[] args, EntryPointResolver resolver)
152 {
153 setErrorMessage(CoreMessages.noEntryPointFoundWithArgsUsingResolver(
154 component.getClass().getName(), args, resolver).toString());
155 }
156
157 public void setErrorNoMatchingMethodsCalled(Object component, String methods, EntryPointResolver resolver)
158 {
159 setErrorMessage(CoreMessages.noMatchingMethodsOnObjectCalledUsingResolver(
160 component.getClass().getName(), methods, resolver).toString());
161 }
162
163 }