Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d3fd8db93643e46dc58c9939117e00dd33147d29
[simgrid.git] / src / bindings / java / jmsg_storage.c
1 /* Functions related to the java storage API.                            */
2
3 /* Copyright (c) 2012-2015. 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 "xbt/str.h"
10 #include "simgrid/msg.h"
11 #include "jmsg.h"
12 #include "jmsg_storage.h"
13 #include "jxbt_utilities.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
16
17 static jmethodID jstorage_method_Storage_constructor;
18 static jfieldID jstorage_field_Storage_bind;
19 static jfieldID jstorage_field_Storage_name;
20
21 jobject jstorage_new_instance(JNIEnv * env) {
22   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
23   return (*env)->NewObject(env, cls, jstorage_method_Storage_constructor);
24 }
25
26 msg_storage_t jstorage_get_native(JNIEnv * env, jobject jstorage) {
27   return (msg_storage_t) (uintptr_t) (*env)->GetLongField(env, jstorage, jstorage_field_Storage_bind);
28 }
29
30 JNIEXPORT void JNICALL
31 Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) {
32   jclass class_Storage = (*env)->FindClass(env, "org/simgrid/msg/Storage");
33   jstorage_method_Storage_constructor = (*env)->GetMethodID(env, class_Storage, "<init>", "()V");
34   jstorage_field_Storage_bind = jxbt_get_jfield(env,class_Storage, "bind", "J");
35   jstorage_field_Storage_name = jxbt_get_jfield(env, class_Storage, "name", "Ljava/lang/String;");
36   if (!class_Storage || !jstorage_field_Storage_name || !jstorage_method_Storage_constructor || !jstorage_field_Storage_bind) {
37     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
38   }
39 }
40
41 void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) {
42   (*env)->SetLongField(env, jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage));
43 }
44
45 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
46   return (*env)->NewGlobalRef(env, jstorage);
47 }
48
49 void jstorage_unref(JNIEnv * env, jobject jstorage) {
50   (*env)->DeleteGlobalRef(env, jstorage);
51 }
52
53 const char *jstorage_get_name(jobject jstorage, JNIEnv * env) {
54   msg_storage_t storage = jstorage_get_native(env, jstorage);
55   return MSG_storage_get_name(storage);
56 }
57
58 JNIEXPORT jobject JNICALL
59 Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls,
60                                          jstring jname) {
61   msg_storage_t storage;
62   jobject jstorage;
63
64   /* get the C string from the java string */
65   if (jname == NULL) {
66         jxbt_throw_null(env,bprintf("No host can have a null name"));
67         return NULL;
68   }
69   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
70   storage = MSG_storage_get_by_name(name);
71
72   if (!storage) {                  /* invalid name */
73     jxbt_throw_storage_not_found(env, name);
74     (*env)->ReleaseStringUTFChars(env, jname, name);
75     return NULL;
76   }
77   (*env)->ReleaseStringUTFChars(env, jname, name);
78
79   if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) {       /* native host not associated yet with java host */
80
81     /* Instantiate a new java storage */
82     jstorage = jstorage_new_instance(env);
83
84     if (!jstorage) {
85       jxbt_throw_jni(env, "java storage instantiation failed");
86       return NULL;
87     }
88
89     /* get a global reference to the newly created storage */
90     jstorage = jstorage_ref(env, jstorage);
91
92     if (!jstorage) {
93       jxbt_throw_jni(env, "new global ref allocation failed");
94       return NULL;
95     }
96     /* Sets the java storage name */
97     (*env)->SetObjectField(env, jstorage, jstorage_field_Storage_name, jname);
98     /* bind the java storage and the native storage */
99     jstorage_bind(jstorage, storage, env);
100
101     /* the native storage data field is set with the global reference to the
102      * java storage returned by this function
103      */
104     xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage);
105   }
106
107   /* return the global reference to the java storage instance */
108   return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL);
109 }
110
111
112 JNIEXPORT jlong JNICALL
113 Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
114   msg_storage_t storage = jstorage_get_native(env, jstorage);
115
116   if (!storage) {
117     jxbt_throw_notbound(env, "storage", jstorage);
118     return -1;
119   }
120
121   return (jlong) MSG_storage_get_size(storage);
122 }
123
124 JNIEXPORT jlong JNICALL
125 Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
126   msg_storage_t storage = jstorage_get_native(env, jstorage);
127
128   if (!storage) {
129     jxbt_throw_notbound(env, "storage", jstorage);
130     return -1;
131   }
132
133   return (jlong) MSG_storage_get_free_size(storage);
134 }
135
136 JNIEXPORT jlong JNICALL
137 Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
138   msg_storage_t storage = jstorage_get_native(env, jstorage);
139
140   if (!storage) {
141     jxbt_throw_notbound(env, "storage", jstorage);
142     return -1;
143   }
144
145   return (jlong) MSG_storage_get_used_size(storage);
146 }
147
148 JNIEXPORT jobject JNICALL
149 Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
150   msg_storage_t storage = jstorage_get_native(env, jstorage);
151
152   if (!storage) {
153     jxbt_throw_notbound(env, "storage", jstorage);
154     return NULL;
155   }
156   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
157
158   const char *property = MSG_storage_get_property_value(storage, name);
159   if (!property) {
160     return NULL;
161   }
162   jobject jproperty = (*env)->NewStringUTF(env, property);
163
164   (*env)->ReleaseStringUTFChars(env, jname, name);
165
166   return jproperty;
167 }
168
169 JNIEXPORT void JNICALL
170 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
171         msg_storage_t storage = jstorage_get_native(env, jstorage);
172
173   if (!storage) {
174     jxbt_throw_notbound(env, "storage", jstorage);
175     return;
176   }
177   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
178   const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0);
179   char *value = xbt_strdup(value_java);
180
181   MSG_storage_set_property_value(storage, name, value, xbt_free_f);
182
183   (*env)->ReleaseStringUTFChars(env, jvalue, value);
184   (*env)->ReleaseStringUTFChars(env, jname, name);
185
186 }
187
188 JNIEXPORT jobject JNICALL
189 Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
190   msg_storage_t storage = jstorage_get_native(env, jstorage);
191
192   if (!storage) {
193     jxbt_throw_notbound(env, "storage", jstorage);
194     return NULL;
195   }
196   const char *host_name = MSG_storage_get_host(storage);
197   if (!host_name) {
198     return NULL;
199   }
200   jobject jhost_name = (*env)->NewStringUTF(env, host_name);
201
202   return jhost_name;
203 }
204
205 JNIEXPORT jobjectArray JNICALL
206 Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
207 {
208   int index;
209   jobjectArray jtable;
210   jobject jstorage;
211   jstring jname;
212   msg_storage_t storage;
213
214   xbt_dynar_t table =  MSG_storages_as_dynar();
215   int count = xbt_dynar_length(table);
216
217   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
218
219   if (!cls) {
220     return NULL;
221   }
222
223   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
224
225   if (!jtable) {
226     jxbt_throw_jni(env, "Storages table allocation failed");
227     return NULL;
228   }
229
230   for (index = 0; index < count; index++) {
231     storage = xbt_dynar_get_as(table,index,msg_storage_t);
232     jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL));
233
234     if (!jstorage) {
235       jname = (*env)->NewStringUTF(env, MSG_storage_get_name(storage));
236       jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
237     }
238
239     (*env)->SetObjectArrayElement(env, jtable, index, jstorage);
240   }
241   xbt_dynar_free(&table);
242   return jtable;
243 }