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