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 public void flush()
52 throws IOException
53 {
54 outputStream.flush();
55 }
56 }
57
58 private FlushReceiver flushReceiver;
59
60 @Override
61 public Process execute()
62 throws CommandLineException
63 {
64 Process process = super.execute();
65
66 if ( process.getOutputStream() != null )
67 {
68 flushReceiver = new OutputStreamFlushReceiver( process.getOutputStream() );
69 }
70
71 return process;
72 }
73
74 public FlushReceiver getFlushReceiver()
75 {
76 return flushReceiver;
77 }
78
79 }