1 /* Functions related to the java storage API. */
3 /* Copyright (c) 2012-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <surf/surf_routing.h>
12 #include "simgrid/msg.h"
14 #include "jmsg_storage.h"
15 #include "jxbt_utilities.h"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
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 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 if (!class_Storage || !jstorage_field_Storage_name || !jstorage_method_Storage_constructor || !jstorage_field_Storage_bind) {
38 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this 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 const char *jstorage_get_name(jobject jstorage, JNIEnv * env) {
55 msg_storage_t storage = jstorage_get_native(env, jstorage);
56 return MSG_storage_get_name(storage);
59 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) {
60 msg_storage_t storage;
63 /* get the C string from the java string */
64 if (jname == nullptr) {
65 jxbt_throw_null(env,bprintf("No host can have a null name"));
68 const char *name = env->GetStringUTFChars(jname, 0);
69 storage = MSG_storage_get_by_name(name);
71 if (!storage) { /* invalid name */
72 jxbt_throw_storage_not_found(env, name);
73 env->ReleaseStringUTFChars(jname, name);
76 env->ReleaseStringUTFChars(jname, name);
78 if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) { /* native host not associated yet with java host */
80 /* Instantiate a new java storage */
81 jstorage = jstorage_new_instance(env);
84 jxbt_throw_jni(env, "java storage instantiation failed");
88 /* get a global reference to the newly created storage */
89 jstorage = jstorage_ref(env, jstorage);
92 jxbt_throw_jni(env, "new global ref allocation failed");
95 /* Sets the java storage name */
96 env->SetObjectField(jstorage, jstorage_field_Storage_name, jname);
97 /* bind the java storage and the native storage */
98 jstorage_bind(jstorage, storage, env);
100 /* the native storage data field is set with the global reference to the
101 * java storage returned by this function
103 xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage);
106 /* return the global reference to the java storage instance */
107 return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL);
110 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
111 msg_storage_t storage = jstorage_get_native(env, jstorage);
114 jxbt_throw_notbound(env, "storage", jstorage);
118 return (jlong) MSG_storage_get_size(storage);
121 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
122 msg_storage_t storage = jstorage_get_native(env, jstorage);
125 jxbt_throw_notbound(env, "storage", jstorage);
129 return (jlong) MSG_storage_get_free_size(storage);
132 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
133 msg_storage_t storage = jstorage_get_native(env, jstorage);
136 jxbt_throw_notbound(env, "storage", jstorage);
140 return (jlong) MSG_storage_get_used_size(storage);
143 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
144 msg_storage_t storage = jstorage_get_native(env, jstorage);
147 jxbt_throw_notbound(env, "storage", jstorage);
150 const char *name = env->GetStringUTFChars((jstring) jname, 0);
152 const char *property = MSG_storage_get_property_value(storage, name);
156 jobject jproperty = env->NewStringUTF(property);
158 env->ReleaseStringUTFChars((jstring) jname, name);
163 JNIEXPORT void JNICALL
164 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
165 msg_storage_t storage = jstorage_get_native(env, jstorage);
168 jxbt_throw_notbound(env, "storage", jstorage);
171 const char *name = env->GetStringUTFChars((jstring) jname, 0);
172 const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
173 char *value = xbt_strdup(value_java);
175 MSG_storage_set_property_value(storage, name, value, xbt_free_f);
177 env->ReleaseStringUTFChars((jstring) jvalue, value_java);
178 env->ReleaseStringUTFChars((jstring) jname, name);
182 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
183 msg_storage_t storage = jstorage_get_native(env, jstorage);
186 jxbt_throw_notbound(env, "storage", jstorage);
189 const char *host_name = MSG_storage_get_host(storage);
193 jobject jhost_name = env->NewStringUTF(host_name);
198 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
204 msg_storage_t storage;
206 xbt_dynar_t table = MSG_storages_as_dynar();
207 int count = xbt_dynar_length(table);
209 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
215 jtable = env->NewObjectArray((jsize) count, cls, nullptr);
218 jxbt_throw_jni(env, "Storages table allocation failed");
222 for (index = 0; index < count; index++) {
223 storage = xbt_dynar_get_as(table,index,msg_storage_t);
224 jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL));
227 jname = env->NewStringUTF(MSG_storage_get_name(storage));
228 jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
231 env->SetObjectArrayElement(jtable, index, jstorage);
233 xbt_dynar_free(&table);