Flag.c revision 0
0N/A/*
0N/A * Copyright 2003-2005 Sun Microsystems, Inc. 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
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A#include <stdlib.h>
0N/A#include <stdio.h>
0N/A#include <jni.h>
0N/A#include "management.h"
0N/A#include "sun_management_Flag.h"
0N/A
0N/Astatic jobject default_origin = NULL;
0N/Astatic jobject vm_creation_origin = NULL;
0N/Astatic jobject mgmt_origin = NULL;
0N/Astatic jobject envvar_origin = NULL;
0N/Astatic jobject config_file_origin = NULL;
0N/Astatic jobject ergo_origin = NULL;
0N/Astatic jobject other_origin = NULL;
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_management_Flag_getInternalFlagCount
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A jlong count = jmm_interface->GetLongAttribute(env, NULL,
0N/A JMM_VM_GLOBAL_COUNT);
0N/A return (jint) count;
0N/A}
0N/A
0N/AJNIEXPORT jobjectArray JNICALL
0N/A Java_sun_management_Flag_getAllFlagNames
0N/A(JNIEnv *env, jclass cls)
0N/A{
0N/A return jmm_interface->GetVMGlobalNames(env);
0N/A}
0N/A
0N/Astatic jobject find_origin_constant(JNIEnv* env, const char* enum_name) {
0N/A jvalue field;
0N/A field = JNU_GetStaticFieldByName(env,
0N/A NULL,
0N/A "com/sun/management/VMOption$Origin",
0N/A enum_name,
0N/A "Lcom/sun/management/VMOption$Origin;");
0N/A return (*env)->NewGlobalRef(env, field.l);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_management_Flag_initialize
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A default_origin = find_origin_constant(env, "DEFAULT");
0N/A vm_creation_origin = find_origin_constant(env, "VM_CREATION");
0N/A mgmt_origin = find_origin_constant(env, "MANAGEMENT");
0N/A envvar_origin = find_origin_constant(env, "ENVIRON_VAR");
0N/A config_file_origin = find_origin_constant(env, "CONFIG_FILE");
0N/A ergo_origin = find_origin_constant(env, "ERGONOMIC");
0N/A other_origin = find_origin_constant(env, "OTHER");
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_management_Flag_getFlags
0N/A (JNIEnv *env, jclass cls, jobjectArray names, jobjectArray flags, jint count)
0N/A{
0N/A char errmsg[128];
0N/A
0N/A jint num_flags, i, index;
0N/A jmmVMGlobal* globals;
0N/A const char* class_name = "sun/management/Flag";
0N/A const char* signature = "(Ljava/lang/String;Ljava/lang/Object;ZZLcom/sun/management/VMOption$Origin;)V";
0N/A jobject origin;
0N/A jobject valueObj;
0N/A jobject flag;
0N/A
0N/A if (flags == NULL) {
0N/A JNU_ThrowNullPointerException(env, 0);
0N/A return 0;
0N/A }
0N/A
0N/A if (count == 0) {
0N/A JNU_ThrowIllegalArgumentException(env, 0);
0N/A return 0;
0N/A }
0N/A
0N/A globals = (jmmVMGlobal*) malloc(count * sizeof(jmmVMGlobal));
0N/A if (globals == NULL) {
0N/A JNU_ThrowOutOfMemoryError(env, 0);
0N/A return 0;
0N/A }
0N/A
0N/A num_flags = jmm_interface->GetVMGlobals(env, names, globals, count);
0N/A if (num_flags == 0) {
0N/A free(globals);
0N/A return 0;
0N/A }
0N/A
0N/A index = 0;
0N/A for (i = 0; i < count; i++) {
0N/A if (globals[i].name == NULL) {
0N/A continue;
0N/A }
0N/A switch (globals[i].type) {
0N/A case JMM_VMGLOBAL_TYPE_JBOOLEAN:
0N/A valueObj = JNU_NewObjectByName(env, "java/lang/Boolean", "(Z)V",
0N/A globals[i].value.z);
0N/A break;
0N/A case JMM_VMGLOBAL_TYPE_JSTRING:
0N/A valueObj = globals[i].value.l;
0N/A break;
0N/A case JMM_VMGLOBAL_TYPE_JLONG:
0N/A valueObj = JNU_NewObjectByName(env, "java/lang/Long", "(J)V",
0N/A globals[i].value.j);
0N/A break;
0N/A default:
0N/A // unsupported type
0N/A sprintf(errmsg, "Unsupported VMGlobal Type %d", globals[i].type);
0N/A JNU_ThrowInternalError(env, errmsg);
0N/A free(globals);
0N/A return 0;
0N/A }
0N/A switch (globals[i].origin) {
0N/A case JMM_VMGLOBAL_ORIGIN_DEFAULT:
0N/A origin = default_origin;
0N/A break;
0N/A case JMM_VMGLOBAL_ORIGIN_COMMAND_LINE:
0N/A origin = vm_creation_origin;
0N/A break;
0N/A case JMM_VMGLOBAL_ORIGIN_MANAGEMENT:
0N/A origin = mgmt_origin;
0N/A break;
0N/A case JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR:
0N/A origin = envvar_origin;
0N/A break;
0N/A case JMM_VMGLOBAL_ORIGIN_CONFIG_FILE:
0N/A origin = config_file_origin;
0N/A break;
0N/A case JMM_VMGLOBAL_ORIGIN_ERGONOMIC:
0N/A origin = ergo_origin;
0N/A break;
0N/A case JMM_VMGLOBAL_ORIGIN_OTHER:
0N/A origin = other_origin;
0N/A break;
0N/A default:
0N/A // unknown origin
0N/A origin = other_origin;
0N/A break;
0N/A }
0N/A flag = JNU_NewObjectByName(env, class_name, signature, globals[i].name,
0N/A valueObj, globals[i].writeable,
0N/A globals[i].external, origin);
0N/A if (flag == NULL) {
0N/A free(globals);
0N/A JNU_ThrowOutOfMemoryError(env, 0);
0N/A return 0;
0N/A }
0N/A (*env)->SetObjectArrayElement(env, flags, index, flag);
0N/A index++;
0N/A }
0N/A
0N/A if (index != num_flags) {
0N/A JNU_ThrowInternalError(env, "Number of Flag objects created unmatched");
0N/A free(globals);
0N/A return 0;
0N/A }
0N/A
0N/A free(globals);
0N/A
0N/A /* return the number of Flag objects created */
0N/A return num_flags;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_management_Flag_setLongValue
0N/A (JNIEnv *env, jclass cls, jstring name, jlong value)
0N/A{
0N/A jvalue v;
0N/A v.j = value;
0N/A
0N/A jmm_interface->SetVMGlobal(env, name, v);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_management_Flag_setBooleanValue
0N/A (JNIEnv *env, jclass cls, jstring name, jboolean value)
0N/A{
0N/A jvalue v;
0N/A v.z = value;
0N/A
0N/A jmm_interface->SetVMGlobal(env, name, v);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_management_Flag_setStringValue
0N/A (JNIEnv *env, jclass cls, jstring name, jstring value)
0N/A{
0N/A jvalue v;
0N/A v.l = value;
0N/A
0N/A jmm_interface->SetVMGlobal(env, name, v);
0N/A}