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.MessageProcessorAnnotationParser;
11  import org.mule.api.MuleContext;
12  import org.mule.api.MuleRuntimeException;
13  import org.mule.api.context.MuleContextAware;
14  import org.mule.api.expression.ExpressionAnnotationParser;
15  import org.mule.api.registry.ObjectProcessor;
16  import org.mule.api.registry.RegistrationException;
17  import org.mule.config.AnnotationsParserFactory;
18  import org.mule.config.i18n.CoreMessages;
19  
20  import java.lang.annotation.Annotation;
21  import java.lang.reflect.Member;
22  import java.util.Collection;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * Loads the default expression, router and endpoint parsers provided by Mule.  Mule modules can add to these by
29   * registering the additional parsers in the 'registry-bootstrap.properties' for custom module.
30   */
31  public class RegistryBackedAnnotationsParserFactory implements AnnotationsParserFactory, MuleContextAware
32  {
33      /**
34       * logger used by this class
35       */
36      protected transient final Log logger = LogFactory.getLog(RegistryBackedAnnotationsParserFactory.class);
37  
38      protected MuleContext muleContext;
39  
40      public void setMuleContext(MuleContext context)
41      {
42          this.muleContext = context;
43      }
44  
45      public EndpointAnnotationParser getEndpointParser(Annotation annotation, Class aClass, Member member)
46      {
47          Collection<EndpointAnnotationParser> parsers = muleContext.getRegistry().lookupObjects(EndpointAnnotationParser.class);
48          for (EndpointAnnotationParser parser : parsers)
49          {
50              if (parser.supports(annotation, aClass, member))
51              {
52                  return parser;
53              }
54          }
55          return null;
56      }
57  
58      public ExpressionAnnotationParser getExpressionParser(Annotation annotation)
59      {
60          Collection<ExpressionAnnotationParser> parsers = muleContext.getRegistry().lookupObjects(ExpressionAnnotationParser.class);
61          for (ExpressionAnnotationParser parser : parsers)
62          {
63              if (parser.supports(annotation))
64              {
65                  return parser;
66              }
67          }
68          return null;
69      }
70  
71      public MessageProcessorAnnotationParser getRouterParser(Annotation annotation, Class aClass, Member member)
72      {
73          Collection<MessageProcessorAnnotationParser> parsers = muleContext.getRegistry().lookupObjects(MessageProcessorAnnotationParser.class);
74          for (MessageProcessorAnnotationParser parser : parsers)
75          {
76              if (parser.supports(annotation, aClass, member))
77              {
78                  return parser;
79              }
80          }
81          return null;
82      }
83  
84      protected void registerObjectProcessor(ObjectProcessor processor)
85      {
86          try
87          {
88              muleContext.getRegistry().registerObject("_" + processor.getClass().getSimpleName(), processor);
89          }
90          catch (RegistrationException e)
91          {
92              throw new MuleRuntimeException(CoreMessages.failedToCreate(processor.getClass().getName()), e);
93          }
94      }
95  
96  }