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