src/share/native/java/lang/System.c
author ohair
Fri Jan 30 16:27:33 2009 -0800 (3 years ago)
changeset 9 2d585507a41b
parent 239e8fe7a0af1
child 354ffa98eed5766
permissions -rw-r--r--
6755905: Changes for openjdk6 build 04
6628175: NetBeans/OpenJDK projects should support generated sources
6598089: JDK 7: AWT often goes into busy loop when showing dialog
6612531: api/javax_swing/ScrollPaneLayout/index.html#xxxLayoutSize[ScrollPaneLayout2024] throws NPE
6550267: Debugging some NetBeans/OpenJDK project's demo apps does not work
6610014: Allow partial open jdk builds without access to binary plugs
6643627: JMX source code includes incorrect Java code
6652042: NetBeans JConsole project should run tests against JAR built by project
6652053: Rename NetBeans project from j2se to jdk
6653678: NetBeans JDK projects README file should be release-agnostic
6654456: OpenJDK build problem with freetype makefiles
6654903: OpenJDK NetBeans projects should ony run appropriate tests
6658909: A few more SCCS keyword cleanups and whitespace processing
6616089: Whitespace cleanup on all sources
Summary: Final b04 state (as defined by the source bundle)
Reviewed-by: darcy
        1 /*
        2  * Copyright 1994-2005 Sun Microsystems, Inc.  All Rights Reserved.
        3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
        4  *
        5  * This code is free software; you can redistribute it and/or modify it
        6  * under the terms of the GNU General Public License version 2 only, as
        7  * published by the Free Software Foundation.  Sun designates this
        8  * particular file as subject to the "Classpath" exception as provided
        9  * by Sun in the LICENSE file that accompanied this code.
       10  *
       11  * This code is distributed in the hope that it will be useful, but WITHOUT
       12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       14  * version 2 for more details (a copy is included in the LICENSE file that
       15  * accompanied this code).
       16  *
       17  * You should have received a copy of the GNU General Public License version
       18  * 2 along with this work; if not, write to the Free Software Foundation,
       19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       20  *
       21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       22  * CA 95054 USA or visit www.sun.com if you need additional information or
       23  * have any questions.
       24  */
       25 
       26 #include <string.h>
       27 
       28 #include "jni.h"
       29 #include "jni_util.h"
       30 #include "jvm.h"
       31 #include "java_props.h"
       32 
       33 #include "java_lang_System.h"
       34 
       35 #define OBJ "Ljava/lang/Object;"
       36 
       37 /* Only register the performance-critical methods */
       38 static JNINativeMethod methods[] = {
       39     {"currentTimeMillis", "()J",              (void *)&JVM_CurrentTimeMillis},
       40     {"nanoTime",          "()J",              (void *)&JVM_NanoTime},
       41     {"arraycopy",     "(" OBJ "I" OBJ "II)V", (void *)&JVM_ArrayCopy},
       42 };
       43 
       44 #undef OBJ
       45 
       46 JNIEXPORT void JNICALL
       47 Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls)
       48 {
       49     (*env)->RegisterNatives(env, cls,
       50                             methods, sizeof(methods)/sizeof(methods[0]));
       51 }
       52 
       53 JNIEXPORT jint JNICALL
       54 Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x)
       55 {
       56     return JVM_IHashCode(env, x);
       57 }
       58 
       59 #define PUTPROP(props, key, val) \
       60     if (1) { \
       61         jstring jkey = (*env)->NewStringUTF(env, key); \
       62         jstring jval = (*env)->NewStringUTF(env, val); \
       63         jobject r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \
       64         if ((*env)->ExceptionOccurred(env)) return NULL; \
       65         (*env)->DeleteLocalRef(env, jkey); \
       66         (*env)->DeleteLocalRef(env, jval); \
       67         (*env)->DeleteLocalRef(env, r); \
       68     } else ((void) 0)
       69 
       70 #define PUTPROP_ForPlatformCString(props, key, val) \
       71     if (1) { \
       72         jstring jkey = JNU_NewStringPlatform(env, key); \
       73         jstring jval = JNU_NewStringPlatform(env, val); \
       74         jobject r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \
       75         if ((*env)->ExceptionOccurred(env)) return NULL; \
       76         (*env)->DeleteLocalRef(env, jkey); \
       77         (*env)->DeleteLocalRef(env, jval); \
       78         (*env)->DeleteLocalRef(env, r); \
       79     } else ((void) 0)
       80 
       81 #ifndef VENDOR /* Third party may overwrite this. */
       82 #define VENDOR "Sun Microsystems Inc."
       83 #define VENDOR_URL "http://java.sun.com/"
       84 #define VENDOR_URL_BUG "http://java.sun.com/cgi-bin/bugreport.cgi"
       85 #endif
       86 
       87 #define JAVA_MAX_SUPPORTED_VERSION 50
       88 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
       89 
       90 JNIEXPORT jobject JNICALL
       91 Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
       92 {
       93     char buf[128];
       94     java_props_t *sprops = GetJavaProperties(env);
       95     jmethodID putID = (*env)->GetMethodID(env,
       96                                           (*env)->GetObjectClass(env, props),
       97                                           "put",
       98             "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
       99 
      100     if (sprops == NULL || putID == NULL ) return NULL;
      101 
      102     PUTPROP(props, "java.specification.version",
      103             JDK_MAJOR_VERSION "." JDK_MINOR_VERSION);
      104     PUTPROP(props, "java.specification.name",
      105             "Java Platform API Specification");
      106     PUTPROP(props, "java.specification.vendor", "Sun Microsystems Inc.");
      107 
      108     PUTPROP(props, "java.version", RELEASE);
      109     PUTPROP(props, "java.vendor", VENDOR);
      110     PUTPROP(props, "java.vendor.url", VENDOR_URL);
      111     PUTPROP(props, "java.vendor.url.bug", VENDOR_URL_BUG);
      112 
      113     jio_snprintf(buf, sizeof(buf), "%d.%d", JAVA_MAX_SUPPORTED_VERSION,
      114                                             JAVA_MAX_SUPPORTED_MINOR_VERSION);
      115     PUTPROP(props, "java.class.version", buf);
      116 
      117     if (sprops->awt_toolkit) {
      118         PUTPROP(props, "awt.toolkit", sprops->awt_toolkit);
      119     }
      120 
      121     /* os properties */
      122     PUTPROP(props, "os.name", sprops->os_name);
      123     PUTPROP(props, "os.version", sprops->os_version);
      124     PUTPROP(props, "os.arch", sprops->os_arch);
      125 
      126     /* file system properties */
      127     PUTPROP(props, "file.separator", sprops->file_separator);
      128     PUTPROP(props, "path.separator", sprops->path_separator);
      129     PUTPROP(props, "line.separator", sprops->line_separator);
      130 
      131     /*
      132      *  user.language
      133      *  user.country, user.variant (if user's environment specifies them)
      134      *  file.encoding
      135      *  file.encoding.pkg
      136      */
      137     PUTPROP(props, "user.language", sprops->language);
      138     if (sprops->country) {
      139         PUTPROP(props, "user.country", sprops->country);
      140     }
      141     if (sprops->variant) {
      142         PUTPROP(props, "user.variant", sprops->variant);
      143     }
      144     PUTPROP(props, "file.encoding", sprops->encoding);
      145     PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding);
      146     PUTPROP(props, "file.encoding.pkg", "sun.io");
      147     /* unicode_encoding specifies the default endianness */
      148     PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding);
      149     PUTPROP(props, "sun.cpu.isalist",
      150             (sprops->cpu_isalist ? sprops->cpu_isalist : ""));
      151     PUTPROP(props, "sun.cpu.endian",  sprops->cpu_endian);
      152 
      153     /* !!! DO NOT call PUTPROP_ForPlatformCString before this line !!!
      154      * !!! I18n properties have not been set up yet !!!
      155      */
      156 
      157     /* Printing properties */
      158     /* Note: java.awt.printerjob is an implementation private property which
      159      * just happens to have a java.* name because it is referenced in
      160      * a java.awt class. It is the mechanism by which the Sun implementation
      161      * finds the appropriate class in the JRE for the platform.
      162      * It is explicitly not designed to be overridden by clients as
      163      * a way of replacing the implementation class, and in any case
      164      * the mechanism by which the class is loaded is constrained to only
      165      * find and load classes that are part of the JRE.
      166      * This property may be removed if that mechanism is redesigned
      167      */
      168     PUTPROP(props, "java.awt.printerjob", sprops->printerJob);
      169 
      170     /* data model */
      171     if (sizeof(sprops) == 4) {
      172         sprops->data_model = "32";
      173     } else if (sizeof(sprops) == 8) {
      174         sprops->data_model = "64";
      175     } else {
      176         sprops->data_model = "unknown";
      177     }
      178     PUTPROP(props, "sun.arch.data.model",  \
      179                     sprops->data_model);
      180 
      181     /* patch level */
      182     PUTPROP(props, "sun.os.patch.level",  \
      183                     sprops->patch_level);
      184 
      185     /* Java2D properties */
      186     /* Note: java.awt.graphicsenv is an implementation private property which
      187      * just happens to have a java.* name because it is referenced in
      188      * a java.awt class. It is the mechanism by which the Sun implementation
      189      * finds the appropriate class in the JRE for the platform.
      190      * It is explicitly not designed to be overridden by clients as
      191      * a way of replacing the implementation class, and in any case
      192      * the mechanism by which the class is loaded is constrained to only
      193      * find and load classes that are part of the JRE.
      194      * This property may be removed if that mechanism is redesigned
      195      */
      196     PUTPROP(props, "java.awt.graphicsenv", sprops->graphics_env);
      197     if (sprops->font_dir != NULL) {
      198         PUTPROP_ForPlatformCString(props,
      199                                    "sun.java2d.fontpath", sprops->font_dir);
      200     }
      201 
      202     PUTPROP_ForPlatformCString(props, "java.io.tmpdir", sprops->tmp_dir);
      203 
      204     PUTPROP_ForPlatformCString(props, "user.name", sprops->user_name);
      205     PUTPROP_ForPlatformCString(props, "user.home", sprops->user_home);
      206 
      207     PUTPROP(props, "user.timezone", sprops->timezone);
      208 
      209     PUTPROP_ForPlatformCString(props, "user.dir", sprops->user_dir);
      210 
      211     /* This is a sun. property as it is currently only set for Gnome and
      212      * Windows desktops.
      213      */
      214     if (sprops->desktop != NULL) {
      215         PUTPROP(props, "sun.desktop", sprops->desktop);
      216     }
      217 
      218     return JVM_InitProperties(env, props);
      219 }
      220 
      221 /*
      222  * The following three functions implement setter methods for
      223  * java.lang.System.{in, out, err}. They are natively implemented
      224  * because they violate the semantics of the language (i.e. set final
      225  * variable).
      226  */
      227 JNIEXPORT void JNICALL
      228 Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)
      229 {
      230     jfieldID fid =
      231         (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");
      232     if (fid == 0)
      233         return;
      234     (*env)->SetStaticObjectField(env,cla,fid,stream);
      235 }
      236 
      237 JNIEXPORT void JNICALL
      238 Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
      239 {
      240     jfieldID fid =
      241         (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
      242     if (fid == 0)
      243         return;
      244     (*env)->SetStaticObjectField(env,cla,fid,stream);
      245 }
      246 
      247 JNIEXPORT void JNICALL
      248 Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)
      249 {
      250     jfieldID fid =
      251         (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");
      252     if (fid == 0)
      253         return;
      254     (*env)->SetStaticObjectField(env,cla,fid,stream);
      255 }
      256 
      257 static void cpchars(jchar *dst, char *src, int n)
      258 {
      259     int i;
      260     for (i = 0; i < n; i++) {
      261         dst[i] = src[i];
      262     }
      263 }
      264 
      265 JNIEXPORT jstring JNICALL
      266 Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)
      267 {
      268     int len;
      269     int prefix_len = (int) strlen(JNI_LIB_PREFIX);
      270     int suffix_len = (int) strlen(JNI_LIB_SUFFIX);
      271 
      272     jchar chars[256];
      273     if (libname == NULL) {
      274         JNU_ThrowNullPointerException(env, 0);
      275         return NULL;
      276     }
      277     len = (*env)->GetStringLength(env, libname);
      278     if (len > 240) {
      279         JNU_ThrowIllegalArgumentException(env, "name too long");
      280         return NULL;
      281     }
      282     cpchars(chars, JNI_LIB_PREFIX, prefix_len);
      283     (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);
      284     len += prefix_len;
      285     cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);
      286     len += suffix_len;
      287 
      288     return (*env)->NewString(env, chars, len);
      289 }