View Javadoc

1   /*
2    * $Id: LookupInjectionProcessor.java 20320 2010-11-24 15:03:31Z 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.processors;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.annotations.expressions.Lookup;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.expression.RequiredValueException;
16  import org.mule.api.registry.InjectProcessor;
17  import org.mule.config.i18n.AnnotationsMessages;
18  import org.mule.util.StringUtils;
19  
20  import java.lang.reflect.Field;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * TODO
27   */
28  public class LookupInjectionProcessor implements InjectProcessor, MuleContextAware
29  {
30      /**
31       * logger used by this class
32       */
33      protected transient final Log logger = LogFactory.getLog(LookupInjectionProcessor.class);
34  
35      private MuleContext context;
36  
37      public LookupInjectionProcessor()
38      {
39      }
40  
41      public LookupInjectionProcessor(MuleContext context)
42      {
43          this.context = context;
44      }
45  
46      public void setMuleContext(MuleContext context)
47      {
48          this.context = context;
49      }
50  
51      public Object process(Object object)
52      {
53          Field[] fields;
54          try
55          {
56              fields = object.getClass().getDeclaredFields();
57          }
58          catch (NoClassDefFoundError e)
59          {
60              //Only log the warning when debugging
61              if (logger.isDebugEnabled())
62              {
63                  logger.warn(e.toString());
64              }
65              return object;
66          }
67          for (int i = 0; i < fields.length; i++)
68          {
69              Field field = fields[i];
70              if (field.isAnnotationPresent(Lookup.class))
71              {
72                  try
73                  {
74                      field.setAccessible(true);
75                      Object value;
76                      String name = field.getAnnotation(Lookup.class).value();
77                      boolean optional = field.getAnnotation(Lookup.class).optional();
78                      if(StringUtils.isBlank(name))
79                      {
80                          value = context.getRegistry().lookupObject(field.getType());
81                      }
82                      else
83                      {
84                          value = context.getRegistry().lookupObject(name);
85                      }
86                      if (value == null && !optional)
87                      {
88                          throw new RequiredValueException(AnnotationsMessages.lookupNotFoundInRegistry(field.getType(), name, object.getClass()));
89                      }
90  
91                      field.set(object, value);
92                  }
93                  catch (RequiredValueException e)
94                  {
95                      throw e;
96                  }
97                  catch (Exception e)
98                  {
99                      throw new RequiredValueException(AnnotationsMessages.lookupFailedSeePreviousException(object), e);
100                 }
101             }
102         }
103         return object;
104     }
105 }