KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > EnvLoader


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.util.*;
17 import java.io.*;
18
19 /**
20  * Environment Loader - loads system environment variables int System.properties
21  *
22  * @author Jorg Janke
23  * @version $Id: EnvLoader.java,v 1.3 2002/08/12 01:55:13 danb Exp $
24  */

25 public class EnvLoader
26 {
27     private static final boolean DEBUG = false;
28
29     /**
30      * Load System environment variables into System.properies
31      * <p>
32      * Prints error messages on System.err
33      * @param prefix String to prefix variable names
34      * @return true if success
35      */

36     public static boolean load (String JavaDoc prefix)
37     {
38         Properties prop = getEnv(prefix);
39         if (prop == null)
40             return false;
41         // load
42
Object JavaDoc[] pp = prop.keySet().toArray();
43         for (int i = 0; i < pp.length; i++)
44         {
45             String JavaDoc key = pp[i].toString();
46             String JavaDoc value = prop.getProperty(key);
47             System.setProperty(key, value);
48         }
49         if (DEBUG)
50             Log.printProperties(System.getProperties(), "System with Environment", false);
51         return true;
52     } // load
53

54     /**
55      * Ger Environment variables
56      * @param prefix String to prefix variable names
57      * @return Properties with prefixed system environment variables or null if not successful
58      */

59     public static Properties getEnv (String JavaDoc prefix)
60     {
61         String JavaDoc cmd = "cmd /c set"; // Windows
62
if (!System.getProperty("os.name", "").startsWith("Win"))
63             cmd = "set"; // Unix/Linux
64
String JavaDoc result = execCommand (cmd);
65         if (result == null || result.length() == 0)
66             return null;
67         //
68
if (prefix == null)
69             prefix = "";
70         return parseEnv (result, prefix);
71     } // getEnv
72

73     /**
74      * Execute command and return output
75      */

76     private static String JavaDoc execCommand (String JavaDoc command)
77     {
78         Process JavaDoc cmd;
79         try
80         {
81             cmd = Runtime.getRuntime().exec(command);
82         }
83         catch (Exception JavaDoc e)
84         {
85             System.err.println("-- Error executing command: " + command + " - " + e.toString());
86             return null;
87         }
88         if (DEBUG)
89             System.out.println("** Command executed: " + command);
90
91         StringBuffer JavaDoc bufOut = new StringBuffer JavaDoc();
92         StringBuffer JavaDoc bufErr = new StringBuffer JavaDoc();
93         try
94         {
95             InputStream in = cmd.getInputStream();
96             InputStream err = cmd.getErrorStream();
97             //
98
int c;
99             while ((c = in.read()) != -1)
100                 bufOut.append((char)c);
101             in.close();
102             //
103
while ((c = err.read()) != -1)
104                 bufErr.append((char)c);
105             err.close();
106         }
107         catch (Exception JavaDoc e)
108         {
109             System.err.println("-- Error reading output: " + e.toString());
110             return null;
111         }
112         if (DEBUG)
113         {
114             System.out.println("** Command result: " + bufOut.toString());
115             System.out.println("** Command error: " + bufErr.toString());
116         }
117         return bufOut.toString();
118     } // execCommand
119

120     /**
121      * Parse Env and return it in properties
122      */

123     private static Properties parseEnv (String JavaDoc input, String JavaDoc prefix)
124     {
125         Properties prop = new Properties ();
126         //
127
String JavaDoc separator = System.getProperty("line.separator", "\n");
128         StringTokenizer st = new StringTokenizer(input, separator);
129         while (st.hasMoreTokens())
130         {
131             String JavaDoc s = st.nextToken();
132         // System.out.println(">" + s + "<");
133
int pos = s.indexOf("="); // first "="
134
if (pos > 0)
135                 prop.setProperty(prefix + s.substring(0, pos), s.substring(pos+1));
136         }
137         if (DEBUG)
138             System.out.println("** Loaded " + prop.size() + " Properties");
139         return prop;
140     } // parseEnv
141

142 } // EnvLoader
143
Popular Tags