From 2f225795fc1e4a28a1bc5bf7d7fd7159320df1cf Mon Sep 17 00:00:00 2001 From: JET Date: Mon, 2 Apr 2012 22:15:02 -0600 Subject: [PATCH] added RuntimePermission and System.getenv() --- classpath/java-lang.cpp | 29 +++++++++++++++++ classpath/java/lang/RuntimePermission.java | 21 +++++++++++++ classpath/java/lang/System.java | 36 ++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 classpath/java/lang/RuntimePermission.java diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index bb087560fe..9e99dda6cf 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -631,6 +631,35 @@ Java_java_lang_System_getProperty(JNIEnv* e, jclass, jstring name, return r; } +// System.getEnvironment() implementation +// TODO: For Win32, replace usage of deprecated _wenviron +#ifndef PLATFORM_WINDOWS +extern char** environ; +# else +extern wchar_t** _wenviron; +const wchar_t** environ = _wenviron; +#endif +extern "C" JNIEXPORT jobjectArray JNICALL +Java_java_lang_System_getEnvironment(JNIEnv* env, jclass) { + int length; + for (length = 0; environ[length] != 0; ++length); + + jobjectArray stringArray = + env->NewObjectArray(length, env->FindClass("java/lang/String"), + env->NewStringUTF("")); + + for (int i = 0; i < length; i++) { +#ifndef PLATFORM_WINDOWS + jobject varString = env->NewStringUTF(environ[i]); // UTF-8 +#else + jobject varString = env->NewString(environ[i]); // UTF-16 +#endif + env->SetObjectArrayElement(stringArray, i, varString); + } + + return stringArray; +} + extern "C" JNIEXPORT jlong JNICALL Java_java_lang_System_currentTimeMillis(JNIEnv*, jclass) { diff --git a/classpath/java/lang/RuntimePermission.java b/classpath/java/lang/RuntimePermission.java new file mode 100644 index 0000000000..a737444a6f --- /dev/null +++ b/classpath/java/lang/RuntimePermission.java @@ -0,0 +1,21 @@ +/* Copyright (c) 2012, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.lang; + +import java.security.BasicPermission; + +public class RuntimePermission extends BasicPermission { + + public RuntimePermission(String name) { + super(name); + } + +} diff --git a/classpath/java/lang/System.java b/classpath/java/lang/System.java index 9506fb3457..827f357e8b 100644 --- a/classpath/java/lang/System.java +++ b/classpath/java/lang/System.java @@ -17,12 +17,15 @@ import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileDescriptor; +import java.util.Map; +import java.util.Hashtable; import java.util.Properties; public abstract class System { private static final long NanoTimeBaseInMillis = currentTimeMillis(); private static Property properties; + private static Map environment; private static SecurityManager securityManager; // static { @@ -145,4 +148,37 @@ public abstract class System { this.next = next; } } + + public static String getenv(String name) throws NullPointerException, + SecurityException { + if (getSecurityManager() != null) { // is this allowed? + getSecurityManager(). + checkPermission(new RuntimePermission("getenv." + name)); + } + return getenv().get(name); + } + + public static Map getenv() throws SecurityException { + if (getSecurityManager() != null) { // is this allowed? + getSecurityManager().checkPermission(new RuntimePermission("getenv.*")); + } + + if (environment == null) { // build environment table + String[] vars = getEnvironment(); + environment = new Hashtable(vars.length); + for (String var : vars) { // parse name-value pairs + int equalsIndex = var.indexOf('='); + // null names and values are forbidden + if (equalsIndex != -1 && equalsIndex < var.length() - 1) { + environment.put(var.substring(0, equalsIndex), + var.substring(equalsIndex + 1)); + } + } + } + + return environment; + } + + /** Returns the native process environment. */ + private static native String[] getEnvironment(); }