View Javadoc

1   /*
2    * $Id: CallableEntryPointResolver.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
11  package org.mule.model.resolvers;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleRuntimeException;
15  import org.mule.api.lifecycle.Callable;
16  import org.mule.api.model.EntryPointResolver;
17  import org.mule.api.model.InvocationResult;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.config.i18n.MessageFactory;
20  
21  import java.lang.reflect.Method;
22  
23  /**
24   * An entry-point resolver that only allows Service objects that implement the
25   * Callable interface
26   *
27   * @see org.mule.api.lifecycle.Callable
28   */
29  public class CallableEntryPointResolver implements EntryPointResolver
30  {
31      protected static final Method callableMethod;
32  
33      static
34      {
35          try
36          {
37              callableMethod = Callable.class.getMethod("onCall", new Class[] {MuleEventContext.class});
38          }
39          catch (NoSuchMethodException e)
40          {
41              throw new MuleRuntimeException(
42                      MessageFactory.createStaticMessage("Panic! No onCall(MuleEventContext) method found in the Callable interface."));
43          }
44      }
45  
46      public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
47      {
48          if (component instanceof Callable)
49          {
50              Object result = ((Callable) component).onCall(context);
51              return new InvocationResult(this, result, callableMethod);
52          }
53          else
54          {
55              InvocationResult result = new InvocationResult(this, InvocationResult.State.NOT_SUPPORTED);
56              result.setErrorMessage(CoreMessages.objectDoesNotImplementInterface(component, Callable.class).toString());
57              return result;
58          }
59      }
60  
61      @Override
62      public String toString()
63      {
64          return "CallableEntryPointResolver{}";
65      }
66  }