added RuntimePermission and System.getenv()

This commit is contained in:
JET 2012-04-02 22:15:02 -06:00
parent 382f016415
commit 2f225795fc
3 changed files with 86 additions and 0 deletions

View File

@ -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)
{

View File

@ -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);
}
}

View File

@ -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<String, String> 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<String, String> 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<String, String>(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();
}