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);
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 const char *jstorage_get_name(jobject jstorage, JNIEnv * env) {
57 msg_storage_t storage = jstorage_get_native(env, jstorage);
58 return MSG_storage_get_name(storage);
61 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) {
62 msg_storage_t storage;
65 /* get the C string from the java string */
66 if (jname == nullptr) {
67 jxbt_throw_null(env,bprintf("No host can have a null name"));
70 const char *name = env->GetStringUTFChars(jname, 0);
71 storage = MSG_storage_get_by_name(name);
73 if (!storage) { /* invalid name */
74 jxbt_throw_storage_not_found(env, name);
75 env->ReleaseStringUTFChars(jname, name);
78 env->ReleaseStringUTFChars(jname, name);
80 if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) { /* native host not associated yet with java host */
82 /* Instantiate a new java storage */
83 jstorage = jstorage_new_instance(env);
86 jxbt_throw_jni(env, "java storage instantiation failed");
90 /* get a global reference to the newly created storage */
91 jstorage = jstorage_ref(env, jstorage);
94 jxbt_throw_jni(env, "new global ref allocation failed");
97 /* Sets the java storage name */
98 env->SetObjectField(jstorage, jstorage_field_Storage_name, jname);
99 /* bind the java storage and the native storage */
100 jstorage_bind(jstorage, storage, env);
102 /* the native storage data field is set with the global reference to the
103 * java storage returned by this function
105 xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage);
108 /* return the global reference to the java storage instance */
109 return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL);
112 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
113 msg_storage_t storage = jstorage_get_native(env, jstorage);
116 jxbt_throw_notbound(env, "storage", jstorage);
120 return (jlong) MSG_storage_get_size(storage);
123 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
124 msg_storage_t storage = jstorage_get_native(env, jstorage);
127 jxbt_throw_notbound(env, "storage", jstorage);
131 return (jlong) MSG_storage_get_free_size(storage);
134 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
135 msg_storage_t storage = jstorage_get_native(env, jstorage);
138 jxbt_throw_notbound(env, "storage", jstorage);
142 return (jlong) MSG_storage_get_used_size(storage);
145 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
146 msg_storage_t storage = jstorage_get_native(env, jstorage);
149 jxbt_throw_notbound(env, "storage", jstorage);
152 const char *name = env->GetStringUTFChars((jstring) jname, 0);
154 const char *property = MSG_storage_get_property_value(storage, name);
158 jobject jproperty = env->NewStringUTF(property);
160 env->ReleaseStringUTFChars((jstring) jname, name);
165 JNIEXPORT void JNICALL
166 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
167 msg_storage_t storage = jstorage_get_native(env, jstorage);
170 jxbt_throw_notbound(env, "storage", jstorage);
173 const char *name = env->GetStringUTFChars((jstring) jname, 0);
174 const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
175 char *value = xbt_strdup(value_java);
177 MSG_storage_set_property_value(storage, name, value);
179 env->ReleaseStringUTFChars((jstring) jvalue, value_java);
180 env->ReleaseStringUTFChars((jstring) jname, name);
184 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
185 msg_storage_t storage = jstorage_get_native(env, jstorage);
188 jxbt_throw_notbound(env, "storage", jstorage);
191 const char *host_name = MSG_storage_get_host(storage);
195 jobject jhost_name = env->NewStringUTF(host_name);
200 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
206 msg_storage_t storage;
208 xbt_dynar_t table = MSG_storages_as_dynar();
209 int count = xbt_dynar_length(table);
211 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
217 jtable = env->NewObjectArray((jsize) count, cls, nullptr);
220 jxbt_throw_jni(env, "Storages table allocation failed");
224 for (index = 0; index < count; index++) {
225 storage = xbt_dynar_get_as(table,index,msg_storage_t);
226 jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL));
229 jname = env->NewStringUTF(MSG_storage_get_name(storage));
230 jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
233 env->SetObjectArrayElement(jtable, index, jstorage);
235 xbt_dynar_free(&table);