1 /*
   2  * Copyright (c) 2003, 2005, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <stdio.h>
  28 #include <string.h>
  29 #include <jni.h>
  30 #include "management.h"
  31 #include "sun_management_Flag.h"
  32 
  33 static jobject default_origin = NULL;
  34 static jobject vm_creation_origin = NULL;
  35 static jobject mgmt_origin = NULL;
  36 static jobject envvar_origin = NULL;
  37 static jobject config_file_origin = NULL;
  38 static jobject ergo_origin = NULL;
  39 static jobject other_origin = NULL;
  40 
  41 JNIEXPORT jint JNICALL
  42 Java_sun_management_Flag_getInternalFlagCount
  43   (JNIEnv *env, jclass cls)
  44 {
  45     jlong count = jmm_interface->GetLongAttribute(env, NULL,
  46                                                   JMM_VM_GLOBAL_COUNT);
  47     return (jint) count;
  48 }
  49 
  50 JNIEXPORT jobjectArray JNICALL
  51   Java_sun_management_Flag_getAllFlagNames
  52 (JNIEnv *env, jclass cls)
  53 {
  54   return jmm_interface->GetVMGlobalNames(env);
  55 }
  56 
  57 static jobject find_origin_constant(JNIEnv* env, const char* enum_name) {
  58     jvalue field;
  59     field = JNU_GetStaticFieldByName(env,
  60                                      NULL,
  61                                      "com/sun/management/VMOption$Origin",
  62                                      enum_name,
  63                                      "Lcom/sun/management/VMOption$Origin;");
  64     return (*env)->NewGlobalRef(env, field.l);
  65 }
  66 
  67 JNIEXPORT void JNICALL
  68 Java_sun_management_Flag_initialize
  69   (JNIEnv *env, jclass cls)
  70 {
  71     default_origin = find_origin_constant(env, "DEFAULT");
  72     vm_creation_origin = find_origin_constant(env, "VM_CREATION");
  73     mgmt_origin = find_origin_constant(env, "MANAGEMENT");
  74     envvar_origin = find_origin_constant(env, "ENVIRON_VAR");
  75     config_file_origin = find_origin_constant(env, "CONFIG_FILE");
  76     ergo_origin = find_origin_constant(env, "ERGONOMIC");
  77     other_origin = find_origin_constant(env, "OTHER");
  78 }
  79 
  80 JNIEXPORT jint JNICALL
  81 Java_sun_management_Flag_getFlags
  82   (JNIEnv *env, jclass cls, jobjectArray names, jobjectArray flags, jint count)
  83 {
  84     jint num_flags, i, index;
  85     jmmVMGlobal* globals;
  86     size_t gsize;
  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     gsize = count * sizeof(jmmVMGlobal);
 104     globals = (jmmVMGlobal*) malloc(gsize);
 105     if (globals == NULL) {
 106         JNU_ThrowOutOfMemoryError(env, 0);
 107         return 0;
 108     }
 109 
 110     memset(globals, 0, gsize);
 111     num_flags = jmm_interface->GetVMGlobals(env, names, globals, count);
 112     if (num_flags == 0) {
 113         free(globals);
 114         return 0;
 115     }
 116 
 117     index = 0;
 118     for (i = 0; i < count; i++) {
 119         if (globals[i].name == NULL) {
 120             continue;
 121         }
 122         switch (globals[i].type) {
 123         case JMM_VMGLOBAL_TYPE_JBOOLEAN:
 124             valueObj = JNU_NewObjectByName(env, "java/lang/Boolean", "(Z)V",
 125                                            globals[i].value.z);
 126             break;
 127         case JMM_VMGLOBAL_TYPE_JSTRING:
 128             valueObj = globals[i].value.l;
 129             break;
 130         case JMM_VMGLOBAL_TYPE_JLONG:
 131             valueObj = JNU_NewObjectByName(env, "java/lang/Long", "(J)V",
 132                                            globals[i].value.j);
 133             break;
 134         default:
 135             // ignore unsupported type
 136             continue;
 137         }
 138         switch (globals[i].origin) {
 139         case JMM_VMGLOBAL_ORIGIN_DEFAULT:
 140             origin = default_origin;
 141             break;
 142         case JMM_VMGLOBAL_ORIGIN_COMMAND_LINE:
 143             origin = vm_creation_origin;
 144             break;
 145         case JMM_VMGLOBAL_ORIGIN_MANAGEMENT:
 146             origin = mgmt_origin;
 147             break;
 148         case JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR:
 149             origin = envvar_origin;
 150             break;
 151         case JMM_VMGLOBAL_ORIGIN_CONFIG_FILE:
 152             origin = config_file_origin;
 153             break;
 154         case JMM_VMGLOBAL_ORIGIN_ERGONOMIC:
 155             origin = ergo_origin;
 156             break;
 157         case JMM_VMGLOBAL_ORIGIN_OTHER:
 158             origin = other_origin;
 159             break;
 160         default:
 161             // unknown origin
 162             origin = other_origin;
 163             break;
 164         }
 165         flag = JNU_NewObjectByName(env, class_name, signature, globals[i].name,
 166                                    valueObj, globals[i].writeable,
 167                                    globals[i].external, origin);
 168         if (flag == NULL) {
 169             free(globals);
 170             JNU_ThrowOutOfMemoryError(env, 0);
 171             return 0;
 172         }
 173         (*env)->SetObjectArrayElement(env, flags, index, flag);
 174         index++;
 175     }
 176 
 177     if (index != num_flags) {
 178         JNU_ThrowInternalError(env, "Number of Flag objects created unmatched");
 179         free(globals);
 180         return 0;
 181     }
 182 
 183     free(globals);
 184 
 185     /* return the number of Flag objects created */
 186     return num_flags;
 187 }
 188 
 189 JNIEXPORT void JNICALL
 190 Java_sun_management_Flag_setLongValue
 191   (JNIEnv *env, jclass cls, jstring name, jlong value)
 192 {
 193    jvalue v;
 194    v.j = value;
 195 
 196    jmm_interface->SetVMGlobal(env, name, v);
 197 }
 198 
 199 JNIEXPORT void JNICALL
 200 Java_sun_management_Flag_setBooleanValue
 201   (JNIEnv *env, jclass cls, jstring name, jboolean value)
 202 {
 203    jvalue v;
 204    v.z = value;
 205 
 206    jmm_interface->SetVMGlobal(env, name, v);
 207 }
 208 
 209 JNIEXPORT void JNICALL
 210 Java_sun_management_Flag_setStringValue
 211   (JNIEnv *env, jclass cls, jstring name, jstring value)
 212 {
 213    jvalue v;
 214    v.l = value;
 215 
 216    jmm_interface->SetVMGlobal(env, name, v);
 217 }
--- EOF ---















































































   1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <stdio.h>
  28 #include <string.h>
  29 #include <jni.h>
  30 #include "management.h"
  31 #include "sun_management_Flag.h"
  32 
  33 static jobject default_origin = NULL;
  34 static jobject vm_creation_origin = NULL;
  35 static jobject mgmt_origin = NULL;
  36 static jobject envvar_origin = NULL;
  37 static jobject config_file_origin = NULL;
  38 static jobject ergo_origin = NULL;
  39 static jobject other_origin = NULL;
  40 
  41 JNIEXPORT jint JNICALL
  42 Java_sun_management_Flag_getInternalFlagCount
  43   (JNIEnv *env, jclass cls)
  44 {
  45     jlong count = jmm_interface->GetLongAttribute(env, NULL,
  46                                                   JMM_VM_GLOBAL_COUNT);
  47     return (jint) count;
  48 }
  49 
  50 JNIEXPORT jobjectArray JNICALL
  51   Java_sun_management_Flag_getAllFlagNames
  52 (JNIEnv *env, jclass cls)
  53 {
  54   return jmm_interface->GetVMGlobalNames(env);
  55 }
  56 
  57 static jobject find_origin_constant(JNIEnv* env, const char* enum_name) {
  58     jvalue field;
  59     field = JNU_GetStaticFieldByName(env,
  60                                      NULL,
  61                                      "com/sun/management/VMOption$Origin",
  62                                      enum_name,
  63                                      "Lcom/sun/management/VMOption$Origin;");
  64     return (*env)->NewGlobalRef(env, field.l);
  65 }
  66 
  67 JNIEXPORT void JNICALL
  68 Java_sun_management_Flag_initialize
  69   (JNIEnv *env, jclass cls)
  70 {
  71     default_origin = find_origin_constant(env, "DEFAULT");
  72     vm_creation_origin = find_origin_constant(env, "VM_CREATION");
  73     mgmt_origin = find_origin_constant(env, "MANAGEMENT");
  74     envvar_origin = find_origin_constant(env, "ENVIRON_VAR");
  75     config_file_origin = find_origin_constant(env, "CONFIG_FILE");
  76     ergo_origin = find_origin_constant(env, "ERGONOMIC");
  77     other_origin = find_origin_constant(env, "OTHER");
  78 }
  79 
  80 JNIEXPORT jint JNICALL
  81 Java_sun_management_Flag_getFlags
  82   (JNIEnv *env, jclass cls, jobjectArray names, jobjectArray flags, jint count)
  83 {
  84     jint num_flags, i, index;
  85     jmmVMGlobal* globals;
  86     size_t gsize;
  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     gsize = count * sizeof(jmmVMGlobal);
 104     globals = (jmmVMGlobal*) malloc(gsize);
 105     if (globals == NULL) {
 106         JNU_ThrowOutOfMemoryError(env, 0);
 107         return 0;
 108     }
 109 
 110     memset(globals, 0, gsize);
 111     num_flags = jmm_interface->GetVMGlobals(env, names, globals, count);
 112     if (num_flags == 0) {
 113         free(globals);
 114         return 0;
 115     }
 116 
 117     index = 0;
 118     for (i = 0; i < count; i++) {
 119         if (globals[i].name == NULL) {
 120             continue;
 121         }
 122         switch (globals[i].type) {
 123         case JMM_VMGLOBAL_TYPE_JBOOLEAN:
 124             valueObj = JNU_NewObjectByName(env, "java/lang/Boolean", "(Z)V",
 125                                            globals[i].value.z);
 126             break;
 127         case JMM_VMGLOBAL_TYPE_JSTRING:
 128             valueObj = globals[i].value.l;
 129             break;
 130         case JMM_VMGLOBAL_TYPE_JLONG:
 131             valueObj = JNU_NewObjectByName(env, "java/lang/Long", "(J)V",
 132                                            globals[i].value.j);
 133             break;
 134         default:
 135             // ignore unsupported type
 136             continue;
 137         }
 138         switch (globals[i].origin) {
 139         case JMM_VMGLOBAL_ORIGIN_DEFAULT:
 140             origin = default_origin;
 141             break;
 142         case JMM_VMGLOBAL_ORIGIN_COMMAND_LINE:
 143             origin = vm_creation_origin;
 144             break;
 145         case JMM_VMGLOBAL_ORIGIN_MANAGEMENT:
 146             origin = mgmt_origin;
 147             break;
 148         case JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR:
 149             origin = envvar_origin;
 150             break;
 151         case JMM_VMGLOBAL_ORIGIN_CONFIG_FILE:
 152             origin = config_file_origin;
 153             break;
 154         case JMM_VMGLOBAL_ORIGIN_ERGONOMIC:
 155             origin = ergo_origin;
 156             break;
 157         case JMM_VMGLOBAL_ORIGIN_OTHER:
 158             origin = other_origin;
 159             break;
 160         default:
 161             // unknown origin
 162             origin = other_origin;
 163             break;
 164         }
 165         flag = JNU_NewObjectByName(env, class_name, signature, globals[i].name,
 166                                    valueObj, globals[i].writeable,
 167                                    globals[i].external, origin);
 168         if (flag == NULL) {
 169             free(globals);
 170             JNU_ThrowOutOfMemoryError(env, 0);
 171             return 0;
 172         }
 173         (*env)->SetObjectArrayElement(env, flags, index, flag);
 174         index++;
 175     }
 176 
 177     if (index != num_flags) {
 178         JNU_ThrowInternalError(env, "Number of Flag objects created unmatched");
 179         free(globals);
 180         return 0;
 181     }
 182 
 183     free(globals);
 184 
 185     /* return the number of Flag objects created */
 186     return num_flags;
 187 }
 188 
 189 JNIEXPORT void JNICALL
 190 Java_sun_management_Flag_setLongValue
 191   (JNIEnv *env, jclass cls, jstring name, jlong value)
 192 {
 193    jvalue v;
 194    v.j = value;
 195 
 196    jmm_interface->SetVMGlobal(env, name, v);
 197 }
 198 
 199 JNIEXPORT void JNICALL
 200 Java_sun_management_Flag_setBooleanValue
 201   (JNIEnv *env, jclass cls, jstring name, jboolean value)
 202 {
 203    jvalue v;
 204    v.z = value;
 205 
 206    jmm_interface->SetVMGlobal(env, name, v);
 207 }
 208 
 209 JNIEXPORT void JNICALL
 210 Java_sun_management_Flag_setStringValue
 211   (JNIEnv *env, jclass cls, jstring name, jstring value)
 212 {
 213    jvalue v;
 214    v.l = value;
 215 
 216    jmm_interface->SetVMGlobal(env, name, v);
 217 }
--- EOF ---