1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.impl.model.streaming; |
11 | |
|
12 | |
import org.mule.config.i18n.CoreMessages; |
13 | |
import org.mule.providers.streaming.StreamMessageAdapter; |
14 | |
import org.mule.umo.MessagingException; |
15 | |
import org.mule.umo.UMODescriptor; |
16 | |
import org.mule.umo.UMOEventContext; |
17 | |
import org.mule.umo.UMOException; |
18 | |
import org.mule.umo.endpoint.UMOEndpoint; |
19 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
20 | |
import org.mule.umo.routing.UMOOutboundRouter; |
21 | |
import org.mule.umo.routing.UMOOutboundRouterCollection; |
22 | |
|
23 | |
import java.io.BufferedOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.OutputStream; |
26 | |
import java.util.Iterator; |
27 | |
|
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class DeferredOutputStream extends OutputStream |
38 | |
{ |
39 | |
|
40 | 0 | protected final Log logger = LogFactory.getLog(DeferredOutputStream.class); |
41 | |
private UMOEventContext event; |
42 | 0 | private OutputStream out = null; |
43 | 0 | private int buffer = 0; |
44 | |
|
45 | |
|
46 | |
public DeferredOutputStream(UMOEventContext event) |
47 | 0 | { |
48 | 0 | this.event = event; |
49 | 0 | } |
50 | |
|
51 | |
|
52 | |
public DeferredOutputStream(UMOEventContext event, int buffer) |
53 | 0 | { |
54 | 0 | this.event = event; |
55 | 0 | this.buffer = buffer; |
56 | 0 | } |
57 | |
|
58 | |
public void write(int b) throws IOException |
59 | |
{ |
60 | 0 | if (out == null) |
61 | |
{ |
62 | 0 | out = getOutputStream(); |
63 | |
} |
64 | 0 | out.write(b); |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
public void write(byte b[]) throws IOException |
69 | |
{ |
70 | 0 | if (out == null) |
71 | |
{ |
72 | 0 | out = getOutputStream(); |
73 | |
} |
74 | 0 | out.write(b); |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
public void write(byte b[], int off, int len) throws IOException |
79 | |
{ |
80 | 0 | if (out == null) |
81 | |
{ |
82 | 0 | out = getOutputStream(); |
83 | |
} |
84 | 0 | out.write(b, off, len); |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
public void flush() throws IOException |
89 | |
{ |
90 | |
|
91 | 0 | if (out != null) |
92 | |
{ |
93 | 0 | logger.debug("flushing deferred output stream"); |
94 | 0 | out.flush(); |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 0 | logger.debug("deferred output stream unflushed"); |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
public void close() throws IOException |
104 | |
{ |
105 | |
|
106 | 0 | if (out != null) |
107 | |
{ |
108 | 0 | out.close(); |
109 | |
} |
110 | 0 | } |
111 | |
|
112 | |
protected OutputStream getOutputStream() throws IOException |
113 | |
{ |
114 | 0 | StreamMessageAdapter adapter = (StreamMessageAdapter) event.getMessage().getAdapter(); |
115 | 0 | OutputStream temp = getOutputStreamFromRouter(); |
116 | 0 | if (temp == null) |
117 | |
{ |
118 | 0 | temp = adapter.getOutputStream(); |
119 | |
} |
120 | 0 | if (temp == null) |
121 | |
{ |
122 | 0 | throw new IOException("No output stream was found for the current event: " + event); |
123 | |
} |
124 | 0 | else if (getBuffer() > 0) |
125 | |
{ |
126 | 0 | return new BufferedOutputStream(temp, getBuffer()); |
127 | |
} |
128 | |
else |
129 | |
{ |
130 | 0 | return temp; |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
|
135 | |
public int getBuffer() |
136 | |
{ |
137 | 0 | return buffer; |
138 | |
} |
139 | |
|
140 | |
public void setBuffer(int buffer) |
141 | |
{ |
142 | 0 | if (out != null) |
143 | |
{ |
144 | 0 | throw new IllegalStateException("The stream buffer cannot be set after the stream has been written to"); |
145 | |
} |
146 | 0 | this.buffer = buffer; |
147 | 0 | } |
148 | |
|
149 | |
protected OutputStream getOutputStreamFromRouter() throws IOException |
150 | |
{ |
151 | 0 | UMODescriptor descriptor = event.getComponentDescriptor(); |
152 | 0 | UMOEndpointURI endpoint = event.getEndpointURI(); |
153 | |
|
154 | 0 | UMOOutboundRouterCollection messageRouter = descriptor.getOutboundRouter(); |
155 | 0 | if (messageRouter.hasEndpoints()) |
156 | |
{ |
157 | 0 | for (Iterator iterator = messageRouter.getRouters().iterator(); iterator.hasNext();) |
158 | |
{ |
159 | 0 | UMOOutboundRouter router = (UMOOutboundRouter) iterator.next(); |
160 | 0 | boolean match = false; |
161 | |
try |
162 | |
{ |
163 | 0 | match = router.isMatch(event.getMessage()); |
164 | |
} |
165 | 0 | catch (MessagingException e) |
166 | |
{ |
167 | 0 | throw (IOException) new IOException(e.toString()).initCause(e); |
168 | 0 | } |
169 | 0 | if (match) |
170 | |
{ |
171 | 0 | if (router.getEndpoints().size() != 1) |
172 | |
{ |
173 | 0 | throw new IOException( |
174 | |
CoreMessages.streamingComponentMustHaveOneEndpoint(descriptor.getName()).toString()); |
175 | |
} |
176 | |
else |
177 | |
{ |
178 | 0 | UMOEndpoint ep = (UMOEndpoint) router.getEndpoints().get(0); |
179 | |
try |
180 | |
{ |
181 | 0 | return ep.getConnector().getOutputStream(ep, event.getMessage()); |
182 | |
} |
183 | 0 | catch (UMOException e) |
184 | |
{ |
185 | 0 | throw (IOException) new IOException( |
186 | |
CoreMessages.streamingFailedForEndpoint(endpoint.toString()).toString()).initCause(e); |
187 | |
} |
188 | |
} |
189 | |
} |
190 | 0 | } |
191 | |
|
192 | 0 | throw new IOException( |
193 | |
CoreMessages.streamingComponentMustHaveOneEndpoint(descriptor.getName()).toString()); |
194 | |
} |
195 | 0 | if (logger.isDebugEnabled()) |
196 | |
{ |
197 | 0 | logger.debug("there are no outbound endpoints configured on this component, the otput stream provided from the message adapter will be used"); |
198 | |
} |
199 | |
|
200 | |
|
201 | 0 | return null; |
202 | |
} |
203 | |
} |