View Javadoc

1   /*
2    * $Id: CallAnnotationParser.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.config;
11  
12  import org.mule.MessageExchangePattern;
13  import org.mule.api.MuleException;
14  import org.mule.api.annotations.meta.Channel;
15  import org.mule.api.annotations.meta.ChannelType;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.endpoint.OutboundEndpoint;
18  import org.mule.config.endpoint.AbstractEndpointAnnotationParser;
19  import org.mule.config.endpoint.AnnotatedEndpointData;
20  import org.mule.module.ibeans.i18n.IBeansMessages;
21  import org.mule.module.ibeans.spi.support.CallOutboundEndpoint;
22  import org.mule.module.ibeans.spi.support.CallRequestEndpoint;
23  
24  import java.beans.ExceptionListener;
25  import java.lang.annotation.Annotation;
26  import java.lang.reflect.Member;
27  import java.lang.reflect.Method;
28  import java.util.Map;
29  
30  import org.ibeans.annotation.Call;
31  import org.ibeans.api.CallException;
32  import org.ibeans.api.ExceptionListenerAware;
33  
34  /**
35   * The parser responsible for parsing {@link org.ibeans.annotation.Call} annotations.
36   */
37  public class CallAnnotationParser extends AbstractEndpointAnnotationParser
38  {
39      protected AnnotatedEndpointData createEndpointData(Annotation annotation) throws MuleException
40      {
41          Call call = (Call) annotation;
42          AnnotatedEndpointData epd = new AnnotatedEndpointData(MessageExchangePattern.REQUEST_RESPONSE, ChannelType.Outbound, call);
43          epd.setAddress(call.uri());
44          epd.setProperties(AnnotatedEndpointData.convert(call.properties()));
45          return epd;
46      }
47  
48      protected String getIdentifier()
49      {
50          return Call.class.getAnnotation(Channel.class).identifer();
51      }
52  
53      @Override
54      public boolean supports(Annotation annotation, Class clazz, Member member)
55      {
56          //You cannot use the @Call annotation on an implementation class
57          boolean supports = clazz.isInterface();
58          if (supports)
59          {
60              supports = annotation instanceof Call;  
61          }
62          if (supports)
63          {
64              //Allow services to extend an exception listener that the user can plug in
65              if (ExceptionListenerAware.class.isAssignableFrom(clazz))
66              {
67                  supports = true;
68              }
69              else
70              {
71                  Class[] exceptionTypes = ((Method) member).getExceptionTypes();
72                  boolean hasValidExceptionType = false;
73                  for (int i = 0; i < exceptionTypes.length; i++)
74                  {
75                      Class exceptionType = exceptionTypes[i];
76                      hasValidExceptionType = exceptionType.equals(Exception.class) || exceptionType.isAssignableFrom(CallException.class) || clazz.isAssignableFrom(ExceptionListener.class);
77                  }
78                  if (!hasValidExceptionType)
79                  {
80                      throw new IllegalArgumentException(IBeansMessages.illegalCallMethod((Method)member).getMessage());
81                  }
82              }
83          }
84          return supports;
85      }
86  
87      public OutboundEndpoint parseOutboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
88      {
89          AnnotatedEndpointData data = createEndpointData(annotation);
90          if (data.getConnectorName() == null)
91          {
92              data.setConnectorName((String) metaInfo.get("connectorName"));
93          }
94          return new CallOutboundEndpoint(muleContext, data);
95      }
96  
97      public InboundEndpoint parseInboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
98      {
99          AnnotatedEndpointData data = createEndpointData(annotation);
100         if (data.getConnectorName() == null)
101         {
102             data.setConnectorName((String) metaInfo.get("connectorName"));
103         }
104         return new CallRequestEndpoint(muleContext, data);
105     }
106 }