1
2
3
4
5
6
7
8
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
26
27
28 public class DataObjectDefinitionParser extends ChildDefinitionParser
29 {
30
31
32
33
34
35 public DataObjectDefinitionParser(String setterMethod)
36 {
37 super(setterMethod, DataObjectFactoryBean.class);
38 }
39
40
41
42
43
44
45
46
47 public DataObjectDefinitionParser(String setterMethod, Class constraint)
48 {
49 super(setterMethod, DataObjectFactoryBean.class, constraint);
50 }
51
52
53
54
55
56
57
58
59
60 public DataObjectDefinitionParser(String setterMethod, Class constraint, boolean allowClassAttribute)
61 {
62 super(setterMethod, DataObjectFactoryBean.class, constraint, allowClassAttribute);
63 }
64
65
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 }