View Javadoc

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