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