1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.tck.functional; |
12 | |
|
13 | |
import org.mule.impl.model.streaming.StreamingService; |
14 | |
import org.mule.umo.UMOEventContext; |
15 | |
import org.mule.util.ClassUtils; |
16 | |
import org.mule.util.StringMessageUtils; |
17 | |
|
18 | |
import java.io.InputStream; |
19 | |
import java.io.OutputStream; |
20 | |
|
21 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger; |
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class FunctionalStreamingTestComponent implements StreamingService |
38 | |
{ |
39 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
40 | |
|
41 | 0 | private static AtomicInteger count = new AtomicInteger(0); |
42 | 0 | private int number = count.incrementAndGet(); |
43 | |
|
44 | |
public static final int STREAM_SAMPLE_SIZE = 4; |
45 | |
public static final int STREAM_BUFFER_SIZE = 4096; |
46 | |
private EventCallback eventCallback; |
47 | 0 | private String summary = null; |
48 | 0 | private long targetSize = -1; |
49 | |
|
50 | |
public FunctionalStreamingTestComponent() |
51 | 0 | { |
52 | 0 | logger.debug("creating " + toString()); |
53 | 0 | } |
54 | |
|
55 | |
public void setEventCallback(EventCallback eventCallback, long targetSize) |
56 | |
{ |
57 | 0 | logger.debug("setting callback: " + eventCallback + " in " + toString()); |
58 | 0 | this.eventCallback = eventCallback; |
59 | 0 | this.targetSize = targetSize; |
60 | 0 | } |
61 | |
|
62 | |
public String getSummary() |
63 | |
{ |
64 | 0 | return summary; |
65 | |
} |
66 | |
|
67 | |
public int getNumber() |
68 | |
{ |
69 | 0 | return number; |
70 | |
} |
71 | |
|
72 | |
public void call(InputStream in, OutputStream unused, UMOEventContext context) throws Exception |
73 | |
{ |
74 | |
try |
75 | |
{ |
76 | 0 | logger.debug("arrived at " + toString()); |
77 | 0 | byte[] startData = new byte[STREAM_SAMPLE_SIZE]; |
78 | 0 | int startDataSize = 0; |
79 | 0 | byte[] endData = new byte[STREAM_SAMPLE_SIZE]; |
80 | 0 | int endDataSize = 0; |
81 | 0 | int endRingPointer = 0; |
82 | 0 | long streamLength = 0; |
83 | 0 | byte[] buffer = new byte[STREAM_BUFFER_SIZE]; |
84 | |
|
85 | |
|
86 | 0 | int bytesRead = 0; |
87 | 0 | while (bytesRead >= 0) |
88 | |
{ |
89 | 0 | bytesRead = in.read(buffer); |
90 | 0 | if (bytesRead > 0) |
91 | |
{ |
92 | 0 | if (logger.isDebugEnabled()) |
93 | |
{ |
94 | 0 | logger.debug("read " + bytesRead + " bytes"); |
95 | |
} |
96 | 0 | streamLength += bytesRead; |
97 | 0 | int startOfEndBytes = 0; |
98 | 0 | for (int i = 0; startDataSize < STREAM_SAMPLE_SIZE && i < bytesRead; ++i) |
99 | |
{ |
100 | 0 | startData[startDataSize++] = buffer[i]; |
101 | 0 | ++startOfEndBytes; |
102 | |
} |
103 | 0 | startOfEndBytes = Math.max(startOfEndBytes, bytesRead - STREAM_SAMPLE_SIZE); |
104 | 0 | for (int i = startOfEndBytes; i < bytesRead; ++i) |
105 | |
{ |
106 | 0 | ++endDataSize; |
107 | 0 | endData[endRingPointer++ % STREAM_SAMPLE_SIZE] = buffer[i]; |
108 | |
} |
109 | 0 | if (streamLength >= targetSize) |
110 | |
{ |
111 | 0 | doCallback(startData, startDataSize, |
112 | |
endData, endDataSize, endRingPointer, |
113 | |
streamLength, context); |
114 | |
} |
115 | |
} |
116 | |
} |
117 | |
|
118 | |
} |
119 | 0 | catch (Exception e) |
120 | |
{ |
121 | 0 | if (logger.isDebugEnabled()) |
122 | |
{ |
123 | 0 | logger.debug(e); |
124 | |
} |
125 | 0 | throw e; |
126 | 0 | } |
127 | 0 | } |
128 | |
|
129 | |
private void doCallback(byte[] startData, int startDataSize, |
130 | |
byte[] endData, int endDataSize, int endRingPointer, |
131 | |
long streamLength, UMOEventContext context) throws Exception |
132 | |
{ |
133 | |
|
134 | 0 | StringBuffer result = new StringBuffer("Received stream"); |
135 | 0 | result.append("; length: "); |
136 | 0 | result.append(streamLength); |
137 | 0 | result.append("; '"); |
138 | |
|
139 | 0 | for (int i = 0; i < startDataSize; ++i) |
140 | |
{ |
141 | 0 | result.append((char) startData[i]); |
142 | |
} |
143 | |
|
144 | 0 | int endSize = Math.min(endDataSize, STREAM_SAMPLE_SIZE); |
145 | 0 | if (endSize > 0) |
146 | |
{ |
147 | 0 | result.append("..."); |
148 | 0 | for (int i = 0; i < endSize; ++i) |
149 | |
{ |
150 | 0 | result.append((char) endData[(endRingPointer + i) % STREAM_SAMPLE_SIZE]); |
151 | |
} |
152 | |
} |
153 | 0 | result.append("'"); |
154 | |
|
155 | 0 | summary = result.toString(); |
156 | |
|
157 | 0 | String msg = StringMessageUtils.getBoilerPlate("Message Received in component: " |
158 | |
+ context.getComponentDescriptor().getName() + ". " + summary |
159 | |
+ "\n callback: " + eventCallback, |
160 | |
'*', 80); |
161 | |
|
162 | 0 | logger.info(msg); |
163 | |
|
164 | 0 | if (eventCallback != null) |
165 | |
{ |
166 | 0 | eventCallback.eventReceived(context, this); |
167 | |
} |
168 | 0 | } |
169 | |
|
170 | |
public String toString() |
171 | |
{ |
172 | 0 | return ClassUtils.getSimpleName(getClass()) + "/" + number; |
173 | |
} |
174 | |
|
175 | |
} |