1 /* Java bindings of the Storage API. */
3 /* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved. */
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. */
8 #include "simgrid/plugins/file_system.h"
9 #include "simgrid/storage.h"
11 #include "include/xbt/signal.hpp"
13 #include "jmsg_storage.h"
14 #include "jxbt_utilities.hpp"
15 #include "simgrid/s4u/Storage.hpp"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
19 static jmethodID jstorage_method_Storage_constructor;
20 static jfieldID jstorage_field_Storage_bind;
21 static jfieldID jstorage_field_Storage_name;
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);
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);
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");
42 void jstorage_bind(jobject jstorage, sg_storage_t storage, JNIEnv* env)
44 env->SetLongField(jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage));
47 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
48 return env->NewGlobalRef(jstorage);
51 void jstorage_unref(JNIEnv * env, jobject jstorage) {
52 env->DeleteGlobalRef(jstorage);
55 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) {
57 jobject jstorage = nullptr;
59 /* get the C string from the java string */
60 if (jname == nullptr) {
61 jxbt_throw_null(env, "No host can have a null name");
64 const char *name = env->GetStringUTFChars(jname, 0);
65 storage = sg_storage_get_by_name(name);
67 if (not storage) { /* invalid name */
68 jxbt_throw_storage_not_found(env, name);
69 env->ReleaseStringUTFChars(jname, name);
72 env->ReleaseStringUTFChars(jname, name);
74 if (java_storage_map.find(storage) == java_storage_map.end()) {
75 /* Instantiate a new java storage */
76 jstorage = jstorage_new_instance(env);
79 jxbt_throw_jni(env, "java storage instantiation failed");
83 /* get a global reference to the newly created storage */
84 jstorage = jstorage_ref(env, jstorage);
87 jxbt_throw_jni(env, "new global ref allocation failed");
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);
95 /* the native storage data field is set with the global reference to the
96 * java storage returned by this function
98 java_storage_map.insert({storage, jstorage});
100 jstorage = java_storage_map.at(storage);
102 /* return the global reference to the java storage instance */
103 return (jobject)jstorage;
106 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
107 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
110 jxbt_throw_notbound(env, "storage", jstorage);
114 return (jlong)sg_storage_get_size(storage);
117 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
118 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
121 jxbt_throw_notbound(env, "storage", jstorage);
125 return (jlong)sg_storage_get_size_free(storage);
128 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
129 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
132 jxbt_throw_notbound(env, "storage", jstorage);
136 return (jlong)sg_storage_get_size_used(storage);
139 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
140 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
143 jxbt_throw_notbound(env, "storage", jstorage);
146 const char *name = env->GetStringUTFChars((jstring) jname, 0);
148 const char* property = sg_storage_get_property_value(storage, name);
152 jobject jproperty = env->NewStringUTF(property);
154 env->ReleaseStringUTFChars((jstring) jname, name);
159 JNIEXPORT void JNICALL
160 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
161 sg_storage_t storage = jstorage_get_native(env, jstorage);
164 jxbt_throw_notbound(env, "storage", jstorage);
167 const char *name = env->GetStringUTFChars((jstring) jname, 0);
168 const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
170 storage->set_property(name, std::string(value_java));
172 env->ReleaseStringUTFChars((jstring) jvalue, value_java);
173 env->ReleaseStringUTFChars((jstring) jname, name);
176 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
177 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
180 jxbt_throw_notbound(env, "storage", jstorage);
183 const char* host_name = sg_storage_get_host(storage);
187 jobject jhost_name = env->NewStringUTF(host_name);
192 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
198 sg_storage_t storage;
200 xbt_dynar_t table = sg_storages_as_dynar();
201 int count = xbt_dynar_length(table);
203 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
209 jtable = env->NewObjectArray((jsize) count, cls, nullptr);
212 jxbt_throw_jni(env, "Storages table allocation failed");
216 for (index = 0; index < count; index++) {
217 storage = xbt_dynar_get_as(table, index, sg_storage_t);
218 if (java_storage_map.find(storage) != java_storage_map.end()) {
219 jstorage = java_storage_map.at(storage);
221 jname = env->NewStringUTF(sg_storage_get_name(storage));
222 jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
225 env->SetObjectArrayElement(jtable, index, jstorage);
227 xbt_dynar_free(&table);