Logo AND Algorithmique Numérique Distribuée

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