Logo AND Algorithmique Numérique Distribuée

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