1 package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
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 java.io.IOException;
23 import java.io.OutputStream;
24 import org.apache.maven.shared.utils.cli.CommandLineException;
25 import org.apache.maven.shared.utils.cli.Commandline;
26
27 /**
28 * A {@link Commandline} implementation that provides the output stream of
29 * the executed process in form of a {@link FlushReceiver}, for it to be
30 * flushed on demand.
31 *
32 * @author Andreas Gudian
33 */
34 public class OutputStreamFlushableCommandline
35 extends Commandline
36 implements FlushReceiverProvider
37 {
38 /**
39 * Wraps an output stream in order to delegate a flush.
40 */
41 private final class OutputStreamFlushReceiver
42 implements FlushReceiver
43 {
44 private final OutputStream outputStream;
45
46 private OutputStreamFlushReceiver( OutputStream outputStream )
47 {
48 this.outputStream = outputStream;
49 }
50
51 @Override
52 public void flush()
53 throws IOException
54 {
55 outputStream.flush();
56 }
57 }
58
59 private volatile FlushReceiver flushReceiver;
60
61 @Override
62 public Process execute()
63 throws CommandLineException
64 {
65 Process process = super.execute();
66
67 if ( process.getOutputStream() != null )
68 {
69 flushReceiver = new OutputStreamFlushReceiver( process.getOutputStream() );
70 }
71
72 return process;
73 }
74
75 @Override
76 public FlushReceiver getFlushReceiver()
77 {
78 return flushReceiver;
79 }
80
81 }