View Javadoc
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.util.Collection;
23  import java.util.Collections;
24  import java.util.Properties;
25  import java.util.concurrent.ConcurrentLinkedDeque;
26  
27  import org.apache.maven.shared.utils.cli.CommandLineException;
28  import org.apache.maven.shared.utils.cli.CommandLineUtils;
29  import org.apache.maven.shared.utils.cli.Commandline;
30  
31  /**
32   * A {@link Commandline} implementation that provides the output stream of
33   * the executed process in form of a {@link FlushReceiver}, for it to be
34   * flushed on demand.
35   *
36   * @author Andreas Gudian
37   */
38  public class OutputStreamFlushableCommandline
39      extends Commandline
40      implements FlushReceiverProvider
41  {
42      private final Collection<String> excludedEnvironmentVariables;
43      private volatile FlushReceiver flushReceiver;
44  
45      /**
46       * for testing purposes only
47       */
48      public OutputStreamFlushableCommandline()
49      {
50          this( new String[0] );
51      }
52  
53      public OutputStreamFlushableCommandline( String[] excludedEnvironmentVariables )
54      {
55          this.excludedEnvironmentVariables = new ConcurrentLinkedDeque<>();
56          Collections.addAll( this.excludedEnvironmentVariables, excludedEnvironmentVariables );
57      }
58  
59      @Override
60      public final void addSystemEnvironment()
61      {
62          Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
63  
64          for ( String key : systemEnvVars.stringPropertyNames() )
65          {
66              if ( !excludedEnvironmentVariables.contains( key ) )
67              {
68                  addEnvironment( key, systemEnvVars.getProperty( key ) );
69              }
70          }
71      }
72  
73      @Override
74      public Process execute()
75          throws CommandLineException
76      {
77          Process process = super.execute();
78  
79          if ( process.getOutputStream() != null )
80          {
81              flushReceiver = new OutputStreamFlushReceiver( process.getOutputStream() );
82          }
83  
84          return process;
85      }
86  
87      @Override
88      public FlushReceiver getFlushReceiver()
89      {
90          return flushReceiver;
91      }
92  }