1 /* Java bindings of the Storage API. */
3 /* Copyright (c) 2012-2019. 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/msg.h"
9 #include "simgrid/plugins/file_system.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, msg_storage_t storage, JNIEnv * env) {
43 env->SetLongField(jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage));
46 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
47 return env->NewGlobalRef(jstorage);
50 void jstorage_unref(JNIEnv * env, jobject jstorage) {
51 env->DeleteGlobalRef(jstorage);
54 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) {
55 msg_storage_t storage;
56 jobject jstorage = nullptr;
58 /* get the C string from the java string */
59 if (jname == nullptr) {
60 jxbt_throw_null(env, "No host can have a null name");
63 const char *name = env->GetStringUTFChars(jname, 0);
64 storage = MSG_storage_get_by_name(name);
66 if (not storage) { /* invalid name */
67 jxbt_throw_storage_not_found(env, name);
68 env->ReleaseStringUTFChars(jname, name);
71 env->ReleaseStringUTFChars(jname, name);
73 if (java_storage_map.find(storage) == java_storage_map.end()) {
74 /* Instantiate a new java storage */
75 jstorage = jstorage_new_instance(env);
78 jxbt_throw_jni(env, "java storage instantiation failed");
82 /* get a global reference to the newly created storage */
83 jstorage = jstorage_ref(env, jstorage);
86 jxbt_throw_jni(env, "new global ref allocation failed");
89 /* Sets the java storage name */
90 env->SetObjectField(jstorage, jstorage_field_Storage_name, jname);
91 /* bind the java storage and the native storage */
92 jstorage_bind(jstorage, storage, env);
94 /* the native storage data field is set with the global reference to the
95 * java storage returned by this function
97 java_storage_map.insert({storage, jstorage});
99 jstorage = java_storage_map.at(storage);
101 /* return the global reference to the java storage instance */
102 return (jobject)jstorage;
105 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
106 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
109 jxbt_throw_notbound(env, "storage", jstorage);
113 return (jlong) MSG_storage_get_size(storage);
116 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
117 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
120 jxbt_throw_notbound(env, "storage", jstorage);
124 return (jlong) MSG_storage_get_free_size(storage);
127 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
128 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
131 jxbt_throw_notbound(env, "storage", jstorage);
135 return (jlong) MSG_storage_get_used_size(storage);
138 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
139 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
142 jxbt_throw_notbound(env, "storage", jstorage);
145 const char *name = env->GetStringUTFChars((jstring) jname, 0);
147 const char *property = MSG_storage_get_property_value(storage, name);
151 jobject jproperty = env->NewStringUTF(property);
153 env->ReleaseStringUTFChars((jstring) jname, name);
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);
163 jxbt_throw_notbound(env, "storage", jstorage);
166 const char *name = env->GetStringUTFChars((jstring) jname, 0);
167 const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
169 storage->set_property(name, std::string(value_java));
171 env->ReleaseStringUTFChars((jstring) jvalue, value_java);
172 env->ReleaseStringUTFChars((jstring) jname, name);
175 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
176 const_sg_storage_t storage = jstorage_get_native(env, jstorage);
179 jxbt_throw_notbound(env, "storage", jstorage);
182 const char *host_name = MSG_storage_get_host(storage);
186 jobject jhost_name = env->NewStringUTF(host_name);
191 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
197 msg_storage_t storage;
199 xbt_dynar_t table = MSG_storages_as_dynar();
200 int count = xbt_dynar_length(table);
202 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
208 jtable = env->NewObjectArray((jsize) count, cls, nullptr);
211 jxbt_throw_jni(env, "Storages table allocation failed");
215 for (index = 0; index < count; index++) {
216 storage = xbt_dynar_get_as(table,index,msg_storage_t);
217 if (java_storage_map.find(storage) != java_storage_map.end()) {
218 jstorage = java_storage_map.at(storage);
220 jname = env->NewStringUTF(MSG_storage_get_name(storage));
221 jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
224 env->SetObjectArrayElement(jtable, index, jstorage);
226 xbt_dynar_free(&table);