📄 rights_jni.c
字号:
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* http://www.opensource.org/licenses/cddl1.php
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* http://www.opensource.org/licenses/cddl1.php. If
* applicable, add the following below this CDDL HEADER,
* with the fields enclosed by brackets "[]" replaced
* with your own identifying information:
* Portions Copyright [yyyy]
* [name of copyright owner]
*/
/*
* $(@)rights_jni.c $Revision: 1.1.1.1 $ $Date: 2006/07/31 17:37:53 $
*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*/
#include <jni.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include "rights_xface.h"
#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
#ifndef DEFAULT_CLASSPATH
#define DEFAULT_CLASSPATH "../mmi_client" /* classpath */
#endif /* DEFAULT_CLASSPATH */
#define LOGGING_PATH "./logging.properties"
#define ACTION_STAT_CLASS_NAME "org/omc/dream/mmi/client/ActionStatNative" /* name of actionStat class */
#define RIGHTS_CLASS_NAME "org/omc/dream/mmi/client/MMIRightsManager" /* name of java mmi class */
#define STRING_CLASS_NAME "java/lang/String"
#define CHECK_ACTION "checkAction"
#define CHECK_ACTION_SIG "(ILjava/lang/String;Lorg/omc/dream/mmi/client/ActionStatNative;)Z"
#define REPORT_USAGE "reportUsage"
#define REPORT_USAGE_SIG "(ILjava/lang/String;)V"
#define PREPARE_KEYS "prepareKeys"
#define PREPARE_KEYS_SIG "(ILjava/lang/String;)V"
#define RELEASE_RIGHTS "releaseRights"
#define RELEASE_RIGHTS_SIG "(Ljava/lang/String;)V"
#define CONS_SIG "()V" /* signature of a simple constructor */
typedef struct
{
JavaVM *jvm;
/* this should be loaded everytime for a jni call from a new thread */
JNIEnv *env;
/* following variables have global references.*/
jclass rights_class;
jclass action_stat_class;
jclass string_class;
jobject rights_mgr;
}JNIData;
/*
* checks for exceptions
*/
unsigned char checkException( JNIEnv *env )
{
jthrowable exc;
exc = (*env)->ExceptionOccurred(env);
if( exc ) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear( env );
return 1;
}
return 0;
}
/*
* only creates objects with simple constructors without args
* if classname is NULL then gclass must not be NULL;
* gclass can be non NULL if there is a cached class reference already
* available.
*/
jobject createNewJNIObject( JNIEnv *env, char *classname, jclass gclass )
{
jclass cls;
if ( env == NULL ) {
return NULL;
}
if ( classname != NULL ) {
cls = (*env)->FindClass( env, classname );
if ( cls == NULL ) {
checkException( env );
fprintf( stderr, "\ncreateNewJNIObject: cannot find class %s", classname );
return NULL;
}
} else if ( gclass == NULL ) {
fprintf( stderr, "\ncreateNewJNIObject: gclass cannot be NULL");
return NULL;
} else {
cls = gclass;
}
jmethodID mid = (*env)->GetMethodID( env, cls, "<init>", CONS_SIG );
if ( mid == NULL ) {
checkException( env );
fprintf( stderr, "\ncreateNewJNIObject: no simple constructor found");
return NULL;
}
jobject obj = (*env)->NewObject( env, cls, mid );
if ( obj == NULL ) {
checkException( env );
fprintf( stderr, "\ncreateNewJNIObject: cannot instantiate object");
return NULL;
}
/* create a weak global reference. In case we forget to delete the reference */
jobject gobj = (*(env))->NewWeakGlobalRef( env, obj );
if ( gobj == NULL ) {
checkException( env );
}
(*(env))->DeleteLocalRef( env, obj );
#ifdef _DEBUG
fprintf( stderr, "\n%s:%d:: weak gloabl ref created for class %p ptr: %p ", __FILE__, __LINE__, cls, gobj );
#endif /* _DEBUG */
return gobj;
}
void clearRefs( JNIData *data )
{
if ( data->rights_class != NULL ) {
(*(data->env))->DeleteGlobalRef( data->env, data->rights_class );
data->rights_class = NULL;
}
if( data->rights_mgr != NULL ) {
(*(data->env))->DeleteGlobalRef( data->env, data->rights_mgr );
data->rights_mgr = NULL;
}
if ( data->action_stat_class != NULL ) {
(*(data->env))->DeleteGlobalRef( data->env, data->action_stat_class );
data->action_stat_class = NULL;
}
if ( data->string_class != NULL ) {
(*(data->env))->DeleteGlobalRef( data->env, data->string_class );
data->string_class = NULL;
}
}
/* Creates a jvm and returns a pointer to that */
void * jniInit( char * classpath )
{
jint res;
char *default_classpath = DEFAULT_CLASSPATH;
char user_classpath[1024];
char *env_classpath;
JNIData *jni_data = (JNIData *)malloc( sizeof( JNIData ) );
if ( jni_data == NULL ) {
fprintf( stderr, "\n%s:%d::Malloc failed", __FILE__, __LINE__ );
exit( -1 );
}
/*
if( classpath == NULL ) {
env_classpath = getenv("CLASSPATH");
if( env_classpath == NULL ) {
user_classpath = (char *)malloc( strlen( default_classpath ) );
strcpy( user_classpath, default_classpath );
} else {
user_classpath = (char *)malloc( strlen( env_classpath ) );
strcpy( user_classpath, env_classpath );
}
} else {
user_classpath = (char *)malloc( strlen( classpath ) );
strcpy( user_classpath, env_classpath );
}
*/
env_classpath = getenv("CLASSPATH");
if( env_classpath ) {
strcpy( user_classpath, env_classpath );
}
#ifdef _DEBUG
fprintf( stderr, "\n%s:%d::classpath %s\n", __FILE__, __LINE__, user_classpath );
#endif /* _DEBUG */
/* store class name */
#ifndef JNI_VERSION_1_2
fprintf(stderr, "\nOnly JNI version 1.2 is supported");
exit(-1);
#endif /* JNI_VERSION_1_2 */
JavaVMInitArgs vm_args;
JavaVMOption options[3];
options[0].optionString = (char *) malloc( strlen(user_classpath) + 30 );
strcpy( options[0].optionString, "-Djava.class.path=" );
strcat( options[0].optionString, user_classpath );
options[1].optionString = (char *) malloc( strlen(LOGGING_PATH) + 50 );
strcpy( options[1].optionString, "-Djava.util.logging.config.file=" );
strcat( options[1].optionString, LOGGING_PATH );
#ifdef _DEBUG
fprintf( stderr, "\n%s:%d::jniinit - loggingpath %s", __FILE__, __LINE__, options[1].optionString );
#endif /* _DEBUG */
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = JNI_FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -