1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.util.StreamCloser; |
15 | |
import org.mule.api.util.StreamCloserService; |
16 | |
|
17 | |
import java.io.IOException; |
18 | |
import java.io.InputStream; |
19 | |
import java.util.Iterator; |
20 | |
|
21 | |
import javax.xml.transform.sax.SAXSource; |
22 | |
import javax.xml.transform.stream.StreamSource; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
import org.xml.sax.InputSource; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class DefaultStreamCloserService implements StreamCloserService |
33 | |
{ |
34 | |
|
35 | 0 | private static final Log log = LogFactory.getLog(DefaultStreamCloserService.class); |
36 | |
|
37 | |
private MuleContext muleContext; |
38 | 0 | private StreamCloser coreStreamTypesCloser = new CoreStreamTypesCloser(); |
39 | |
|
40 | |
public void closeStream(Object stream) |
41 | |
{ |
42 | |
try |
43 | |
{ |
44 | 0 | if (coreStreamTypesCloser.canClose(stream.getClass())) |
45 | |
{ |
46 | 0 | coreStreamTypesCloser.close(stream); |
47 | |
} |
48 | |
else |
49 | |
{ |
50 | 0 | Iterator closers = muleContext.getRegistry().lookupObjects(StreamCloser.class).iterator(); |
51 | 0 | while (closers.hasNext()) |
52 | |
{ |
53 | 0 | StreamCloser closer = (StreamCloser) closers.next(); |
54 | 0 | if (closer.canClose(stream.getClass())) |
55 | |
{ |
56 | 0 | closer.close(stream); |
57 | |
} |
58 | |
else |
59 | |
{ |
60 | 0 | log.debug("Unable to find an StreamCloser for the stream type: " + stream.getClass() |
61 | |
+ ", the stream: " + stream + " will not be closed."); |
62 | |
} |
63 | 0 | } |
64 | |
} |
65 | |
} |
66 | 0 | catch (Exception e) |
67 | |
{ |
68 | 0 | log.debug("Exception closing stream: " + stream, e); |
69 | 0 | } |
70 | |
|
71 | 0 | } |
72 | |
|
73 | |
public void setMuleContext(MuleContext context) |
74 | |
{ |
75 | 0 | muleContext = context; |
76 | 0 | } |
77 | |
|
78 | 0 | static class CoreStreamTypesCloser implements StreamCloser |
79 | |
{ |
80 | |
|
81 | |
public boolean canClose(Class streamType) |
82 | |
{ |
83 | 0 | return InputStream.class.isAssignableFrom(streamType) |
84 | |
|| InputSource.class.isAssignableFrom(streamType) |
85 | |
|| StreamSource.class.isAssignableFrom(streamType) |
86 | |
|| (SAXSource.class.isAssignableFrom(streamType) && !streamType.getName().endsWith( |
87 | |
"StaxSource")); |
88 | |
} |
89 | |
|
90 | |
public void close(Object stream) throws IOException |
91 | |
{ |
92 | 0 | if (stream instanceof InputStream) |
93 | |
{ |
94 | |
try |
95 | |
{ |
96 | 0 | ((InputStream) stream).close(); |
97 | |
} |
98 | 0 | catch (IOException e) |
99 | |
{ |
100 | |
|
101 | 0 | } |
102 | |
} |
103 | 0 | else if (stream instanceof InputSource) |
104 | |
{ |
105 | 0 | closeInputSourceStream((InputSource) stream); |
106 | |
} |
107 | 0 | else if (stream instanceof SAXSource) |
108 | |
{ |
109 | 0 | closeInputSourceStream(((SAXSource) stream).getInputSource()); |
110 | |
} |
111 | 0 | else if (stream instanceof StreamSource) |
112 | |
{ |
113 | |
try |
114 | |
{ |
115 | 0 | ((StreamSource) stream).getInputStream().close(); |
116 | |
} |
117 | 0 | catch (IOException e) |
118 | |
{ |
119 | 0 | } |
120 | |
} |
121 | 0 | } |
122 | |
|
123 | |
private void closeInputSourceStream(InputSource payload) throws IOException |
124 | |
{ |
125 | 0 | if (payload.getByteStream() != null) |
126 | |
{ |
127 | 0 | payload.getByteStream().close(); |
128 | |
} |
129 | 0 | else if (payload.getCharacterStream() != null) |
130 | |
{ |
131 | 0 | payload.getCharacterStream().close(); |
132 | |
} |
133 | 0 | } |
134 | |
|
135 | |
} |
136 | |
|
137 | |
} |