Фикс чита на основе глобальных переменных часть 2

This commit is contained in:
Gravit 2018-10-22 14:15:44 +07:00
parent 6091be7606
commit 4e75894438

View file

@ -7,54 +7,21 @@
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class EnvHelper { public class EnvHelper {
private static final boolean TST;
private static final boolean HASXW;
public static final String[] toTest; public static final String[] toTest;
public static final Pattern[] test;
static { static {
toTest = new String[] { "_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS" }; toTest = new String[] { "_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS" };
test = new Pattern[] { Pattern.compile("-xm.*\\d+[KMG]") };
TST = check0();
HASXW = check1();
} }
public static void addEnv(ProcessBuilder builder) { public static void addEnv(ProcessBuilder builder) {
if (hasOptsEnv()) { Map<String, String> repl = builder.environment();
Map<String, String> repl = new HashMap<>();
for (String str : toTest) { for (String str : toTest) {
repl.put(str, ""); repl.put(str, "");
repl.put(str.toLowerCase(Locale.US), ""); repl.put(str.toLowerCase(Locale.US), "");
} }
JVMHelper.appendVars(builder, repl);
}
}
private static boolean check0() {
for (String test : toTest)
if (System.getProperty(test) != null)
return true;
return false;
}
/**
* Вынужденное решение ибо тест на наличие -Xm* этакой нужен.
*/
private static boolean check1() {
if (hasOptsEnv())
for (String testStr : toTest)
if (System.getenv(testStr) != null) {
String str = System.getenv(testStr).toLowerCase(Locale.US);
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens())
if (CommonHelper.multiMatches(test, st.nextToken()))
return true;
}
return false;
} }
public static void checkDangerousParametrs() { public static void checkDangerousParametrs() {
if (hasOptsEnv())
for (String t : toTest) for (String t : toTest)
if (System.getenv(t) != null) { if (System.getenv(t) != null) {
String env = System.getenv(t).toLowerCase(Locale.US); String env = System.getenv(t).toLowerCase(Locale.US);
@ -63,12 +30,4 @@ public static void checkDangerousParametrs() {
throw new SecurityException("JavaAgent in global optings not allow"); throw new SecurityException("JavaAgent in global optings not allow");
} }
} }
public static boolean hasOptsEnv() {
return TST;
}
public static boolean hasWarnPreDef() {
return HASXW;
}
} }