001: /*
002: * $Id: InvocationResult.java 10489 2008-01-23 17:53:38Z 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.api.model;
011:
012: import org.mule.config.i18n.CoreMessages;
013:
014: import java.lang.reflect.Method;
015:
016: /** TODO */
017: public class InvocationResult {
018: /** the resover performing the invocation knows that it cannot attempt to make the invocation */
019: public static final int STATE_INVOKE_NOT_SUPPORTED = 0;
020:
021: /** the invocation was successful */
022: public static final int STATE_INVOKED_SUCESSFUL = 1;
023:
024: /** The invocation was attempted but failed */
025: public static final int STATE_INVOKED_FAILED = 2;
026:
027: private String errorMessage;
028:
029: /** the name of the method called for this invocation */
030: private String methodCalled;
031:
032: private Object result;
033:
034: private int state;
035:
036: /**
037: * Will construct an InvocationResult with a given state. The state must be either
038: * {@link #STATE_INVOKE_NOT_SUPPORTED} if the resover performing the invocation knows that it cannot
039: * attempt to make the invocation
040: * {@link #STATE_INVOKED_FAILED} If an invocation attempt is made but fails
041: * {@link #STATE_INVOKED_SUCESSFUL} If the invocation was successful
042: *
043: * @param state the state of the result
044: */
045: public InvocationResult(int state) {
046: if (state < 0 || state > 2) {
047: throw new IllegalArgumentException("state");
048: }
049: this .state = state;
050: }
051:
052: /**
053: * Creates a result with the result payload set. The state of this result will be {@link #STATE_INVOKED_SUCESSFUL}
054: * since only in this state will a result be set.
055: *
056: * @param result the result of a successful invocation
057: */
058: public InvocationResult(Object result, Method method) {
059:
060: this .result = result;
061: this .state = STATE_INVOKED_SUCESSFUL;
062: this .methodCalled = method.getName();
063: }
064:
065: /**
066: * Returns the name of the method invoked, this property is only set if the state of the invocation is
067: * {@link #STATE_INVOKED_SUCESSFUL}
068: *
069: * @return the name of the method invoked
070: */
071: public String getMethodCalled() {
072: return methodCalled;
073: }
074:
075: /**
076: * The result of this invocation
077: *
078: * @return an object or null if the result did not yeild a result or because the state of this invocation result
079: * is either {@link #STATE_INVOKE_NOT_SUPPORTED} or {@link #STATE_INVOKED_FAILED}.
080: */
081: public Object getResult() {
082: return result;
083: }
084:
085: /**
086: * Returns the state of this invocation. Possible values are:
087: * {@link #STATE_INVOKE_NOT_SUPPORTED} if the resover performing the invocation knows that it cannot
088: * attempt to make the invocation
089: * {@link #STATE_INVOKED_FAILED} If an invocation attempt is made but fails
090: * {@link #STATE_INVOKED_SUCESSFUL} If the invocation was successful
091: *
092: * @return
093: */
094: public int getState() {
095: return state;
096: }
097:
098: /**
099: * An optional error message can be set if the invocation state is not {@link #STATE_INVOKED_SUCESSFUL}
100: *
101: * @param message
102: */
103: public void setErrorMessage(String message) {
104: if (state == STATE_INVOKED_SUCESSFUL) {
105: throw new IllegalStateException(CoreMessages
106: .invocationSuccessfulCantSetError().toString());
107: }
108: errorMessage = message;
109: }
110:
111: /**
112: * Returns true if an error message has been set on this result, false otherwise
113: *
114: * @return true if an error message has been set on this result, false otherwise
115: */
116: public boolean hasError() {
117: return errorMessage != null;
118: }
119:
120: /**
121: * Returns the error message set on this result or null if none has been set
122: *
123: * @return the error message set on this result or null if none has been set
124: */
125: public String getErrorMessage() {
126: return errorMessage;
127: }
128:
129: public void setErrorTooManyMatchingMethods(Object component,
130: Class[] argTypes, String methods,
131: EntryPointResolver resolver) {
132: setErrorMessage(CoreMessages
133: .tooManyAcceptableMethodsOnObjectUsingResolverForTypes(
134: component.getClass().getName(), argTypes,
135: resolver).toString());
136: }
137:
138: public void setErrorTooManyMatchingMethods(Object component,
139: Class[] argTypes, EntryPointResolver resolver) {
140: setErrorMessage(CoreMessages
141: .tooManyAcceptableMethodsOnObjectUsingResolverForTypes(
142: component.getClass().getName(), argTypes,
143: resolver).toString());
144: }
145:
146: public void setErrorNoMatchingMethods(Object component,
147: Class[] args, EntryPointResolver resolver) {
148: setErrorMessage(CoreMessages
149: .noEntryPointFoundWithArgsUsingResolver(
150: component.getClass().getName(), args, resolver)
151: .toString());
152: }
153:
154: public void setErrorNoMatchingMethodsCalled(Object component,
155: String methods, EntryPointResolver resolver) {
156: setErrorMessage(CoreMessages
157: .noMatchingMethodsOnObjectCalledUsingResolver(
158: component.getClass().getName(), methods,
159: resolver).toString());
160: }
161:
162: }
|