View Javadoc
1   package org.codehaus.mojo.exec;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.codehaus.plexus.util.StringUtils;
9   import org.codehaus.plexus.util.cli.StreamConsumer;
10  
11  public class EnvStreamConsumer
12      implements StreamConsumer
13  {
14  
15      public static final String START_PARSING_INDICATOR =
16          "================================This is the beginning of env parsing================================";
17  
18      private Map<String, String> envs = new HashMap<String, String>();
19  
20      private List<String> unparsed = new ArrayList<String>();
21  
22      private boolean startParsing = false;
23  
24      public void consumeLine( String line )
25      {
26  
27          if ( line.startsWith( START_PARSING_INDICATOR ) )
28          {
29              this.startParsing = true;
30              return;
31          }
32  
33          if ( this.startParsing )
34          {
35              String[] tokens = StringUtils.split( line, "=", 2 );
36              if ( tokens.length == 2 )
37              {
38                  envs.put( tokens[0], tokens[1] );
39              } else {
40                  // Don't hide an environment variable with no value e.g. APP_OVERRIDE=
41                  String trimmedLine = line.trim();
42                  if ( trimmedLine.endsWith("=") )
43                  {
44                      envs.put( trimmedLine.substring( 0, ( trimmedLine.length() - 1 ) ), null );
45                  }
46                  else
47                  {
48                      unparsed.add( line );
49                  }
50              }
51          }
52          else
53          {
54              System.out.println( line );
55          }
56  
57      }
58  
59      public Map<String, String> getParsedEnv()
60      {
61          return this.envs;
62      }
63  
64      public List<String> getUnparsedLines()
65      {
66          return this.unparsed;
67      }
68  }