View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.endpoint;
8   
9   import org.mule.api.EndpointAnnotationParser;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleException;
12  import org.mule.api.annotations.meta.Channel;
13  import org.mule.api.config.ConfigurationException;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.util.StringUtils;
19  
20  import java.lang.annotation.Annotation;
21  import java.lang.reflect.Member;
22  import java.util.Map;
23  import java.util.Properties;
24  import java.util.StringTokenizer;
25  
26  /**
27   * TODO
28   */
29  public abstract class AbstractEndpointAnnotationParser implements EndpointAnnotationParser, MuleContextAware
30  {
31      public static final String ENDPOINT_BUILDER_POSTFIX = ".builder";
32  
33      protected MuleContext muleContext;
34  
35      public void setMuleContext(MuleContext context)
36      {
37          this.muleContext = context;
38      }
39  
40      protected AnnotatedEndpointHelper getEndpointHelper() throws MuleException
41      {
42          AnnotatedEndpointHelper helper = muleContext.getRegistry().lookupObject(AnnotatedEndpointHelper.class);
43          if (helper == null)
44          {
45              helper = new AnnotatedEndpointHelper(muleContext);
46          }
47          return helper;
48      }
49  
50      public OutboundEndpoint parseOutboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
51      {
52          return (OutboundEndpoint) getEndpointHelper().processEndpoint(createEndpointData(annotation));
53      }
54  
55      public InboundEndpoint parseInboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
56      {
57          return (InboundEndpoint) getEndpointHelper().processEndpoint(createEndpointData(annotation));
58      }
59  
60      public boolean supports(Annotation annotation, Class clazz, Member member)
61      {
62          Channel channel = annotation.annotationType().getAnnotation(Channel.class);
63          return channel != null && channel.identifer().equals(getIdentifier());
64      }
65  
66      protected Properties convertProperties(String[] properties)
67      {
68          if(properties==null || properties.length==0)
69          {
70              return null;
71          }
72          
73          Properties props = new Properties();
74          for (String property : properties)
75          {
76              StringTokenizer st = new StringTokenizer(property, "=");
77              if(st.hasMoreTokens())
78              {
79                  props.setProperty(st.nextToken().trim(), st.nextToken().trim());
80              }
81          }
82          return props;
83      }
84  
85      protected <T> T lookupConfig(String location, Class<T> type) throws ConfigurationException
86      {
87          if (StringUtils.isEmpty(location))
88          {
89              return null;
90          }
91          Object o = muleContext.getRegistry().lookupObject(location + ENDPOINT_BUILDER_POSTFIX);
92          if (o == null)
93          {
94              o = muleContext.getRegistry().lookupObject(location);
95              if (o == null)
96              {
97                  return null;
98                  //throw new ConfigurationException(CoreMessages.objectNotFound(location));
99              }
100         }
101         if (type.isInstance(o))
102         {
103             return (T) o;
104         }
105         else
106         {
107             throw new ConfigurationException(CoreMessages.objectNotOfCorrectType(o.getClass(), type));
108         }
109     }
110 
111     protected abstract String getIdentifier();
112 
113     protected abstract AnnotatedEndpointData createEndpointData(Annotation annotation) throws MuleException;
114 }