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.plugin.surefire.util.internal.BlockingQueue; |
23 | |
import org.apache.maven.plugin.surefire.util.internal.Java15BlockingQueue; |
24 | |
import org.apache.maven.shared.utils.cli.StreamConsumer; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class ThreadedStreamConsumer |
32 | |
implements StreamConsumer |
33 | |
{ |
34 | |
|
35 | 0 | private final BlockingQueue items = new Java15BlockingQueue(); |
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 BlockingQueue queue; |
47 | |
|
48 | |
private final StreamConsumer target; |
49 | |
|
50 | |
private volatile InterruptedException interruptedException; |
51 | |
|
52 | |
|
53 | |
Pumper( BlockingQueue 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 ( InterruptedException e ) |
72 | |
{ |
73 | 0 | this.interruptedException = e; |
74 | 0 | } |
75 | 0 | } |
76 | |
|
77 | |
public InterruptedException getInterruptedException() |
78 | |
{ |
79 | 0 | return interruptedException; |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
public ThreadedStreamConsumer( StreamConsumer target ) |
84 | 0 | { |
85 | 0 | pumper = new Pumper( items, target ); |
86 | 0 | thread = new Thread( pumper, "ThreadedStreamConsumer" ); |
87 | 0 | thread.start(); |
88 | 0 | } |
89 | |
|
90 | |
public void consumeLine( String s ) |
91 | |
{ |
92 | 0 | items.add( s ); |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
public void close() |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | items.add( poison ); |
101 | 0 | thread.join(); |
102 | |
|
103 | 0 | if ( pumper.getInterruptedException() != null ) |
104 | |
{ |
105 | 0 | throw pumper.getInterruptedException(); |
106 | |
} |
107 | |
} |
108 | 0 | catch ( InterruptedException e ) |
109 | |
{ |
110 | 0 | throw new RuntimeException( e ); |
111 | 0 | } |
112 | 0 | } |
113 | |
} |