1 | |
package org.apache.maven.plugin.surefire.booterclient.output; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.shared.utils.cli.StreamConsumer; |
23 | |
|
24 | |
import java.util.concurrent.LinkedBlockingQueue; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class ThreadedStreamConsumer |
32 | |
implements StreamConsumer |
33 | |
{ |
34 | |
|
35 | 0 | private final java.util.concurrent.BlockingQueue<String> items = new LinkedBlockingQueue<String>(); |
36 | |
|
37 | |
private static final String poison = "Pioson"; |
38 | |
|
39 | |
private final Thread thread; |
40 | |
|
41 | |
private final Pumper pumper; |
42 | |
|
43 | |
static class Pumper |
44 | |
implements Runnable |
45 | |
{ |
46 | |
private final java.util.concurrent.BlockingQueue<String> queue; |
47 | |
|
48 | |
private final StreamConsumer target; |
49 | |
|
50 | |
private volatile Throwable throwable; |
51 | |
|
52 | |
|
53 | |
Pumper( java.util.concurrent.BlockingQueue<String> queue, StreamConsumer target ) |
54 | 0 | { |
55 | 0 | this.queue = queue; |
56 | 0 | this.target = target; |
57 | 0 | } |
58 | |
|
59 | |
public void run() |
60 | |
{ |
61 | |
try |
62 | |
{ |
63 | 0 | String item = queue.take(); |
64 | |
|
65 | 0 | while ( item != poison ) |
66 | |
{ |
67 | 0 | target.consumeLine( item ); |
68 | 0 | item = queue.take(); |
69 | |
} |
70 | |
} |
71 | 0 | catch ( Throwable t ) |
72 | |
{ |
73 | |
|
74 | |
|
75 | 0 | this.throwable = t; |
76 | 0 | } |
77 | 0 | } |
78 | |
|
79 | |
public Throwable getThrowable() |
80 | |
{ |
81 | 0 | return throwable; |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
public ThreadedStreamConsumer( StreamConsumer target ) |
86 | 0 | { |
87 | 0 | pumper = new Pumper( items, target ); |
88 | 0 | thread = new Thread( pumper, "ThreadedStreamConsumer" ); |
89 | 0 | thread.start(); |
90 | 0 | } |
91 | |
|
92 | |
public void consumeLine( String s ) |
93 | |
{ |
94 | 0 | items.add( s ); |
95 | 0 | if ( items.size() > 10000 ) |
96 | |
{ |
97 | |
try |
98 | |
{ |
99 | 0 | Thread.sleep( 100 ); |
100 | |
} |
101 | 0 | catch ( InterruptedException ignore ) |
102 | |
{ |
103 | 0 | } |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
public void close() |
109 | |
{ |
110 | |
try |
111 | |
{ |
112 | 0 | items.add( poison ); |
113 | 0 | thread.join(); |
114 | |
} |
115 | 0 | catch ( InterruptedException e ) |
116 | |
{ |
117 | 0 | throw new RuntimeException( e ); |
118 | 0 | } |
119 | |
|
120 | |
|
121 | 0 | if ( pumper.getThrowable() != null ) |
122 | |
{ |
123 | 0 | throw new RuntimeException( pumper.getThrowable() ); |
124 | |
} |
125 | 0 | } |
126 | |
} |