1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.streaming;
12
13 import org.mule.MuleManager;
14 import org.mule.umo.endpoint.UMOImmutableEndpoint;
15 import org.mule.umo.lifecycle.InitialisationException;
16 import org.mule.umo.transformer.TransformerException;
17 import org.mule.umo.transformer.UMOStreamingTransformer;
18 import org.mule.util.ClassUtils;
19
20 import java.io.InputStream;
21 import java.io.OutputStream;
22
23 import org.apache.commons.beanutils.BeanUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28
29
30 public abstract class AbstractStreamingTransformer implements UMOStreamingTransformer
31 {
32
33
34
35 protected final Log logger = LogFactory.getLog(getClass());
36
37
38
39
40
41 protected String name = null;
42
43
44
45
46 protected UMOImmutableEndpoint endpoint = null;
47
48
49
50
51 protected UMOStreamingTransformer nextTransformer;
52
53
54
55
56
57 private boolean ignoreBadInput = false;
58
59
60
61
62 public AbstractStreamingTransformer()
63 {
64 name = generateTransformerName();
65 }
66
67
68
69
70 public String getName()
71 {
72 if (name == null)
73 {
74 setName(ClassUtils.getSimpleName(getClass()));
75 }
76 return name;
77 }
78
79
80
81
82 public void setName(String string)
83 {
84 logger.debug("Setting transformer name to: " + name);
85 name = string;
86 }
87
88
89
90
91
92
93
94 public final Object transform(InputStream src, OutputStream dest, String encoding)
95 throws TransformerException
96 {
97 if (encoding == null && endpoint != null)
98 {
99 encoding = endpoint.getEncoding();
100 }
101
102
103 if (encoding == null)
104 {
105 encoding = MuleManager.getConfiguration().getEncoding();
106 }
107
108 if (logger.isDebugEnabled())
109 {
110 logger.debug("Applying transformer " + getName() + " (" + getClass().getName() + ")");
111 logger.debug("Object before transform");
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 return null;
136 }
137
138 public UMOImmutableEndpoint getEndpoint()
139 {
140 return endpoint;
141 }
142
143
144
145
146
147
148 public void setEndpoint(UMOImmutableEndpoint endpoint)
149 {
150 this.endpoint = endpoint;
151 UMOStreamingTransformer trans = nextTransformer;
152 while (trans != null && endpoint != null)
153 {
154 trans.setEndpoint(endpoint);
155 trans = trans.getNextTransformer();
156 }
157 }
158
159 protected abstract Object doTransform(InputStream src, OutputStream dest, String encoding)
160 throws TransformerException;
161
162
163
164
165
166
167 public UMOStreamingTransformer getNextTransformer()
168 {
169 return nextTransformer;
170 }
171
172
173
174
175
176
177 public void setNextTransformer(UMOStreamingTransformer nextTransformer)
178 {
179 this.nextTransformer = nextTransformer;
180 }
181
182
183
184
185
186
187 public Object clone() throws CloneNotSupportedException
188 {
189 try
190 {
191
192 return BeanUtils.cloneBean(this);
193 }
194 catch (Exception e)
195 {
196 throw (CloneNotSupportedException) new CloneNotSupportedException("Failed to clone transformer: "
197 + e.getMessage()).initCause(e);
198 }
199 }
200
201
202
203
204
205
206
207 public void initialise() throws InitialisationException
208 {
209
210 }
211
212 protected String generateTransformerName()
213 {
214 String name = getClass().getName();
215 int i = name.lastIndexOf(".");
216 if (i > -1)
217 {
218 name = name.substring(i + 1);
219 }
220 return name;
221 }
222
223 public boolean isIgnoreBadInput()
224 {
225 return ignoreBadInput;
226 }
227
228 public void setIgnoreBadInput(boolean ignoreBadInput)
229 {
230 this.ignoreBadInput = ignoreBadInput;
231 }
232
233 public String toString()
234 {
235 return "StreamingTransformer{" + "name='" + name + "'" + ", ignoreBadInput=" + ignoreBadInput + "}";
236 }
237
238 public boolean isAcceptNull()
239 {
240 return false;
241 }
242
243 }