2 * Copyright 1994-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
31 #include "java_props.h"
33 #include "java_lang_System.h"
35 #define OBJ "Ljava/lang/Object;"
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},
46 JNIEXPORT void JNICALL
47 Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls)
49 (*env)->RegisterNatives(env, cls,
50 methods, sizeof(methods)/sizeof(methods[0]));
53 JNIEXPORT jint JNICALL
54 Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x)
56 return JVM_IHashCode(env, x);
59 #define PUTPROP(props, key, val) \
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); \
70 #define PUTPROP_ForPlatformCString(props, key, val) \
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); \
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"
87 #define JAVA_MAX_SUPPORTED_VERSION 50
88 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
90 JNIEXPORT jobject JNICALL
91 Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
94 java_props_t *sprops = GetJavaProperties(env);
95 jmethodID putID = (*env)->GetMethodID(env,
96 (*env)->GetObjectClass(env, props),
98 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
100 if (sprops == NULL || putID == NULL ) return NULL;
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.");
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);
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);
117 if (sprops->awt_toolkit) {
118 PUTPROP(props, "awt.toolkit", sprops->awt_toolkit);
122 PUTPROP(props, "os.name", sprops->os_name);
123 PUTPROP(props, "os.version", sprops->os_version);
124 PUTPROP(props, "os.arch", sprops->os_arch);
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);
133 * user.country, user.variant (if user's environment specifies them)
137 PUTPROP(props, "user.language", sprops->language);
138 if (sprops->country) {
139 PUTPROP(props, "user.country", sprops->country);
141 if (sprops->variant) {
142 PUTPROP(props, "user.variant", sprops->variant);
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);
153 /* !!! DO NOT call PUTPROP_ForPlatformCString before this line !!!
154 * !!! I18n properties have not been set up yet !!!
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
168 PUTPROP(props, "java.awt.printerjob", sprops->printerJob);
171 if (sizeof(sprops) == 4) {
172 sprops->data_model = "32";
173 } else if (sizeof(sprops) == 8) {
174 sprops->data_model = "64";
176 sprops->data_model = "unknown";
178 PUTPROP(props, "sun.arch.data.model", \
182 PUTPROP(props, "sun.os.patch.level", \
183 sprops->patch_level);
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
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);
202 PUTPROP_ForPlatformCString(props, "java.io.tmpdir", sprops->tmp_dir);
204 PUTPROP_ForPlatformCString(props, "user.name", sprops->user_name);
205 PUTPROP_ForPlatformCString(props, "user.home", sprops->user_home);
207 PUTPROP(props, "user.timezone", sprops->timezone);
209 PUTPROP_ForPlatformCString(props, "user.dir", sprops->user_dir);
211 /* This is a sun. property as it is currently only set for Gnome and
214 if (sprops->desktop != NULL) {
215 PUTPROP(props, "sun.desktop", sprops->desktop);
218 return JVM_InitProperties(env, props);
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
227 JNIEXPORT void JNICALL
228 Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)
231 (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");
234 (*env)->SetStaticObjectField(env,cla,fid,stream);
237 JNIEXPORT void JNICALL
238 Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
241 (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
244 (*env)->SetStaticObjectField(env,cla,fid,stream);
247 JNIEXPORT void JNICALL
248 Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)
251 (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");
254 (*env)->SetStaticObjectField(env,cla,fid,stream);
257 static void cpchars(jchar *dst, char *src, int n)
260 for (i = 0; i < n; i++) {
265 JNIEXPORT jstring JNICALL
266 Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)
269 int prefix_len = (int) strlen(JNI_LIB_PREFIX);
270 int suffix_len = (int) strlen(JNI_LIB_SUFFIX);
273 if (libname == NULL) {
274 JNU_ThrowNullPointerException(env, 0);
277 len = (*env)->GetStringLength(env, libname);
279 JNU_ThrowIllegalArgumentException(env, "name too long");
282 cpchars(chars, JNI_LIB_PREFIX, prefix_len);
283 (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);
285 cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);
288 return (*env)->NewString(env, chars, len);