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 | 0 | super(setterMethod, DataObjectFactoryBean.class); |
38 | 0 | } |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public DataObjectDefinitionParser(String setterMethod, Class constraint) |
48 | |
{ |
49 | 0 | super(setterMethod, DataObjectFactoryBean.class, constraint); |
50 | 0 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
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 | |
} |