View Javadoc
1   package org.apache.maven.plugin.surefire.booterclient.output;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.shared.utils.cli.StreamConsumer;
23  import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
24  
25  import java.util.concurrent.BlockingQueue;
26  import java.util.concurrent.LinkedBlockingQueue;
27  
28  /**
29   * Knows how to reconstruct *all* the state transmitted over stdout by the forked process.
30   *
31   * @author Kristian Rosenvold
32   */
33  public final class ThreadedStreamConsumer
34      implements StreamConsumer
35  {
36      private static final String POISON = "Pioson";
37  
38      private static final int ITEM_LIMIT_BEFORE_SLEEP = 10000;
39  
40      private final BlockingQueue<String> items = new LinkedBlockingQueue<String>();
41  
42      private final Thread thread;
43  
44      private final Pumper pumper;
45  
46      static class Pumper
47          implements Runnable
48      {
49          private final BlockingQueue<String> queue;
50  
51          private final StreamConsumer target;
52  
53          private volatile Throwable throwable;
54  
55  
56          Pumper( BlockingQueue<String> queue, StreamConsumer target )
57          {
58              this.queue = queue;
59              this.target = target;
60          }
61  
62          public void run()
63          {
64              try
65              {
66                  String item = queue.take();
67                  //noinspection StringEquality
68                  while ( item != POISON )
69                  {
70                      target.consumeLine( item );
71                      item = queue.take();
72                  }
73              }
74              catch ( Throwable t )
75              {
76                  // Think about what happens if the producer overruns us and creates an OOME. Not nice.
77                  // Maybe limit length of blocking queue
78                  this.throwable = t;
79              }
80          }
81  
82          public Throwable getThrowable()
83          {
84              return throwable;
85          }
86      }
87  
88      public ThreadedStreamConsumer( StreamConsumer target )
89      {
90          pumper = new Pumper( items, target );
91          thread = DaemonThreadFactory.newDaemonThread( pumper, "ThreadedStreamConsumer" );
92          thread.start();
93      }
94  
95      @SuppressWarnings( "checkstyle:emptyblock" )
96      public void consumeLine( String s )
97      {
98          items.add( s );
99          if ( items.size() > ITEM_LIMIT_BEFORE_SLEEP )
100         {
101             try
102             {
103                 Thread.sleep( 100 );
104             }
105             catch ( InterruptedException ignore )
106             {
107             }
108         }
109     }
110 
111 
112     public void close()
113     {
114         try
115         {
116             items.add( POISON );
117             thread.join();
118         }
119         catch ( InterruptedException e )
120         {
121             throw new RuntimeException( e );
122         }
123 
124         //noinspection ThrowableResultOfMethodCallIgnored
125         if ( pumper.getThrowable() != null )
126         {
127             throw new RuntimeException( pumper.getThrowable() );
128         }
129     }
130 }