Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add context ucontext for windows.
[simgrid.git] / src / java / jxbt_utilities.c
1 /* Various JNI helper functions                                             */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7   * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdlib.h>             /* abort */
10 #include "xbt/misc.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/str.h"
13 #include "jxbt_utilities.h"
14
15 /* *********** */
16 /* JNI GETTERS */
17 /* *********** */
18
19 jclass jxbt_get_class(JNIEnv * env, const char *name)
20 {
21   jclass cls = (*env)->FindClass(env, name);
22
23   if (!cls) {
24     char *m = bprintf("Class %s not found", name);
25     jxbt_throw_jni(env, m);
26     free(m);
27     return NULL;
28   }
29
30   return cls;
31 }
32
33 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls,
34                            const char *name, const char *signature)
35 {
36   jmethodID id;
37
38   if (!cls)
39     return 0;
40   id = (*env)->GetMethodID(env, cls, name, signature);
41
42   if (!id) {
43
44     jmethodID tostr_id =
45       (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
46     jstring jclassname =
47       (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
48     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
49
50     char *m =
51       bprintf("Cannot find method %s(%s) in %s", name, signature, classname);
52
53     (*env)->ReleaseStringUTFChars(env, jclassname, classname);
54
55     jxbt_throw_jni(env, m);
56
57     free(m);
58     return 0;
59   }
60
61   return id;
62 }
63
64 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls,
65                                   const char *name, const char *signature)
66 {
67   jmethodID id;
68
69   if (!cls)
70     return 0;
71   id = (*env)->GetStaticMethodID(env, cls, name, signature);
72
73   if (!id) {
74
75     jmethodID tostr_id =
76       (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
77     jstring jclassname =
78       (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
79     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
80
81     char *m =
82       bprintf("Cannot find static method %s(%s) in %s", name, signature,
83               classname);
84
85     (*env)->ReleaseStringUTFChars(env, jclassname, classname);
86
87     jxbt_throw_jni(env, m);
88
89     free(m);
90     return 0;
91   }
92
93   return id;
94 }
95
96 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname,
97                                   const char *name, const char *signature)
98 {
99
100   jclass cls;
101   jmethodID id;
102   cls = jxbt_get_class(env, classname);
103
104   if (!cls)
105     return 0;
106
107   id = (*env)->GetStaticMethodID(env, cls, name, signature);
108
109   if (!id) {
110     char *m =
111       bprintf("Cannot find static method %s(%s) in %s", name, signature,
112               classname);
113
114     jxbt_throw_jni(env, m);
115
116     free(m);
117     return 0;
118   }
119   return id;
120 }
121
122 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname,
123                            const char *name, const char *signature)
124 {
125
126   jclass cls;
127   jmethodID id;
128   cls = jxbt_get_class(env, classname);
129
130   if (!cls)
131     return 0;
132
133   id = (*env)->GetMethodID(env, cls, name, signature);
134
135   if (!id) {
136     char *m =
137       bprintf("Cannot find method %s(%s) in %s", name, signature, classname);
138
139     jxbt_throw_jni(env, m);
140
141     free(m);
142     return 0;
143   }
144   return id;
145 }
146
147 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls,
148                          const char *name, const char *signature)
149 {
150   jfieldID id;
151
152   if (!cls)
153     return 0;
154
155   id = (*env)->GetFieldID(env, cls, name, signature);
156
157   if (!id) {
158     jmethodID getname_id =
159       (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
160     jstring jclassname =
161       (jstring) (*env)->CallObjectMethod(env, cls, getname_id, NULL);
162     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
163     char *m =
164       bprintf("Cannot find field %s %s in %s", signature, name, classname);
165
166     (*env)->ReleaseStringUTFChars(env, jclassname, classname);
167
168     jxbt_throw_jni(env, m);
169
170     free(m);
171     return 0;
172   }
173
174   return id;
175 }
176
177 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname,
178                          const char *name, const char *signature)
179 {
180   jclass cls = jxbt_get_class(env, classname);
181   jfieldID id;
182
183   if (!cls)
184     return 0;
185
186   id = (*env)->GetFieldID(env, cls, name, signature);
187
188   if (!id) {
189     char *m =
190       bprintf("Cannot find field %s %s in %s", signature, name, classname);
191
192     jxbt_throw_jni(env, m);
193
194     free(m);
195     return 0;
196   }
197
198   return id;
199 }
200
201 /* ***************** */
202 /* EXCEPTION RAISING */
203 /* ***************** */
204 static void jxbt_throw_by_name(JNIEnv * env, const char *name, char *msg)
205 {
206   jclass cls = (*env)->FindClass(env, name);
207
208   xbt_assert2(cls, "%s (Plus severe error: class %s not found)\n", msg, name);
209
210   (*env)->ThrowNew(env, cls, msg);
211
212   free(msg);
213 }
214
215
216 /* Errors in MSG */
217 void jxbt_throw_jni(JNIEnv * env, const char *msg)
218 {
219   jxbt_throw_by_name(env,
220                      "simgrid/msg/JniException",
221                      bprintf("Internal or JNI error: %s", msg));
222 }
223
224 void jxbt_throw_notbound(JNIEnv * env, const char *kind, void *pointer)
225 {
226   jxbt_throw_by_name(env,
227                      "simgrid/msg/JniException",
228                      bprintf("Internal error: %s %p not bound", kind,
229                              pointer));
230 }
231
232 void jxbt_throw_native(JNIEnv * env, char *msg)
233 {
234   jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
235 }
236
237 /* *** */
238 void jxbt_throw_null(JNIEnv * env, char *msg)
239 {
240   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
241 }
242
243 /* Errors on user side */
244 void jxbt_throw_illegal(JNIEnv * env, char *msg)
245 {
246   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
247 }
248
249 void jxbt_throw_host_not_found(JNIEnv * env, const char *invalid_name)
250 {
251   jxbt_throw_by_name(env,
252                      "simgrid/msg/HostNotFoundException",
253                      bprintf("No such host: %s", invalid_name));
254 }
255
256 void jxbt_throw_process_not_found(JNIEnv * env, const char *invalid_name)
257 {
258   jxbt_throw_by_name(env,
259                      "simgrid/msg/ProcessNotFoundException",
260                      bprintf("No such process: %s", invalid_name));
261 }
262
263 // tranfert failure
264 void jxbt_throw_transfer_failure(JNIEnv *env,char *details) {
265   
266   jxbt_throw_by_name(env,
267                      "simgrid/msg/TransferFailureException",
268                      details);
269   
270 }
271
272 // host failure Exception
273 void jxbt_throw_host_failure(JNIEnv *env,char *details) {
274   
275  jxbt_throw_by_name(env,
276                     "simgrid/msg/HostFailureException",
277                     bprintf("Host Failure %s",details));
278   
279 }
280
281 // time out failure Exception
282 void jxbt_throw_time_out_failure(JNIEnv *env,char *details) {
283   
284   jxbt_throw_by_name(env,
285                     "simgrid/msg/TimeoutException",
286                     details);
287   
288 }
289
290 // task Cancelled exception
291 void jxbt_throw_task_cancelled(JNIEnv *env,char *details)
292 {
293  
294   jxbt_throw_by_name(env,
295                      "simgrid/msg/TaskCancelledException",
296                      details);
297   
298 }