View Javadoc

1   /*
2    * $Id: AnnotatedTransformerObjectProcessor.java 20072 2010-11-04 18:12:14Z mike.schilling $
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.transformer;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.annotations.ContainsTransformerMethods;
15  import org.mule.api.annotations.Transformer;
16  import org.mule.api.context.MuleContextAware;
17  import org.mule.api.registry.PreInitProcessor;
18  import org.mule.util.annotation.AnnotationMetaData;
19  import org.mule.util.annotation.AnnotationUtils;
20  
21  import java.lang.reflect.Method;
22  import java.util.List;
23  
24  /**
25   * Will check all method level annotations to see if there are any {@link org.mule.api.annotations.Transformer} annotations present.
26   * For each method annotated with {@link org.mule.api.annotations.Transformer} a Mule transformer will be created.  When the
27   * transformer is used, the method will get invoked
28   *
29   * @see org.mule.api.annotations.Transformer
30   */
31  public class AnnotatedTransformerObjectProcessor implements PreInitProcessor, MuleContextAware
32  {
33  
34      private MuleContext muleContext;
35  
36      public AnnotatedTransformerObjectProcessor()
37      {
38      }
39  
40      public AnnotatedTransformerObjectProcessor(MuleContext muleContext)
41      {
42          setMuleContext(muleContext);
43      }
44  
45      public void setMuleContext(MuleContext context)
46      {
47          this.muleContext = context;
48      }
49  
50      public Object process(Object object)
51      {
52          Class<? extends Object> clazz = object.getClass();
53          if (clazz.getAnnotation(ContainsTransformerMethods.class) == null)
54          {
55              return object;
56          }
57          List<AnnotationMetaData> annos = AnnotationUtils.getMethodAnnotations(clazz, Transformer.class);
58  
59          if (annos.size() == 0)
60          {
61              return object;
62          }
63          for (AnnotationMetaData data : annos)
64          {
65              try
66              {
67                  Transformer anno = (Transformer) data.getAnnotation();
68                  AnnotatedTransformerProxy trans = new AnnotatedTransformerProxy(
69                          anno.priorityWeighting(),
70                          object, (Method) data.getMember(), anno.sourceTypes(),
71                          null /*anno.sourceMimeType()*/, null /*anno.resultMimeType()*/);
72  
73                  muleContext.getRegistry().registerTransformer(trans);
74              }
75              catch (MuleException e)
76              {
77                  throw new RuntimeException(e);
78              }
79          }
80          return object;
81      }
82  }