View Javadoc

1   /*
2    * $Id: MuleCallAnnotationHandler.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  package org.mule.module.ibeans.spi;
11  
12  import org.mule.DefaultMuleEvent;
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.MuleSession;
18  import org.mule.api.component.InterfaceBinding;
19  import org.mule.api.context.MuleContextAware;
20  import org.mule.api.endpoint.ImmutableEndpoint;
21  import org.mule.config.i18n.CoreMessages;
22  import org.mule.message.DefaultExceptionPayload;
23  import org.mule.module.ibeans.config.IBeanFlowConstruct;
24  import org.mule.session.DefaultMuleSession;
25  import org.mule.transport.NullPayload;
26  import org.mule.util.StringMessageUtils;
27  
28  import java.lang.reflect.Method;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.ibeans.api.ClientAnnotationHandler;
35  import org.ibeans.api.InvocationContext;
36  import org.ibeans.api.Request;
37  import org.ibeans.api.Response;
38  
39  /**
40   * Used to Handle {@link org.ibeans.annotation.Call} annotated method calls.
41   */
42  public class MuleCallAnnotationHandler implements ClientAnnotationHandler
43  {
44      public static final String DEFAULT_METHOD_NAME_TOKEN = "default";
45  
46      protected static Log logger = LogFactory.getLog(MuleCallAnnotationHandler.class);
47  
48      private MuleContext muleContext;
49  
50      private IBeanFlowConstruct flow;
51  
52      protected Map<String, InterfaceBinding> routers = new HashMap<String, InterfaceBinding>();
53  
54      public MuleCallAnnotationHandler(MuleContext muleContext)
55      {
56          this.muleContext = muleContext;
57      }
58  
59      public void setFlow(IBeanFlowConstruct flow)
60      {
61          this.flow = flow;
62      }
63  
64      public void addRouterForInterface(InterfaceBinding router)
65      {
66          if (router instanceof MuleContextAware)
67          {
68              ((MuleContextAware) router).setMuleContext(muleContext);
69          }
70          if (router.getMethod() == null)
71          {
72              if (routers.size() == 0)
73              {
74                  routers.put(DEFAULT_METHOD_NAME_TOKEN, router);
75              }
76              else
77              {
78                  throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage());
79              }
80          }
81          else
82          {
83              routers.put(router.getMethod(), router);
84          }
85  
86  
87      }
88  
89      public Response invoke(InvocationContext ctx) throws Exception
90      {
91          InterfaceBinding router = routers.get(ctx.getMethod().toString());
92          if (router == null)
93          {
94              throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(ctx.getMethod().getName()).toString());
95          }
96          router.getEndpoint().getProperties().putAll(ctx.getIBeanDefaultConfig().getPropertyParams());
97          Request req = ctx.getRequest();
98          MuleMessage message = ((MuleRequestMessage)req).getMessage();
99  
100         if (logger.isTraceEnabled())
101         {
102             try
103             {
104                 logger.trace("Message Before invoking "
105                         + ctx.getMethod()
106                         + ": \n"
107                         + StringMessageUtils.truncate(
108                         StringMessageUtils.toString(message.getPayload()),
109                         2000, false));
110                 logger.trace("Message Headers: \n"
111                         + StringMessageUtils.headersToString(message));
112             }
113             catch (Exception e)
114             {
115                 // ignore
116             }
117         }
118 
119 //        MuleMessage message = new DefaultMuleMessage(req.getPayload(), muleContext);
120 //
121 //        message.addProperties(router.getEndpoint().getProperties(), PropertyScope.INVOCATION);
122 //        for (String s : req.getHeaderNames())
123 //        {
124 //            message.setOutboundProperty(s, req.getHeader(s));
125 //        }
126 //        for (String s : req.getAttachmentNames())
127 //        {
128 //            message.addAttachment(s, req.getAttachment(s));
129 //        }
130        
131 
132         MuleEvent replyEvent = null;
133         MuleMessage reply;
134         MuleSession session = new DefaultMuleSession(flow, muleContext);
135 
136         try
137         {
138             replyEvent = router.process(new DefaultMuleEvent(message, router.getEndpoint(), session));
139         }
140         catch (Throwable e)
141         {
142             //Make all exceptions go through the CallException handler
143             reply = new DefaultMuleMessage(NullPayload.getInstance(), muleContext);
144             reply.setExceptionPayload(new DefaultExceptionPayload(e));
145             return new MuleResponseMessage(reply);
146         }
147         return new MuleResponseMessage(replyEvent.getMessage());
148     }
149 
150     public String getScheme(Method method)
151     {
152         InterfaceBinding router = routers.get(method.toString());
153         if (router == null)
154         {
155             throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(method.getName()).toString());
156         }
157         return router.getEndpoint().getEndpointURI().getScheme();
158     }
159 
160     ImmutableEndpoint getEndpointForMethod(Method method)
161     {
162         InterfaceBinding router = routers.get(method.toString());
163         if (router != null)
164         {
165             return router.getEndpoint();
166         }
167         return null;
168     }
169 }