Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't use %t in log format, but %P; revalidate the output after listener introduction
[simgrid.git] / src / java / jxbt_utilities.c
1 /*
2  * $Id$
3  *
4  * Various JNI helper functions
5  *
6  * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved. 
7  *
8  * This program is free software; you can redistribute it and/or modify it 
9  * under the terms of the license (GNU LGPL) which comes with this package.
10  *
11  */
12
13 #include <stdlib.h> /* abort */
14 #include "xbt/misc.h"
15 #include "xbt/sysdep.h"
16 #include "xbt/str.h"
17 #include "jxbt_utilities.h"
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
20
21 /* *********** */
22 /* JNI GETTERS */
23 /* *********** */
24
25 jclass jxbt_get_class(JNIEnv* env, const char*name) {
26   jclass cls = (*env)->FindClass(env, name);
27         
28   if(!cls) {
29     char *m=bprintf("Class %s not found",name);
30     jxbt_throw_jni(env, m);
31     free(m);
32     return NULL;
33   }
34
35   return cls;
36 }
37
38 jmethodID jxbt_get_jmethod(JNIEnv* env, jclass cls, 
39                            const char *name,const char *signature) {
40   jmethodID id;
41
42   if (!cls)
43     return 0;
44   id = (*env)->GetMethodID(env, cls, name,signature);
45         
46   if(!id) {
47
48     jmethodID tostr_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
49     jstring jclassname = (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
50     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
51
52     char *m=bprintf("Cannot find method %s(%s) in %s", name, signature ,classname);
53
54     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
55
56     jxbt_throw_jni(env,m);
57
58     free(m);
59     return 0;
60   }
61
62   return id;
63 }
64
65 jmethodID jxbt_get_smethod(JNIEnv* env, const char *classname, 
66                           const char *name,const char *signature) { 
67   jclass cls = jxbt_get_class(env,classname);
68   jmethodID id;
69
70   if (!cls)
71     return 0;
72   id = (*env)->GetMethodID(env, cls, name,signature);
73         
74   if(!id) {
75     char *m=bprintf("Cannot find method %s(%s) in %s", name, signature,classname);
76
77     jxbt_throw_jni(env,m);
78
79     free(m);
80     return 0;
81   }
82
83   return id;
84 }
85
86 jfieldID jxbt_get_jfield(JNIEnv* env, jclass cls, 
87                          const char *name, const char *signature) {
88   jfieldID id;
89
90   if (!cls)
91     return 0;
92
93   id = (*env)->GetFieldID(env, cls, name, signature);
94         
95   if(!id) {
96     jmethodID getname_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
97     jstring jclassname = (jstring) (*env)->CallObjectMethod(env,cls, getname_id, NULL);
98     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
99     char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
100
101     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
102
103     jxbt_throw_jni(env,m);
104
105     free(m);
106     return 0;
107   }
108
109   return id;
110 }
111
112 jfieldID jxbt_get_sfield(JNIEnv* env, const char *classname, 
113                         const char *name, const char *signature) {
114   jclass cls = jxbt_get_class(env,classname);
115   jfieldID id;
116
117   if (!cls)
118     return 0;
119
120   id = (*env)->GetFieldID(env, cls, name, signature);
121         
122   if(!id) {
123     char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
124
125     jxbt_throw_jni(env,m);
126
127     free(m);
128     return 0;
129   }
130
131   return id;
132 }
133
134 /* ***************** */
135 /* EXCEPTION RAISING */
136 /* ***************** */
137 static void jxbt_throw_by_name(JNIEnv* env,const char* name, char *msg) {
138    jclass cls = (*env)->FindClass(env, name);
139
140    xbt_assert2(cls,"%s (Plus severe error: class %s not found)\n",
141                msg,name);
142
143    (*env)->ThrowNew(env,cls,msg);
144
145    free(msg);
146 }
147
148
149 /* Errors in MSG */
150 void jxbt_throw_jni(JNIEnv* env,const char* msg) {
151   jxbt_throw_by_name(env,
152                      "simgrid/msg/JniException",
153                      bprintf("Internal or JNI error: %s",msg));
154 }
155 void jxbt_throw_notbound(JNIEnv* env,const char* kind, void *pointer) {
156   jxbt_throw_by_name(env,
157                      "simgrid/msg/JniException",
158                      bprintf("Internal error: %s %p not bound",kind, pointer));
159 }
160
161 void jxbt_throw_native(JNIEnv* env,char* msg) {
162   jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
163 }
164
165 /* *** */
166 void jxbt_throw_null(JNIEnv* env, char* msg) {
167   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
168 }
169
170
171 /* Errors on user side */
172 void jxbt_throw_illegal(JNIEnv* env, char* msg) {
173   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
174 }
175 void jxbt_throw_host_not_found(JNIEnv* env, const char *invalid_name) {
176   jxbt_throw_by_name(env,
177                      "simgrid/msg/HostNotFoundException",
178                      bprintf("No such host: %s",invalid_name));
179 }
180 void jxbt_throw_process_not_found(JNIEnv* env, const char *invalid_name) {
181   jxbt_throw_by_name(env,
182                      "simgrid/msg/ProcessNotFoundException",
183                      bprintf("No such process: %s",invalid_name));
184 }