View Javadoc

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