View Javadoc
1   package org.codehaus.mojo.natives.util;
2   
3   import java.util.Iterator;
4   import java.util.Map;
5   
6   import org.codehaus.mojo.natives.EnvFactory;
7   import org.codehaus.mojo.natives.NativeBuildException;
8   import org.codehaus.plexus.util.cli.Commandline;
9   
10  public class EnvUtil
11  {
12      public static String getEnv( String envKey )
13      {
14          return getEnv( envKey, "" );
15      }
16  
17      public static String getEnv( String envKey, String defaultValue )
18      {
19          return getEnv( envKey, null, defaultValue );
20      }
21  
22      public static String getEnv( String envKey, String alternateSystemProperty, String defaultValue )
23      {
24          String envValue = null;
25  
26          try
27          {
28              // TODO move this to an env object similar to Ant Environment
29              envValue = System.getenv( envKey );
30  
31              if ( envValue == null && alternateSystemProperty != null )
32              {
33                  envValue = System.getProperty( alternateSystemProperty );
34              }
35          }
36          catch ( Error e )
37          {
38              // JDK 1.4?
39              if ( alternateSystemProperty != null )
40              {
41                  envValue = getProperty( alternateSystemProperty );
42              }
43          }
44  
45          if ( envValue == null )
46          {
47              envValue = defaultValue;
48          }
49  
50          return envValue;
51      }
52  
53      private static String getProperty( String key )
54      {
55          if ( key != null )
56          {
57              return System.getProperty( key );
58          }
59  
60          return null;
61      }
62  
63      public static void setupCommandlineEnv( Commandline cl, EnvFactory envFactory )
64          throws NativeBuildException
65      {
66          if ( envFactory != null )
67          {
68              Map envs = envFactory.getEnvironmentVariables();
69  
70              Iterator iter = envs.keySet().iterator();
71  
72              while ( iter.hasNext() )
73              {
74                  String key = (String) iter.next();
75                  cl.addEnvironment( key, (String) envs.get( key ) );
76              }
77          }
78      }
79  
80  }