View Javadoc

1   /*
2    * $Id: EntryPoint.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.impl.model.resolvers;
12  
13  import java.lang.reflect.InvocationTargetException;
14  import java.lang.reflect.Method;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * <code>EntryPoint</code> is a method on a Mule-managed component that is invoked
21   * when an event for the component is received.
22   */
23  public class EntryPoint
24  {
25      /**
26       * logger used by this class
27       */
28      protected static final Log logger = LogFactory.getLog(EntryPoint.class);
29  
30      /**
31       * the method on the object to invoke
32       */
33      private Method method;
34  
35      /**
36       * Creates a new EntryPoint with the given method
37       * 
38       * @param method the method to invoke on the component
39       */
40      public EntryPoint(Method method)
41      {
42          this.method = method;
43      }
44  
45      /**
46       * Will invoke the entry point method on the given component
47       * 
48       * @param component the component to invoke
49       * @param arg the argument to pass to the method invocation
50       * @return An object (if any) returned by the invocation
51       * @throws InvocationTargetException
52       * @throws IllegalAccessException
53       */
54      public Object invoke(Object component, Object arg)
55          throws InvocationTargetException, IllegalAccessException
56      {
57          String methodCall = null;
58          if (logger.isDebugEnabled())
59          {
60              methodCall = component.getClass().getName() + "." + method.getName() + "("
61                           + arg.getClass().getName() + ")";
62              logger.debug("Invoking " + methodCall);
63          }
64  
65          Object result = method.invoke(component, new Object[]{arg});
66          if (logger.isDebugEnabled())
67          {
68              logger.debug("Result of call " + methodCall + " is " + result);
69          }
70          return result;
71      }
72  
73      /**
74       * Determines if the <code>EntryPoint</code> is avoid method or not
75       * 
76       * @return true if the method is void
77       */
78      public boolean isVoid()
79      {
80          return method.getReturnType().getName().equals("void");
81      }
82  
83      /**
84       * Gets the method name
85       * 
86       * @return the method name
87       */
88      public String getName()
89      {
90          if (method == null)
91          {
92              return null;
93          }
94          return method.getName();
95      }
96  
97      /**
98       * Gets the argument type for the method
99       * 
100      * @return the argument type. It should never be null
101      */
102     public Class getParameterType()
103     {
104         return method.getParameterTypes()[0];
105     }
106 
107     /**
108      * Gets the method return type of the method
109      * 
110      * @return the return type or null if the method is void
111      */
112     public Class getReturnType()
113     {
114         if (isVoid())
115         {
116             return null;
117         }
118         else
119         {
120             return method.getReturnType();
121         }
122     }
123 
124     protected void setMethod(Method method)
125     {
126         this.method = method;
127     }
128 
129     protected Method getMethod()
130     {
131         return method;
132     }
133 }