View Javadoc

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          super(setterMethod, DataObjectFactoryBean.class);
38      }
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          super(setterMethod, DataObjectFactoryBean.class, constraint);
50      }
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          super(setterMethod, DataObjectFactoryBean.class, constraint, allowClassAttribute);
63      }
64  
65      //@Override
66      protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
67      {
68          if(StringUtils.isNotEmpty(element.getTextContent()))
69          {
70              assembler.extendBean("data", element.getTextContent(), false);
71          }
72          super.postProcess(context, assembler, element);
73      }
74  
75      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              context = applicationContext;
86          }
87  
88          public Object getObject() throws Exception
89          {
90              if(data!=null)
91              {
92                  return data;
93              }
94  
95              if(file!=null)
96              {
97                  if(binary)
98                  {
99                      data = IOUtils.toByteArray(IOUtils.getResourceAsStream(file, getClass()));
100                 }
101                 else
102                 {
103                     data = IOUtils.getResourceAsString(file, getClass());
104                 }
105             }
106             else if(ref!=null)
107             {
108                 data = context.getBean(ref);
109             }
110 
111             if(data==null)
112             {
113                 throw new IllegalArgumentException("Data is null was not found");
114             }
115             return data;
116         }
117 
118         public Class getObjectType()
119         {
120             return Object.class;
121         }
122 
123         public boolean isSingleton()
124         {
125             return true;
126         }
127 
128         public String getFile()
129         {
130             return file;
131         }
132 
133         public void setFile(String file)
134         {
135             this.file = file;
136         }
137 
138         public String getRef()
139         {
140             return ref;
141         }
142 
143         public void setRef(String ref)
144         {
145             this.ref = ref;
146         }
147 
148         public Object getData()
149         {
150             return data;
151         }
152 
153         public void setData(Object data)
154         {
155             this.data = data;
156         }
157 
158         public boolean isBinary()
159         {
160             return binary;
161         }
162 
163         public void setBinary(boolean binary)
164         {
165             this.binary = binary;
166         }
167     }
168 }