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.HashSet;
24  import java.util.Properties;
25  import java.util.Set;
26  import java.util.concurrent.ConcurrentLinkedDeque;
27  
28  import org.apache.maven.surefire.shared.utils.cli.CommandLineUtils;
29  
30  import static java.util.Collections.addAll;
31  
32  /**
33   * A {@link org.apache.maven.surefire.shared.utils.cli.Commandline} implementation.
34   *
35   * @author Andreas Gudian
36   */
37  public class Commandline
38      extends org.apache.maven.surefire.shared.utils.cli.Commandline
39  {
40      private final Collection<String> excludedEnvironmentVariables;
41      private final Set<String> addedEnvironmentVariables;
42  
43      /**
44       * for testing purposes only
45       */
46      public Commandline()
47      {
48          this( new String[0] );
49      }
50  
51      public Commandline( String[] excludedEnvironmentVariables )
52      {
53          this.excludedEnvironmentVariables = new ConcurrentLinkedDeque<>();
54          addedEnvironmentVariables = new HashSet<>();
55          addAll( this.excludedEnvironmentVariables, excludedEnvironmentVariables );
56      }
57  
58      @Override
59      public void addEnvironment( String name, String value )
60      {
61          super.addEnvironment( name, value );
62          addedEnvironmentVariables.add( name );
63      }
64  
65      @Override
66      public final void addSystemEnvironment()
67      {
68          Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
69  
70          for ( String key : systemEnvVars.stringPropertyNames() )
71          {
72              if ( !addedEnvironmentVariables.contains( key ) && !excludedEnvironmentVariables.contains( key ) )
73              {
74                  addEnvironment( key, systemEnvVars.getProperty( key ) );
75              }
76          }
77      }
78  }