📄 envloader.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.util;
import java.util.*;
import java.io.*;
/**
* Environment Loader - loads system environment variables int System.properties
*
* @author Jorg Janke
* @version $Id: EnvLoader.java,v 1.3 2002/08/12 01:55:13 danb Exp $
*/
public class EnvLoader
{
private static final boolean DEBUG = false;
/**
* Load System environment variables into System.properies
* <p>
* Prints error messages on System.err
* @param prefix String to prefix variable names
* @return true if success
*/
public static boolean load (String prefix)
{
Properties prop = getEnv(prefix);
if (prop == null)
return false;
// load
Object[] pp = prop.keySet().toArray();
for (int i = 0; i < pp.length; i++)
{
String key = pp[i].toString();
String value = prop.getProperty(key);
System.setProperty(key, value);
}
if (DEBUG)
Log.printProperties(System.getProperties(), "System with Environment", false);
return true;
} // load
/**
* Ger Environment variables
* @param prefix String to prefix variable names
* @return Properties with prefixed system environment variables or null if not successful
*/
public static Properties getEnv (String prefix)
{
String cmd = "cmd /c set"; // Windows
if (!System.getProperty("os.name", "").startsWith("Win"))
cmd = "set"; // Unix/Linux
String result = execCommand (cmd);
if (result == null || result.length() == 0)
return null;
//
if (prefix == null)
prefix = "";
return parseEnv (result, prefix);
} // getEnv
/**
* Execute command and return output
*/
private static String execCommand (String command)
{
Process cmd;
try
{
cmd = Runtime.getRuntime().exec(command);
}
catch (Exception e)
{
System.err.println("-- Error executing command: " + command + " - " + e.toString());
return null;
}
if (DEBUG)
System.out.println("** Command executed: " + command);
StringBuffer bufOut = new StringBuffer();
StringBuffer bufErr = new StringBuffer();
try
{
InputStream in = cmd.getInputStream();
InputStream err = cmd.getErrorStream();
//
int c;
while ((c = in.read()) != -1)
bufOut.append((char)c);
in.close();
//
while ((c = err.read()) != -1)
bufErr.append((char)c);
err.close();
}
catch (Exception e)
{
System.err.println("-- Error reading output: " + e.toString());
return null;
}
if (DEBUG)
{
System.out.println("** Command result: " + bufOut.toString());
System.out.println("** Command error: " + bufErr.toString());
}
return bufOut.toString();
} // execCommand
/**
* Parse Env and return it in properties
*/
private static Properties parseEnv (String input, String prefix)
{
Properties prop = new Properties ();
//
String separator = System.getProperty("line.separator", "\n");
StringTokenizer st = new StringTokenizer(input, separator);
while (st.hasMoreTokens())
{
String s = st.nextToken();
// System.out.println(">" + s + "<");
int pos = s.indexOf("="); // first "="
if (pos > 0)
prop.setProperty(prefix + s.substring(0, pos), s.substring(pos+1));
}
if (DEBUG)
System.out.println("** Loaded " + prop.size() + " Properties");
return prop;
} // parseEnv
} // EnvLoader
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -