Coverage Report - org.mule.config.spring.parsers.specific.DataObjectDefinitionParser
 
Classes in this File Line Coverage Branch Coverage Complexity
DataObjectDefinitionParser
0%
0/10
0%
0/2
0
DataObjectDefinitionParser$DataObjectFactoryBean
0%
0/28
0%
0/10
0
 
 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.spring.parsers.specific;
 8  
 
 9  
 import org.mule.config.spring.parsers.assembly.BeanAssembler;
 10  
 import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
 11  
 import org.mule.util.IOUtils;
 12  
 import org.mule.util.StringUtils;
 13  
 
 14  
 import org.springframework.beans.BeansException;
 15  
 import org.springframework.beans.factory.FactoryBean;
 16  
 import org.springframework.beans.factory.xml.ParserContext;
 17  
 import org.springframework.context.ApplicationContext;
 18  
 import org.springframework.context.ApplicationContextAware;
 19  
 import org.w3c.dom.Element;
 20  
 
 21  
 /**
 22  
  * Represents a static config data object where the body of the element can be the data of a file
 23  
  * attribute can be set.  Data will be loaded from file into an {@link java.io.InputStream}
 24  
  */
 25  
 public class DataObjectDefinitionParser extends ChildDefinitionParser
 26  
 {
 27  
     /**
 28  
      * The class will be inferred from the class attribute
 29  
      *
 30  
      * @param setterMethod The target method (where the child will be injected)
 31  
      */
 32  
     public DataObjectDefinitionParser(String setterMethod)
 33  
     {
 34  0
         super(setterMethod, DataObjectFactoryBean.class);
 35  0
     }
 36  
 
 37  
     /**
 38  
      * The class (which is inferred from the class attribute if null here) is checked to be
 39  
      * a subclass of the constraint
 40  
      *
 41  
      * @param setterMethod The target method (where the child will be injected)
 42  
      * @param constraint   Superclass of clazz (may be null)
 43  
      */
 44  
     public DataObjectDefinitionParser(String setterMethod, Class constraint)
 45  
     {
 46  0
         super(setterMethod, DataObjectFactoryBean.class, constraint);
 47  0
     }
 48  
 
 49  
     /**
 50  
      * The class (which is inferred from the class attribute if null here) is checked to be
 51  
      * a subclass of the constraint.
 52  
      *
 53  
      * @param setterMethod        The target method (where the child will be injected)
 54  
      * @param constraint          Superclass of clazz (may be null)
 55  
      * @param allowClassAttribute Is class read from class attribute (if present, takes precedence over clazz)
 56  
      */
 57  
     public DataObjectDefinitionParser(String setterMethod, Class constraint, boolean allowClassAttribute)
 58  
     {
 59  0
         super(setterMethod, DataObjectFactoryBean.class, constraint, allowClassAttribute);
 60  0
     }
 61  
 
 62  
     @Override
 63  
     protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
 64  
     {
 65  0
         if(StringUtils.isNotEmpty(element.getTextContent()))
 66  
         {
 67  0
             assembler.extendBean("data", element.getTextContent(), false);
 68  
         }
 69  0
         super.postProcess(context, assembler, element);
 70  0
     }
 71  
 
 72  0
     public static class DataObjectFactoryBean implements FactoryBean, ApplicationContextAware
 73  
     {
 74  
         private String ref;
 75  
         private boolean binary;
 76  
         private String file;
 77  
         private Object data;
 78  
         private ApplicationContext context;
 79  
 
 80  
         public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
 81  
         {
 82  0
             context = applicationContext;
 83  0
         }
 84  
 
 85  
         public Object getObject() throws Exception
 86  
         {
 87  0
             if(data!=null)
 88  
             {
 89  0
                 return data;
 90  
             }
 91  
 
 92  0
             if(file!=null)
 93  
             {
 94  0
                 if(binary)
 95  
                 {
 96  0
                     data = IOUtils.toByteArray(IOUtils.getResourceAsStream(file, getClass()));
 97  
                 }
 98  
                 else
 99  
                 {
 100  0
                     data = IOUtils.getResourceAsString(file, getClass());
 101  
                 }
 102  
             }
 103  0
             else if(ref!=null)
 104  
             {
 105  0
                 data = context.getBean(ref);
 106  
             }
 107  
 
 108  0
             if(data==null)
 109  
             {
 110  0
                 throw new IllegalArgumentException("Data is null was not found");
 111  
             }
 112  0
             return data;
 113  
         }
 114  
 
 115  
         public Class getObjectType()
 116  
         {
 117  0
             return Object.class;
 118  
         }
 119  
 
 120  
         public boolean isSingleton()
 121  
         {
 122  0
             return true;
 123  
         }
 124  
 
 125  
         public String getFile()
 126  
         {
 127  0
             return file;
 128  
         }
 129  
 
 130  
         public void setFile(String file)
 131  
         {
 132  0
             this.file = file;
 133  0
         }
 134  
 
 135  
         public String getRef()
 136  
         {
 137  0
             return ref;
 138  
         }
 139  
 
 140  
         public void setRef(String ref)
 141  
         {
 142  0
             this.ref = ref;
 143  0
         }
 144  
 
 145  
         public Object getData()
 146  
         {
 147  0
             return data;
 148  
         }
 149  
 
 150  
         public void setData(Object data)
 151  
         {
 152  0
             this.data = data;
 153  0
         }
 154  
 
 155  
         public boolean isBinary()
 156  
         {
 157  0
             return binary;
 158  
         }
 159  
 
 160  
         public void setBinary(boolean binary)
 161  
         {
 162  0
             this.binary = binary;
 163  0
         }
 164  
     }
 165  
 }