1 /* Java bindings of the Storage API. */
3 /* Copyright (c) 2012-2017. 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);
21 static jmethodID jstorage_method_Storage_constructor;
22 static jfieldID jstorage_field_Storage_bind;
23 static jfieldID jstorage_field_Storage_name;
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);
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);
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");
44 void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) {
45 env->SetLongField(jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage));
48 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
49 return env->NewGlobalRef(jstorage);
52 void jstorage_unref(JNIEnv * env, jobject jstorage) {
53 env->DeleteGlobalRef(jstorage);
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;
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");
65 const char *name = env->GetStringUTFChars(jname, 0);
66 storage = MSG_storage_get_by_name(name);
68 if (not storage) { /* invalid name */
69 jxbt_throw_storage_not_found(env, name);
70 env->ReleaseStringUTFChars(jname, name);
73 env->ReleaseStringUTFChars(jname, name);
75 if (java_storage_map.find(storage) == java_storage_map.end()) {
76 /* Instantiate a new java storage */
77 jstorage = jstorage_new_instance(env);
80 jxbt_throw_jni(env, "java storage instantiation failed");
84 /* get a global reference to the newly created storage */
85 jstorage = jstorage_ref(env, jstorage);
88 jxbt_throw_jni(env, "new global ref allocation failed");
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);
96 /* the native storage data field is set with the global reference to the
97 * java storage returned by this function
99 java_storage_map.insert({storage, jstorage});
101 jstorage = java_storage_map.at(storage);
103 /* return the global reference to the java storage instance */
104 return (jobject)jstorage;
107 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
108 msg_storage_t storage = jstorage_get_native(env, jstorage);
111 jxbt_throw_notbound(env, "storage", jstorage);
115 return (jlong) MSG_storage_get_size(storage);
118 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
119 msg_storage_t storage = jstorage_get_native(env, jstorage);
122 jxbt_throw_notbound(env, "storage", jstorage);
126 return (jlong) MSG_storage_get_free_size(storage);
129 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
130 msg_storage_t storage = jstorage_get_native(env, jstorage);
133 jxbt_throw_notbound(env, "storage", jstorage);
137 return (jlong) MSG_storage_get_used_size(storage);
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);
144 jxbt_throw_notbound(env, "storage", jstorage);
147 const char *name = env->GetStringUTFChars((jstring) jname, 0);
149 const char *property = MSG_storage_get_property_value(storage, name);
153 jobject jproperty = env->NewStringUTF(property);
155 env->ReleaseStringUTFChars((jstring) jname, name);
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);
165 jxbt_throw_notbound(env, "storage", jstorage);
168 const char *name = env->GetStringUTFChars((jstring) jname, 0);
169 const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
171 storage->setProperty(name, std::string(value_java));
173 env->ReleaseStringUTFChars((jstring) jvalue, value_java);
174 env->ReleaseStringUTFChars((jstring) jname, name);
178 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
179 msg_storage_t storage = jstorage_get_native(env, jstorage);
182 jxbt_throw_notbound(env, "storage", jstorage);
185 const char *host_name = MSG_storage_get_host(storage);
189 jobject jhost_name = env->NewStringUTF(host_name);
194 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
200 msg_storage_t storage;
202 xbt_dynar_t table = MSG_storages_as_dynar();
203 int count = xbt_dynar_length(table);
205 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
211 jtable = env->NewObjectArray((jsize) count, cls, nullptr);
214 jxbt_throw_jni(env, "Storages table allocation failed");
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);
223 jname = env->NewStringUTF(MSG_storage_get_name(storage));
224 jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
227 env->SetObjectArrayElement(jtable, index, jstorage);
229 xbt_dynar_free(&table);