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 private final java.util.concurrent.BlockingQueue<String> items = new LinkedBlockingQueue<String>();
35
36 private static final String POISON = "Pioson";
37 private static final int ITEM_LIMIT_BEFORE_SLEEP = 10000;
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 {
55 this.queue = queue;
56 this.target = target;
57 }
58
59 public void run()
60 {
61 try
62 {
63 String item = queue.take();
64
65 while ( item != POISON )
66 {
67 target.consumeLine( item );
68 item = queue.take();
69 }
70 }
71 catch ( Throwable t )
72 {
73
74
75 this.throwable = t;
76 }
77 }
78
79 public Throwable getThrowable()
80 {
81 return throwable;
82 }
83 }
84
85 public ThreadedStreamConsumer( StreamConsumer target )
86 {
87 pumper = new Pumper( items, target );
88 thread = new Thread( pumper, "ThreadedStreamConsumer" );
89 thread.start();
90 }
91
92 @SuppressWarnings( "checkstyle:emptyblock" )
93 public void consumeLine( String s )
94 {
95 items.add( s );
96 if ( items.size() > ITEM_LIMIT_BEFORE_SLEEP )
97 {
98 try
99 {
100 Thread.sleep( 100 );
101 }
102 catch ( InterruptedException ignore )
103 {
104 }
105 }
106 }
107
108
109 public void close()
110 {
111 try
112 {
113 items.add( POISON );
114 thread.join();
115 }
116 catch ( InterruptedException e )
117 {
118 throw new RuntimeException( e );
119 }
120
121
122 if ( pumper.getThrowable() != null )
123 {
124 throw new RuntimeException( pumper.getThrowable() );
125 }
126 }
127 }