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