0N/A/*
3261N/A * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <string.h>
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "java_props.h"
0N/A
0N/A#include "java_lang_System.h"
0N/A
0N/A#define OBJ "Ljava/lang/Object;"
0N/A
0N/A/* Only register the performance-critical methods */
0N/Astatic JNINativeMethod methods[] = {
0N/A {"currentTimeMillis", "()J", (void *)&JVM_CurrentTimeMillis},
0N/A {"nanoTime", "()J", (void *)&JVM_NanoTime},
0N/A {"arraycopy", "(" OBJ "I" OBJ "II)V", (void *)&JVM_ArrayCopy},
0N/A};
0N/A
0N/A#undef OBJ
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_lang_System_registerNatives(JNIEnv *env, jclass cls)
0N/A{
0N/A (*env)->RegisterNatives(env, cls,
0N/A methods, sizeof(methods)/sizeof(methods[0]));
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x)
0N/A{
0N/A return JVM_IHashCode(env, x);
0N/A}
0N/A
0N/A#define PUTPROP(props, key, val) \
0N/A if (1) { \
0N/A jstring jkey = (*env)->NewStringUTF(env, key); \
0N/A jstring jval = (*env)->NewStringUTF(env, val); \
0N/A jobject r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \
0N/A if ((*env)->ExceptionOccurred(env)) return NULL; \
0N/A (*env)->DeleteLocalRef(env, jkey); \
0N/A (*env)->DeleteLocalRef(env, jval); \
0N/A (*env)->DeleteLocalRef(env, r); \
0N/A } else ((void) 0)
0N/A
2247N/A/* "key" is a char type string with only ASCII character in it.
2247N/A "val" is a nchar (typedefed in java_props.h) type string */
2247N/A
2247N/A#define PUTPROP_ForPlatformNString(props, key, val) \
0N/A if (1) { \
2247N/A jstring jkey = (*env)->NewStringUTF(env, key); \
2247N/A jstring jval = GetStringPlatform(env, val); \
0N/A jobject r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \
0N/A if ((*env)->ExceptionOccurred(env)) return NULL; \
0N/A (*env)->DeleteLocalRef(env, jkey); \
0N/A (*env)->DeleteLocalRef(env, jval); \
0N/A (*env)->DeleteLocalRef(env, r); \
0N/A } else ((void) 0)
2700N/A#define REMOVEPROP(props, key) \
2700N/A if (1) { \
2700N/A jstring jkey = JNU_NewStringPlatform(env, key); \
2700N/A jobject r = (*env)->CallObjectMethod(env, props, removeID, jkey); \
2700N/A if ((*env)->ExceptionOccurred(env)) return NULL; \
2700N/A (*env)->DeleteLocalRef(env, jkey); \
2700N/A (*env)->DeleteLocalRef(env, r); \
2700N/A } else ((void) 0)
2700N/A#define GETPROP(props, key, jret) \
2700N/A if (1) { \
2700N/A jstring jkey = JNU_NewStringPlatform(env, key); \
2700N/A jret = (*env)->CallObjectMethod(env, props, getPropID, jkey); \
2700N/A if ((*env)->ExceptionOccurred(env)) return NULL; \
2700N/A (*env)->DeleteLocalRef(env, jkey); \
2700N/A } else ((void) 0)
0N/A
0N/A#ifndef VENDOR /* Third party may overwrite this. */
2807N/A#define VENDOR "Oracle Corporation"
2807N/A#define VENDOR_URL "http://java.oracle.com/"
3910N/A#define VENDOR_URL_BUG "http://bugreport.sun.com/bugreport/"
0N/A#endif
0N/A
355N/A#define JAVA_MAX_SUPPORTED_VERSION 51
0N/A#define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
0N/A
2807N/A#ifdef JAVA_SPECIFICATION_VENDOR /* Third party may NOT overwrite this. */
2807N/A #error "ERROR: No override of JAVA_SPECIFICATION_VENDOR is allowed"
2807N/A#else
2807N/A #define JAVA_SPECIFICATION_VENDOR "Oracle Corporation"
2884N/A#endif
2814N/A
2700N/Astatic int fmtdefault; // boolean value
2700N/Ajobject fillI18nProps(JNIEnv *env, jobject props, char *baseKey,
2700N/A char *platformDispVal, char *platformFmtVal,
2700N/A jmethodID putID, jmethodID getPropID) {
2700N/A jstring jVMBaseVal = NULL;
2700N/A
2700N/A GETPROP(props, baseKey, jVMBaseVal);
2700N/A if (jVMBaseVal) {
2700N/A // user specified the base property. there's nothing to do here.
2700N/A (*env)->DeleteLocalRef(env, jVMBaseVal);
2700N/A } else {
2700N/A char buf[64];
2700N/A jstring jVMVal = NULL;
2700N/A const char *baseVal = "";
2700N/A
2700N/A /* user.xxx base property */
2700N/A if (fmtdefault) {
2700N/A if (platformFmtVal) {
2700N/A PUTPROP(props, baseKey, platformFmtVal);
2700N/A baseVal = platformFmtVal;
2700N/A }
2700N/A } else {
2700N/A if (platformDispVal) {
2700N/A PUTPROP(props, baseKey, platformDispVal);
2700N/A baseVal = platformDispVal;
2700N/A }
2700N/A }
2700N/A
2700N/A /* user.xxx.display property */
2700N/A jio_snprintf(buf, sizeof(buf), "%s.display", baseKey);
2700N/A GETPROP(props, buf, jVMVal);
2700N/A if (jVMVal == NULL) {
2700N/A if (platformDispVal && (strcmp(baseVal, platformDispVal) != 0)) {
2700N/A PUTPROP(props, buf, platformDispVal);
2700N/A }
2700N/A } else {
2700N/A (*env)->DeleteLocalRef(env, jVMVal);
2700N/A }
2700N/A
2700N/A /* user.xxx.format property */
2700N/A jio_snprintf(buf, sizeof(buf), "%s.format", baseKey);
2700N/A GETPROP(props, buf, jVMVal);
2700N/A if (jVMVal == NULL) {
2700N/A if (platformFmtVal && (strcmp(baseVal, platformFmtVal) != 0)) {
2700N/A PUTPROP(props, buf, platformFmtVal);
2700N/A }
2700N/A } else {
2700N/A (*env)->DeleteLocalRef(env, jVMVal);
2700N/A }
2700N/A }
2700N/A
2700N/A return NULL;
2700N/A}
2700N/A
0N/AJNIEXPORT jobject JNICALL
0N/AJava_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
0N/A{
0N/A char buf[128];
0N/A java_props_t *sprops = GetJavaProperties(env);
0N/A jmethodID putID = (*env)->GetMethodID(env,
0N/A (*env)->GetObjectClass(env, props),
0N/A "put",
0N/A "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
2700N/A jmethodID removeID = (*env)->GetMethodID(env,
2700N/A (*env)->GetObjectClass(env, props),
2700N/A "remove",
2700N/A "(Ljava/lang/Object;)Ljava/lang/Object;");
2700N/A jmethodID getPropID = (*env)->GetMethodID(env,
2700N/A (*env)->GetObjectClass(env, props),
2700N/A "getProperty",
2700N/A "(Ljava/lang/String;)Ljava/lang/String;");
2700N/A jobject ret = NULL;
2700N/A jstring jVMVal = NULL;
0N/A
0N/A if (sprops == NULL || putID == NULL ) return NULL;
0N/A
0N/A PUTPROP(props, "java.specification.version",
0N/A JDK_MAJOR_VERSION "." JDK_MINOR_VERSION);
0N/A PUTPROP(props, "java.specification.name",
0N/A "Java Platform API Specification");
2807N/A PUTPROP(props, "java.specification.vendor",
2807N/A JAVA_SPECIFICATION_VENDOR);
0N/A
0N/A PUTPROP(props, "java.version", RELEASE);
0N/A PUTPROP(props, "java.vendor", VENDOR);
0N/A PUTPROP(props, "java.vendor.url", VENDOR_URL);
0N/A PUTPROP(props, "java.vendor.url.bug", VENDOR_URL_BUG);
0N/A
0N/A jio_snprintf(buf, sizeof(buf), "%d.%d", JAVA_MAX_SUPPORTED_VERSION,
0N/A JAVA_MAX_SUPPORTED_MINOR_VERSION);
0N/A PUTPROP(props, "java.class.version", buf);
0N/A
0N/A if (sprops->awt_toolkit) {
0N/A PUTPROP(props, "awt.toolkit", sprops->awt_toolkit);
0N/A }
0N/A
0N/A /* os properties */
0N/A PUTPROP(props, "os.name", sprops->os_name);
0N/A PUTPROP(props, "os.version", sprops->os_version);
0N/A PUTPROP(props, "os.arch", sprops->os_arch);
0N/A
0N/A /* file system properties */
0N/A PUTPROP(props, "file.separator", sprops->file_separator);
0N/A PUTPROP(props, "path.separator", sprops->path_separator);
0N/A PUTPROP(props, "line.separator", sprops->line_separator);
0N/A
0N/A /*
0N/A * user.language
2976N/A * user.script, user.country, user.variant (if user's environment specifies them)
0N/A * file.encoding
0N/A * file.encoding.pkg
0N/A */
0N/A PUTPROP(props, "user.language", sprops->language);
2976N/A if (sprops->script) {
2976N/A PUTPROP(props, "user.script", sprops->script);
2976N/A }
0N/A if (sprops->country) {
0N/A PUTPROP(props, "user.country", sprops->country);
0N/A }
0N/A if (sprops->variant) {
0N/A PUTPROP(props, "user.variant", sprops->variant);
0N/A }
0N/A PUTPROP(props, "file.encoding", sprops->encoding);
0N/A PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding);
0N/A PUTPROP(props, "file.encoding.pkg", "sun.io");
0N/A /* unicode_encoding specifies the default endianness */
0N/A PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding);
0N/A PUTPROP(props, "sun.cpu.isalist",
0N/A (sprops->cpu_isalist ? sprops->cpu_isalist : ""));
0N/A PUTPROP(props, "sun.cpu.endian", sprops->cpu_endian);
0N/A
4632N/A
4632N/A#ifdef MACOSX
4632N/A /* Proxy setting properties */
4632N/A if (sprops->httpProxyEnabled) {
4632N/A PUTPROP(props, "http.proxyHost", sprops->httpHost);
4632N/A PUTPROP(props, "http.proxyPort", sprops->httpPort);
4632N/A }
4632N/A
4632N/A if (sprops->httpsProxyEnabled) {
4632N/A PUTPROP(props, "https.proxyHost", sprops->httpsHost);
4632N/A PUTPROP(props, "https.proxyPort", sprops->httpsPort);
4632N/A }
4632N/A
4632N/A if (sprops->ftpProxyEnabled) {
4632N/A PUTPROP(props, "ftp.proxyHost", sprops->ftpHost);
4632N/A PUTPROP(props, "ftp.proxyPort", sprops->ftpPort);
4632N/A }
4632N/A
4632N/A if (sprops->socksProxyEnabled) {
4632N/A PUTPROP(props, "socksProxyHost", sprops->socksHost);
4632N/A PUTPROP(props, "socksProxyPort", sprops->socksPort);
4632N/A }
4632N/A
4632N/A if (sprops->gopherProxyEnabled) {
4632N/A // The gopher client is different in that it expects an 'is this set?' flag that the others don't.
4632N/A PUTPROP(props, "gopherProxySet", "true");
4632N/A PUTPROP(props, "gopherProxyHost", sprops->gopherHost);
4632N/A PUTPROP(props, "gopherProxyPort", sprops->gopherPort);
4632N/A } else {
4632N/A PUTPROP(props, "gopherProxySet", "false");
4632N/A }
4632N/A
4632N/A // Mac OS X only has a single proxy exception list which applies
4632N/A // to all protocols
4632N/A if (sprops->exceptionList) {
4632N/A PUTPROP(props, "http.nonProxyHosts", sprops->exceptionList);
4632N/A // HTTPS: implementation in jsse.jar uses http.nonProxyHosts
4632N/A PUTPROP(props, "ftp.nonProxyHosts", sprops->exceptionList);
4632N/A PUTPROP(props, "socksNonProxyHosts", sprops->exceptionList);
4632N/A }
4632N/A#endif
4632N/A
2247N/A /* !!! DO NOT call PUTPROP_ForPlatformNString before this line !!!
0N/A * !!! I18n properties have not been set up yet !!!
0N/A */
0N/A
0N/A /* Printing properties */
0N/A /* Note: java.awt.printerjob is an implementation private property which
0N/A * just happens to have a java.* name because it is referenced in
2807N/A * a java.awt class. It is the mechanism by which the implementation
0N/A * finds the appropriate class in the JRE for the platform.
0N/A * It is explicitly not designed to be overridden by clients as
0N/A * a way of replacing the implementation class, and in any case
0N/A * the mechanism by which the class is loaded is constrained to only
0N/A * find and load classes that are part of the JRE.
0N/A * This property may be removed if that mechanism is redesigned
0N/A */
0N/A PUTPROP(props, "java.awt.printerjob", sprops->printerJob);
0N/A
0N/A /* data model */
0N/A if (sizeof(sprops) == 4) {
0N/A sprops->data_model = "32";
0N/A } else if (sizeof(sprops) == 8) {
0N/A sprops->data_model = "64";
0N/A } else {
0N/A sprops->data_model = "unknown";
0N/A }
0N/A PUTPROP(props, "sun.arch.data.model", \
0N/A sprops->data_model);
0N/A
0N/A /* patch level */
0N/A PUTPROP(props, "sun.os.patch.level", \
0N/A sprops->patch_level);
0N/A
0N/A /* Java2D properties */
0N/A /* Note: java.awt.graphicsenv is an implementation private property which
0N/A * just happens to have a java.* name because it is referenced in
2807N/A * a java.awt class. It is the mechanism by which the implementation
0N/A * finds the appropriate class in the JRE for the platform.
0N/A * It is explicitly not designed to be overridden by clients as
0N/A * a way of replacing the implementation class, and in any case
0N/A * the mechanism by which the class is loaded is constrained to only
0N/A * find and load classes that are part of the JRE.
0N/A * This property may be removed if that mechanism is redesigned
0N/A */
0N/A PUTPROP(props, "java.awt.graphicsenv", sprops->graphics_env);
0N/A if (sprops->font_dir != NULL) {
2247N/A PUTPROP_ForPlatformNString(props,
0N/A "sun.java2d.fontpath", sprops->font_dir);
0N/A }
0N/A
2247N/A PUTPROP_ForPlatformNString(props, "java.io.tmpdir", sprops->tmp_dir);
0N/A
2247N/A PUTPROP_ForPlatformNString(props, "user.name", sprops->user_name);
2247N/A PUTPROP_ForPlatformNString(props, "user.home", sprops->user_home);
0N/A
0N/A PUTPROP(props, "user.timezone", sprops->timezone);
0N/A
2247N/A PUTPROP_ForPlatformNString(props, "user.dir", sprops->user_dir);
0N/A
0N/A /* This is a sun. property as it is currently only set for Gnome and
0N/A * Windows desktops.
0N/A */
0N/A if (sprops->desktop != NULL) {
0N/A PUTPROP(props, "sun.desktop", sprops->desktop);
0N/A }
0N/A
2700N/A /*
2976N/A * unset "user.language", "user.script", "user.country", and "user.variant"
2700N/A * in order to tell whether the command line option "-DXXXX=YYYY" is
2700N/A * specified or not. They will be reset in fillI18nProps() below.
2700N/A */
2700N/A REMOVEPROP(props, "user.language");
2976N/A REMOVEPROP(props, "user.script");
2700N/A REMOVEPROP(props, "user.country");
2700N/A REMOVEPROP(props, "user.variant");
2700N/A REMOVEPROP(props, "file.encoding");
2700N/A
2700N/A ret = JVM_InitProperties(env, props);
2700N/A
2700N/A /* Check the compatibility flag */
2700N/A GETPROP(props, "sun.locale.formatasdefault", jVMVal);
2700N/A if (jVMVal) {
2700N/A const char * val = (*env)->GetStringUTFChars(env, jVMVal, 0);
2700N/A fmtdefault = !strcmp(val, "true");
2700N/A (*env)->ReleaseStringUTFChars(env, jVMVal, val);
2700N/A (*env)->DeleteLocalRef(env, jVMVal);
2700N/A }
2700N/A
2700N/A /* reconstruct i18n related properties */
2700N/A fillI18nProps(env, props, "user.language", sprops->display_language,
2700N/A sprops->format_language, putID, getPropID);
2976N/A fillI18nProps(env, props, "user.script",
2976N/A sprops->display_script, sprops->format_script, putID, getPropID);
2700N/A fillI18nProps(env, props, "user.country",
2700N/A sprops->display_country, sprops->format_country, putID, getPropID);
2700N/A fillI18nProps(env, props, "user.variant",
2700N/A sprops->display_variant, sprops->format_variant, putID, getPropID);
2700N/A GETPROP(props, "file.encoding", jVMVal);
2700N/A if (jVMVal == NULL) {
5859N/A#ifdef MACOSX
5859N/A /*
5859N/A * Since sun_jnu_encoding is now hard-coded to UTF-8 on Mac, we don't
5859N/A * want to use it to overwrite file.encoding
5859N/A */
5859N/A PUTPROP(props, "file.encoding", sprops->encoding);
5859N/A#else
2700N/A if (fmtdefault) {
2700N/A PUTPROP(props, "file.encoding", sprops->encoding);
2700N/A } else {
2700N/A PUTPROP(props, "file.encoding", sprops->sun_jnu_encoding);
2700N/A }
5859N/A#endif
2700N/A } else {
2700N/A (*env)->DeleteLocalRef(env, jVMVal);
2700N/A }
2700N/A
2700N/A return ret;
0N/A}
0N/A
0N/A/*
0N/A * The following three functions implement setter methods for
0N/A * java.lang.System.{in, out, err}. They are natively implemented
0N/A * because they violate the semantics of the language (i.e. set final
0N/A * variable).
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)
0N/A{
0N/A jfieldID fid =
0N/A (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");
0N/A if (fid == 0)
0N/A return;
0N/A (*env)->SetStaticObjectField(env,cla,fid,stream);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
0N/A{
0N/A jfieldID fid =
0N/A (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
0N/A if (fid == 0)
0N/A return;
0N/A (*env)->SetStaticObjectField(env,cla,fid,stream);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)
0N/A{
0N/A jfieldID fid =
0N/A (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");
0N/A if (fid == 0)
0N/A return;
0N/A (*env)->SetStaticObjectField(env,cla,fid,stream);
0N/A}
0N/A
0N/Astatic void cpchars(jchar *dst, char *src, int n)
0N/A{
0N/A int i;
0N/A for (i = 0; i < n; i++) {
0N/A dst[i] = src[i];
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jstring JNICALL
0N/AJava_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)
0N/A{
0N/A int len;
0N/A int prefix_len = (int) strlen(JNI_LIB_PREFIX);
0N/A int suffix_len = (int) strlen(JNI_LIB_SUFFIX);
0N/A
0N/A jchar chars[256];
0N/A if (libname == NULL) {
0N/A JNU_ThrowNullPointerException(env, 0);
0N/A return NULL;
0N/A }
0N/A len = (*env)->GetStringLength(env, libname);
0N/A if (len > 240) {
0N/A JNU_ThrowIllegalArgumentException(env, "name too long");
0N/A return NULL;
0N/A }
0N/A cpchars(chars, JNI_LIB_PREFIX, prefix_len);
0N/A (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);
0N/A len += prefix_len;
0N/A cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);
0N/A len += suffix_len;
0N/A
0N/A return (*env)->NewString(env, chars, len);
0N/A}