1
2
3
4
5
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
23
24
25 public class DataObjectDefinitionParser extends ChildDefinitionParser
26 {
27
28
29
30
31
32 public DataObjectDefinitionParser(String setterMethod)
33 {
34 super(setterMethod, DataObjectFactoryBean.class);
35 }
36
37
38
39
40
41
42
43
44 public DataObjectDefinitionParser(String setterMethod, Class constraint)
45 {
46 super(setterMethod, DataObjectFactoryBean.class, constraint);
47 }
48
49
50
51
52
53
54
55
56
57 public DataObjectDefinitionParser(String setterMethod, Class constraint, boolean allowClassAttribute)
58 {
59 super(setterMethod, DataObjectFactoryBean.class, constraint, allowClassAttribute);
60 }
61
62 @Override
63 protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
64 {
65 if(StringUtils.isNotEmpty(element.getTextContent()))
66 {
67 assembler.extendBean("data", element.getTextContent(), false);
68 }
69 super.postProcess(context, assembler, element);
70 }
71
72 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 context = applicationContext;
83 }
84
85 public Object getObject() throws Exception
86 {
87 if(data!=null)
88 {
89 return data;
90 }
91
92 if(file!=null)
93 {
94 if(binary)
95 {
96 data = IOUtils.toByteArray(IOUtils.getResourceAsStream(file, getClass()));
97 }
98 else
99 {
100 data = IOUtils.getResourceAsString(file, getClass());
101 }
102 }
103 else if(ref!=null)
104 {
105 data = context.getBean(ref);
106 }
107
108 if(data==null)
109 {
110 throw new IllegalArgumentException("Data is null was not found");
111 }
112 return data;
113 }
114
115 public Class getObjectType()
116 {
117 return Object.class;
118 }
119
120 public boolean isSingleton()
121 {
122 return true;
123 }
124
125 public String getFile()
126 {
127 return file;
128 }
129
130 public void setFile(String file)
131 {
132 this.file = file;
133 }
134
135 public String getRef()
136 {
137 return ref;
138 }
139
140 public void setRef(String ref)
141 {
142 this.ref = ref;
143 }
144
145 public Object getData()
146 {
147 return data;
148 }
149
150 public void setData(Object data)
151 {
152 this.data = data;
153 }
154
155 public boolean isBinary()
156 {
157 return binary;
158 }
159
160 public void setBinary(boolean binary)
161 {
162 this.binary = binary;
163 }
164 }
165 }