View Javadoc

1   /*
2    * $Id: CallableEntryPointResolver.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  
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  import org.mule.util.ClassUtils;
21  
22  import java.lang.reflect.Method;
23  
24  /**
25   * An entrypoint resolver that only allows Service objects that implmement the
26   * Callable interface
27   *
28   * @see org.mule.api.lifecycle.Callable
29   */
30  public class CallableEntryPointResolver implements EntryPointResolver
31  {
32      protected static final Method callableMethod;
33  
34      static
35      {
36          try
37          {
38              callableMethod = Callable.class.getMethod("onCall", new Class[] {MuleEventContext.class});
39          }
40          catch (NoSuchMethodException e)
41          {
42              throw new MuleRuntimeException(
43                      MessageFactory.createStaticMessage("Panic! No onCall(MuleEventContext) method found in the Callable interface."));
44          }
45      }
46  
47  
48      public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
49      {
50          if (component instanceof Callable)
51          {
52              Object result = ((Callable) component).onCall(context);
53              return new InvocationResult(result, callableMethod);
54          }
55          else
56          {
57              InvocationResult result = new InvocationResult(InvocationResult.STATE_INVOKE_NOT_SUPPORTED);
58              result.setErrorMessage(ClassUtils.getClassName(getClass()) + ":" +
59                      CoreMessages.objectDoesNotImplementInterface(component, Callable.class).toString());
60              return result;
61          }
62      }
63  
64  
65      public String toString()
66      {
67          final StringBuffer sb = new StringBuffer();
68          sb.append("CallableEntryPointResolver");
69          sb.append("{}");
70          return sb.toString();
71      }
72  }