datasource/Cached_Content_Loader: added caching content loader
[metux-java.git] / util / Environment.java
blobc57e668f4ca65c20d1d2cfd499d19bba85c2e6a4
2 package org.de.metux.util;
4 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.util.Properties;
7 import java.io.InputStreamReader;
9 /*
10 grabbed this code somewhere on the web
12 we need it since stupid folks @ sun tend to remove essential functions
13 from java-spec and other stupid folks on various vm projects had
14 to follow them.
16 this class is a really ugly hack!
17 imagine: we have to spawn another process just for retrieving
18 the env of this process.
19 against the wall w/ the dude who decided to remove System.getenv()
22 public class Environment extends Properties
24 public Environment()
26 super();
27 String cmd;
28 String os = System.getProperty("os.name").toLowerCase();
30 if (os.indexOf("windows")!=-1)
31 cmd = ((os.indexOf("95")!=-1 ||
32 (os.indexOf("98")!=-1) ||
33 (os.indexOf("ME")!=-1)) ? "command.com":"cmd")+" /Cset";
34 else
35 cmd = "env";
37 try
39 Process p = Runtime.getRuntime().exec(cmd);
40 load(p.getInputStream());
41 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
42 for (String line=""; (line=br.readLine())!=null;)
44 int pos = line.indexOf("=");
45 setProperty(line.substring(0,pos),line.substring(pos+1));
47 br.close();
49 catch (IOException e)
51 setProperty("__EXCEPTION", e.toString());
55 static Environment env;
57 public static String getenv(String name)
59 if (env==null)
60 env = new Environment();
61 return env.getProperty(name);
64 public static String getenv(String name, String def)
66 String s = getenv(name);
67 if (s == null)
68 return def;
69 return s;
72 public static boolean getenv_bool(String name, boolean def)
74 String val = getenv(name,null);
75 if (val==null)
76 return def;
77 val = val.toLowerCase();
79 if (val.equals("on")||val.equals("yes")||val.equals("true")||val.equals("1"))
80 return true;
81 if (val.equals("off")||val.equals("no")||val.equals("false")||val.equals("0"))
82 return false;
84 throw new RuntimeException("getenv_bool(): could not decode \""+val+"\"");