1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.impl.model.resolvers; |
12 | |
|
13 | |
import org.mule.impl.NoSatisfiableMethodsException; |
14 | |
import org.mule.impl.TooManySatisfiableMethodsException; |
15 | |
import org.mule.impl.VoidResult; |
16 | |
import org.mule.impl.model.streaming.DeferredOutputStream; |
17 | |
import org.mule.impl.model.streaming.StreamingService; |
18 | |
import org.mule.providers.streaming.StreamMessageAdapter; |
19 | |
import org.mule.umo.UMOEventContext; |
20 | |
import org.mule.umo.model.UMOEntryPoint; |
21 | |
import org.mule.util.ClassUtils; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.io.OutputStream; |
26 | |
import java.lang.reflect.Method; |
27 | |
import java.util.List; |
28 | |
|
29 | |
import org.apache.commons.logging.Log; |
30 | |
import org.apache.commons.logging.LogFactory; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class StreamingEntryPoint implements UMOEntryPoint |
42 | |
{ |
43 | 0 | protected static final Log logger = LogFactory.getLog(StreamingEntryPoint.class); |
44 | |
private Method streamingMethod; |
45 | 0 | private boolean inAndOut = false; |
46 | |
|
47 | |
public void initialise(Object component) throws Exception |
48 | |
{ |
49 | 0 | if (component instanceof StreamingService) |
50 | |
{ |
51 | 0 | streamingMethod = StreamingService.class.getMethods()[0]; |
52 | |
} |
53 | |
else |
54 | |
{ |
55 | 0 | inAndOut = true; |
56 | 0 | List methods = ClassUtils.getSatisfiableMethods(component.getClass(), new Class[]{ |
57 | |
InputStream.class, OutputStream.class}, true, false, null); |
58 | |
|
59 | 0 | if (methods.size() == 0) |
60 | |
{ |
61 | 0 | inAndOut = false; |
62 | 0 | methods = ClassUtils.getSatisfiableMethods(component.getClass(), |
63 | |
new Class[]{InputStream.class}, true, false, null); |
64 | |
} |
65 | |
|
66 | 0 | if (methods.size() == 0) |
67 | |
{ |
68 | 0 | throw new NoSatisfiableMethodsException(component, new Class[]{InputStream.class}, |
69 | |
new NoSatisfiableMethodsException(component, new Class[]{InputStream.class, |
70 | |
OutputStream.class})); |
71 | |
} |
72 | 0 | else if (methods.size() > 1) |
73 | |
{ |
74 | 0 | throw new TooManySatisfiableMethodsException(component, new Class[]{InputStream.class, |
75 | |
OutputStream.class}); |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | streamingMethod = (Method) methods.get(0); |
80 | |
} |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | |
public Object invoke(Object component, UMOEventContext context) throws Exception |
85 | |
{ |
86 | 0 | if (streamingMethod == null) |
87 | |
{ |
88 | 0 | initialise(component); |
89 | |
} |
90 | |
|
91 | 0 | StreamMessageAdapter adapter = (StreamMessageAdapter) context.getMessage().getAdapter(); |
92 | 0 | OutputStream out = new DeferredOutputStream(context); |
93 | |
|
94 | |
try |
95 | |
{ |
96 | |
Object result; |
97 | |
|
98 | 0 | if (component instanceof StreamingService) |
99 | |
{ |
100 | 0 | result = streamingMethod.invoke(component, new Object[]{adapter.getInputStream(), out, |
101 | |
context}); |
102 | |
} |
103 | 0 | else if (inAndOut) |
104 | |
{ |
105 | 0 | result = streamingMethod.invoke(component, new Object[]{adapter.getInputStream(), out}); |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | 0 | result = streamingMethod.invoke(component, new Object[]{adapter.getInputStream()}); |
110 | |
} |
111 | |
|
112 | 0 | if (streamingMethod.getReturnType().equals(Void.TYPE)) |
113 | |
{ |
114 | 0 | result = VoidResult.getInstance(); |
115 | |
} |
116 | |
|
117 | 0 | return result; |
118 | |
} |
119 | 0 | catch (Exception e) |
120 | |
{ |
121 | 0 | logger.warn("Failed to route streaming event via " + component + ": " + e.getMessage(), e); |
122 | 0 | throw e; |
123 | |
} |
124 | |
finally |
125 | |
{ |
126 | 0 | try |
127 | |
{ |
128 | 0 | out.flush(); |
129 | |
} |
130 | 0 | catch (IOException e) |
131 | |
{ |
132 | |
|
133 | |
|
134 | 0 | } |
135 | |
} |
136 | |
} |
137 | |
} |