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"
11 #include "jmsg_storage.h"
12 #include "jxbt_utilities.hpp"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
18 static jmethodID jstorage_method_Storage_constructor;
19 static jfieldID jstorage_field_Storage_bind;
20 static jfieldID jstorage_field_Storage_name;
22 jobject jstorage_new_instance(JNIEnv * env) {
23 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
24 return env->NewObject(cls, jstorage_method_Storage_constructor);
27 msg_storage_t jstorage_get_native(JNIEnv * env, jobject jstorage) {
28 return (msg_storage_t) (uintptr_t) env->GetLongField(jstorage, jstorage_field_Storage_bind);
31 JNIEXPORT void JNICALL Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) {
32 jclass class_Storage = env->FindClass("org/simgrid/msg/Storage");
33 xbt_assert(class_Storage, "Native initialization of msg/Storage failed. Please report that bug");
34 jstorage_method_Storage_constructor = env->GetMethodID(class_Storage, "<init>", "()V");
35 jstorage_field_Storage_bind = jxbt_get_jfield(env,class_Storage, "bind", "J");
36 jstorage_field_Storage_name = jxbt_get_jfield(env, class_Storage, "name", "Ljava/lang/String;");
37 xbt_assert(jstorage_field_Storage_name && jstorage_method_Storage_constructor && jstorage_field_Storage_bind,
38 "Native initialization of msg/Storage failed. Please report that bug");
41 void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) {
42 env->SetLongField(jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage));
45 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
46 return env->NewGlobalRef(jstorage);
49 void jstorage_unref(JNIEnv * env, jobject jstorage) {
50 env->DeleteGlobalRef(jstorage);
53 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) {
54 msg_storage_t storage;
55 jobject jstorage = nullptr;
57 /* get the C string from the java string */
58 if (jname == nullptr) {
59 jxbt_throw_null(env, "No host can have a null name");
62 const char *name = env->GetStringUTFChars(jname, 0);
63 storage = MSG_storage_get_by_name(name);
65 if (not storage) { /* invalid name */
66 jxbt_throw_storage_not_found(env, name);
67 env->ReleaseStringUTFChars(jname, name);
70 env->ReleaseStringUTFChars(jname, name);
72 if (java_storage_map.find(storage) == java_storage_map.end()) {
73 /* Instantiate a new java storage */
74 jstorage = jstorage_new_instance(env);
77 jxbt_throw_jni(env, "java storage instantiation failed");
81 /* get a global reference to the newly created storage */
82 jstorage = jstorage_ref(env, jstorage);
85 jxbt_throw_jni(env, "new global ref allocation failed");
88 /* Sets the java storage name */
89 env->SetObjectField(jstorage, jstorage_field_Storage_name, jname);
90 /* bind the java storage and the native storage */
91 jstorage_bind(jstorage, storage, env);
93 /* the native storage data field is set with the global reference to the
94 * java storage returned by this function
96 java_storage_map.insert({storage, jstorage});
98 jstorage = java_storage_map.at(storage);
100 /* return the global reference to the java storage instance */
101 return (jobject)jstorage;
104 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
105 msg_storage_t storage = jstorage_get_native(env, jstorage);
108 jxbt_throw_notbound(env, "storage", jstorage);
112 return (jlong) MSG_storage_get_size(storage);
115 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
116 msg_storage_t storage = jstorage_get_native(env, jstorage);
119 jxbt_throw_notbound(env, "storage", jstorage);
123 return (jlong) MSG_storage_get_free_size(storage);
126 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
127 msg_storage_t storage = jstorage_get_native(env, jstorage);
130 jxbt_throw_notbound(env, "storage", jstorage);
134 return (jlong) MSG_storage_get_used_size(storage);
137 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
138 msg_storage_t storage = jstorage_get_native(env, jstorage);
141 jxbt_throw_notbound(env, "storage", jstorage);
144 const char *name = env->GetStringUTFChars((jstring) jname, 0);
146 const char *property = MSG_storage_get_property_value(storage, name);
150 jobject jproperty = env->NewStringUTF(property);
152 env->ReleaseStringUTFChars((jstring) jname, name);
157 JNIEXPORT void JNICALL
158 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
159 msg_storage_t storage = jstorage_get_native(env, jstorage);
162 jxbt_throw_notbound(env, "storage", jstorage);
165 const char *name = env->GetStringUTFChars((jstring) jname, 0);
166 const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
167 char *value = xbt_strdup(value_java);
169 MSG_storage_set_property_value(storage, name, value);
171 env->ReleaseStringUTFChars((jstring) jvalue, value_java);
172 env->ReleaseStringUTFChars((jstring) jname, name);
176 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
177 msg_storage_t storage = jstorage_get_native(env, jstorage);
180 jxbt_throw_notbound(env, "storage", jstorage);
183 const char *host_name = MSG_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 msg_storage_t storage;
200 xbt_dynar_t table = MSG_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,msg_storage_t);
218 if (java_storage_map.find(storage) != java_storage_map.end()) {
219 jstorage = java_storage_map.at(storage);
221 jname = env->NewStringUTF(MSG_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);