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