J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-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 <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <jni.h> |
| 29 | #include "management.h" |
| 30 | #include "sun_management_Flag.h" |
| 31 | |
| 32 | static jobject default_origin = NULL; |
| 33 | static jobject vm_creation_origin = NULL; |
| 34 | static jobject mgmt_origin = NULL; |
| 35 | static jobject envvar_origin = NULL; |
| 36 | static jobject config_file_origin = NULL; |
| 37 | static jobject ergo_origin = NULL; |
| 38 | static jobject other_origin = NULL; |
| 39 | |
| 40 | JNIEXPORT jint JNICALL |
| 41 | Java_sun_management_Flag_getInternalFlagCount |
| 42 | (JNIEnv *env, jclass cls) |
| 43 | { |
| 44 | jlong count = jmm_interface->GetLongAttribute(env, NULL, |
| 45 | JMM_VM_GLOBAL_COUNT); |
| 46 | return (jint) count; |
| 47 | } |
| 48 | |
| 49 | JNIEXPORT jobjectArray JNICALL |
| 50 | Java_sun_management_Flag_getAllFlagNames |
| 51 | (JNIEnv *env, jclass cls) |
| 52 | { |
| 53 | return jmm_interface->GetVMGlobalNames(env); |
| 54 | } |
| 55 | |
| 56 | static jobject find_origin_constant(JNIEnv* env, const char* enum_name) { |
| 57 | jvalue field; |
| 58 | field = JNU_GetStaticFieldByName(env, |
| 59 | NULL, |
| 60 | "com/sun/management/VMOption$Origin", |
| 61 | enum_name, |
| 62 | "Lcom/sun/management/VMOption$Origin;"); |
| 63 | return (*env)->NewGlobalRef(env, field.l); |
| 64 | } |
| 65 | |
| 66 | JNIEXPORT void JNICALL |
| 67 | Java_sun_management_Flag_initialize |
| 68 | (JNIEnv *env, jclass cls) |
| 69 | { |
| 70 | default_origin = find_origin_constant(env, "DEFAULT"); |
| 71 | vm_creation_origin = find_origin_constant(env, "VM_CREATION"); |
| 72 | mgmt_origin = find_origin_constant(env, "MANAGEMENT"); |
| 73 | envvar_origin = find_origin_constant(env, "ENVIRON_VAR"); |
| 74 | config_file_origin = find_origin_constant(env, "CONFIG_FILE"); |
| 75 | ergo_origin = find_origin_constant(env, "ERGONOMIC"); |
| 76 | other_origin = find_origin_constant(env, "OTHER"); |
| 77 | } |
| 78 | |
| 79 | JNIEXPORT jint JNICALL |
| 80 | Java_sun_management_Flag_getFlags |
| 81 | (JNIEnv *env, jclass cls, jobjectArray names, jobjectArray flags, jint count) |
| 82 | { |
| 83 | char errmsg[128]; |
| 84 | |
| 85 | jint num_flags, i, index; |
| 86 | jmmVMGlobal* globals; |
| 87 | const char* class_name = "sun/management/Flag"; |
| 88 | const char* signature = "(Ljava/lang/String;Ljava/lang/Object;ZZLcom/sun/management/VMOption$Origin;)V"; |
| 89 | jobject origin; |
| 90 | jobject valueObj; |
| 91 | jobject flag; |
| 92 | |
| 93 | if (flags == NULL) { |
| 94 | JNU_ThrowNullPointerException(env, 0); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | if (count == 0) { |
| 99 | JNU_ThrowIllegalArgumentException(env, 0); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | globals = (jmmVMGlobal*) malloc(count * sizeof(jmmVMGlobal)); |
| 104 | if (globals == NULL) { |
| 105 | JNU_ThrowOutOfMemoryError(env, 0); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | num_flags = jmm_interface->GetVMGlobals(env, names, globals, count); |
| 110 | if (num_flags == 0) { |
| 111 | free(globals); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | index = 0; |
| 116 | for (i = 0; i < count; i++) { |
| 117 | if (globals[i].name == NULL) { |
| 118 | continue; |
| 119 | } |
| 120 | switch (globals[i].type) { |
| 121 | case JMM_VMGLOBAL_TYPE_JBOOLEAN: |
| 122 | valueObj = JNU_NewObjectByName(env, "java/lang/Boolean", "(Z)V", |
| 123 | globals[i].value.z); |
| 124 | break; |
| 125 | case JMM_VMGLOBAL_TYPE_JSTRING: |
| 126 | valueObj = globals[i].value.l; |
| 127 | break; |
| 128 | case JMM_VMGLOBAL_TYPE_JLONG: |
| 129 | valueObj = JNU_NewObjectByName(env, "java/lang/Long", "(J)V", |
| 130 | globals[i].value.j); |
| 131 | break; |
| 132 | default: |
| 133 | // unsupported type |
| 134 | sprintf(errmsg, "Unsupported VMGlobal Type %d", globals[i].type); |
| 135 | JNU_ThrowInternalError(env, errmsg); |
| 136 | free(globals); |
| 137 | return 0; |
| 138 | } |
| 139 | switch (globals[i].origin) { |
| 140 | case JMM_VMGLOBAL_ORIGIN_DEFAULT: |
| 141 | origin = default_origin; |
| 142 | break; |
| 143 | case JMM_VMGLOBAL_ORIGIN_COMMAND_LINE: |
| 144 | origin = vm_creation_origin; |
| 145 | break; |
| 146 | case JMM_VMGLOBAL_ORIGIN_MANAGEMENT: |
| 147 | origin = mgmt_origin; |
| 148 | break; |
| 149 | case JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR: |
| 150 | origin = envvar_origin; |
| 151 | break; |
| 152 | case JMM_VMGLOBAL_ORIGIN_CONFIG_FILE: |
| 153 | origin = config_file_origin; |
| 154 | break; |
| 155 | case JMM_VMGLOBAL_ORIGIN_ERGONOMIC: |
| 156 | origin = ergo_origin; |
| 157 | break; |
| 158 | case JMM_VMGLOBAL_ORIGIN_OTHER: |
| 159 | origin = other_origin; |
| 160 | break; |
| 161 | default: |
| 162 | // unknown origin |
| 163 | origin = other_origin; |
| 164 | break; |
| 165 | } |
| 166 | flag = JNU_NewObjectByName(env, class_name, signature, globals[i].name, |
| 167 | valueObj, globals[i].writeable, |
| 168 | globals[i].external, origin); |
| 169 | if (flag == NULL) { |
| 170 | free(globals); |
| 171 | JNU_ThrowOutOfMemoryError(env, 0); |
| 172 | return 0; |
| 173 | } |
| 174 | (*env)->SetObjectArrayElement(env, flags, index, flag); |
| 175 | index++; |
| 176 | } |
| 177 | |
| 178 | if (index != num_flags) { |
| 179 | JNU_ThrowInternalError(env, "Number of Flag objects created unmatched"); |
| 180 | free(globals); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | free(globals); |
| 185 | |
| 186 | /* return the number of Flag objects created */ |
| 187 | return num_flags; |
| 188 | } |
| 189 | |
| 190 | JNIEXPORT void JNICALL |
| 191 | Java_sun_management_Flag_setLongValue |
| 192 | (JNIEnv *env, jclass cls, jstring name, jlong value) |
| 193 | { |
| 194 | jvalue v; |
| 195 | v.j = value; |
| 196 | |
| 197 | jmm_interface->SetVMGlobal(env, name, v); |
| 198 | } |
| 199 | |
| 200 | JNIEXPORT void JNICALL |
| 201 | Java_sun_management_Flag_setBooleanValue |
| 202 | (JNIEnv *env, jclass cls, jstring name, jboolean value) |
| 203 | { |
| 204 | jvalue v; |
| 205 | v.z = value; |
| 206 | |
| 207 | jmm_interface->SetVMGlobal(env, name, v); |
| 208 | } |
| 209 | |
| 210 | JNIEXPORT void JNICALL |
| 211 | Java_sun_management_Flag_setStringValue |
| 212 | (JNIEnv *env, jclass cls, jstring name, jstring value) |
| 213 | { |
| 214 | jvalue v; |
| 215 | v.l = value; |
| 216 | |
| 217 | jmm_interface->SetVMGlobal(env, name, v); |
| 218 | } |