View Javadoc

1   /*
2    * $Id: NestedInvocationHandler.java 12075 2008-06-17 11:12:10Z rossmason $
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.routing.nested;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.RequestContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.routing.NestedRouter;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.util.StringMessageUtils;
20  
21  import java.lang.reflect.InvocationHandler;
22  import java.lang.reflect.Method;
23  import java.util.Map;
24  
25  import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  public class NestedInvocationHandler implements InvocationHandler
30  {
31  
32      public static final String DEFAULT_METHOD_NAME_TOKEN = "default";
33  
34      protected static Log logger = LogFactory.getLog(NestedInvocationHandler.class);
35  
36      protected Map routers = new ConcurrentHashMap();
37  
38      public NestedInvocationHandler(NestedRouter router)
39      {
40          addRouterForInterface(router);
41      }
42  
43      public void addRouterForInterface(NestedRouter router)
44      {
45          if (router.getMethod() == null)
46          {
47              if (routers.size() == 0)
48              {
49                  routers.put(DEFAULT_METHOD_NAME_TOKEN, router);
50              }
51              else
52              {
53                  throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage());
54              }
55          }
56          else
57          {
58              routers.put(router.getMethod(), router);
59          }
60      }
61  
62      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
63      {
64          if (method.getName().equals("toString"))
65          {
66              return toString();
67          }
68  
69          MuleMessage message;
70  
71          if (args.length == 1)
72          {
73              message = new DefaultMuleMessage(args[0]);
74          }
75          else
76          {
77              message = new DefaultMuleMessage(args);
78          }
79  
80  
81          NestedRouter router = (NestedRouter) routers.get(method.getName());
82          if (router == null)
83          {
84              router = (NestedRouter) routers.get(DEFAULT_METHOD_NAME_TOKEN);
85          }
86  
87          if (router == null)
88          {
89              throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(method.getName()).toString());
90          }
91  
92          MuleMessage reply;
93          MuleEvent currentEvent = RequestContext.getEvent();
94          reply = router.route(message, currentEvent.getSession(), router.getEndpoint().isSynchronous());
95  
96          if (reply != null)
97          {
98              if (reply.getExceptionPayload() != null)
99              {
100                 throw reply.getExceptionPayload().getException();
101             }
102             else
103             {
104                 return reply.getPayload();
105             }
106         }
107         else
108         {
109             return null;
110         }
111     }
112 
113     public String toString()
114     {
115         final StringBuffer sb = new StringBuffer();
116         sb.append("NestedInvocation");
117         sb.append("{routers='").append(StringMessageUtils.toString(routers));
118         sb.append('}');
119         return sb.toString();
120     }
121 }