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
33 Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) {
34 jclass class_Storage = env->FindClass("org/simgrid/msg/Storage");
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 if (!class_Storage || !jstorage_field_Storage_name || !jstorage_method_Storage_constructor || !jstorage_field_Storage_bind) {
39 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
43 void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) {
44 env->SetLongField(jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage));
47 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
48 return env->NewGlobalRef(jstorage);
51 void jstorage_unref(JNIEnv * env, jobject jstorage) {
52 env->DeleteGlobalRef(jstorage);
55 const char *jstorage_get_name(jobject jstorage, JNIEnv * env) {
56 msg_storage_t storage = jstorage_get_native(env, jstorage);
57 return MSG_storage_get_name(storage);
60 JNIEXPORT jobject JNICALL
61 Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls,
63 msg_storage_t storage;
66 /* get the C string from the java string */
68 jxbt_throw_null(env,bprintf("No host can have a null name"));
71 const char *name = env->GetStringUTFChars(jname, 0);
72 storage = MSG_storage_get_by_name(name);
74 if (!storage) { /* invalid name */
75 jxbt_throw_storage_not_found(env, name);
76 env->ReleaseStringUTFChars(jname, name);
79 env->ReleaseStringUTFChars(jname, name);
81 if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) { /* native host not associated yet with java host */
83 /* Instantiate a new java storage */
84 jstorage = jstorage_new_instance(env);
87 jxbt_throw_jni(env, "java storage instantiation failed");
91 /* get a global reference to the newly created storage */
92 jstorage = jstorage_ref(env, jstorage);
95 jxbt_throw_jni(env, "new global ref allocation failed");
98 /* Sets the java storage name */
99 env->SetObjectField(jstorage, jstorage_field_Storage_name, jname);
100 /* bind the java storage and the native storage */
101 jstorage_bind(jstorage, storage, env);
103 /* the native storage data field is set with the global reference to the
104 * java storage returned by this function
106 xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage);
109 /* return the global reference to the java storage instance */
110 return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL);
114 JNIEXPORT jlong JNICALL
115 Java_org_simgrid_msg_Storage_getSize(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_size(storage);
126 JNIEXPORT jlong JNICALL
127 Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
128 msg_storage_t storage = jstorage_get_native(env, jstorage);
131 jxbt_throw_notbound(env, "storage", jstorage);
135 return (jlong) MSG_storage_get_free_size(storage);
138 JNIEXPORT jlong JNICALL
139 Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
140 msg_storage_t storage = jstorage_get_native(env, jstorage);
143 jxbt_throw_notbound(env, "storage", jstorage);
147 return (jlong) MSG_storage_get_used_size(storage);
150 JNIEXPORT jobject JNICALL
151 Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
152 msg_storage_t storage = jstorage_get_native(env, jstorage);
155 jxbt_throw_notbound(env, "storage", jstorage);
158 const char *name = env->GetStringUTFChars((jstring) jname, 0);
160 const char *property = MSG_storage_get_property_value(storage, name);
164 jobject jproperty = env->NewStringUTF(property);
166 env->ReleaseStringUTFChars((jstring) jname, name);
171 JNIEXPORT void JNICALL
172 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
173 msg_storage_t storage = jstorage_get_native(env, jstorage);
176 jxbt_throw_notbound(env, "storage", jstorage);
179 const char *name = env->GetStringUTFChars((jstring) jname, 0);
180 const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
181 char *value = xbt_strdup(value_java);
183 MSG_storage_set_property_value(storage, name, value, xbt_free_f);
185 env->ReleaseStringUTFChars((jstring) jvalue, value_java);
186 env->ReleaseStringUTFChars((jstring) jname, name);
190 JNIEXPORT jobject JNICALL
191 Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
192 msg_storage_t storage = jstorage_get_native(env, jstorage);
195 jxbt_throw_notbound(env, "storage", jstorage);
198 const char *host_name = MSG_storage_get_host(storage);
202 jobject jhost_name = env->NewStringUTF(host_name);
207 JNIEXPORT jobjectArray JNICALL
208 Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
214 msg_storage_t storage;
216 xbt_dynar_t table = MSG_storages_as_dynar();
217 int count = xbt_dynar_length(table);
219 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
225 jtable = env->NewObjectArray((jsize) count, cls, NULL);
228 jxbt_throw_jni(env, "Storages table allocation failed");
232 for (index = 0; index < count; index++) {
233 storage = xbt_dynar_get_as(table,index,msg_storage_t);
234 jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL));
237 jname = env->NewStringUTF(MSG_storage_get_name(storage));
238 jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
241 env->SetObjectArrayElement(jtable, index, jstorage);
243 xbt_dynar_free(&table);