1 /* Functions related to the java storage API. */
3 /* Copyright (c) 2012-2014. 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 "simgrid/msg.h"
12 #include "jmsg_storage.h"
13 #include "jxbt_utilities.h"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
17 static jmethodID jstorage_method_Storage_constructor;
18 static jfieldID jstorage_field_Storage_bind;
19 static jfieldID jstorage_field_Storage_name;
21 jobject jstorage_new_instance(JNIEnv * env) {
22 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
23 return (*env)->NewObject(env, cls, jstorage_method_Storage_constructor);
26 msg_storage_t jstorage_get_native(JNIEnv * env, jobject jstorage) {
27 return (msg_storage_t) (long) (*env)->GetLongField(env, jstorage, jstorage_field_Storage_bind);
30 JNIEXPORT void JNICALL
31 Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) {
32 jclass class_Storage = (*env)->FindClass(env, "org/simgrid/msg/Storage");
33 jstorage_method_Storage_constructor = (*env)->GetMethodID(env, class_Storage, "<init>", "()V");
34 jstorage_field_Storage_bind = jxbt_get_jfield(env,class_Storage, "bind", "J");
35 jstorage_field_Storage_name = jxbt_get_jfield(env, class_Storage, "name", "Ljava/lang/String;");
36 if (!class_Storage || !jstorage_field_Storage_name || !jstorage_method_Storage_constructor || !jstorage_field_Storage_bind) {
37 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
41 void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) {
42 (*env)->SetLongField(env, jstorage, jstorage_field_Storage_bind, (jlong) (long) (storage));
45 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
46 return (*env)->NewGlobalRef(env, jstorage);
49 void jstorage_unref(JNIEnv * env, jobject jstorage) {
50 (*env)->DeleteGlobalRef(env, jstorage);
53 const char *jstorage_get_name(jobject jstorage, JNIEnv * env) {
54 msg_storage_t storage = jstorage_get_native(env, jstorage);
55 return MSG_storage_get_name(storage);
58 JNIEXPORT jobject JNICALL
59 Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls,
61 msg_storage_t storage;
64 /* get the C string from the java string */
66 jxbt_throw_null(env,bprintf("No host can have a null name"));
69 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
70 storage = MSG_storage_get_by_name(name);
72 if (!storage) { /* invalid name */
73 jxbt_throw_storage_not_found(env, name);
74 (*env)->ReleaseStringUTFChars(env, jname, name);
77 (*env)->ReleaseStringUTFChars(env, jname, name);
79 if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) { /* native host not associated yet with java host */
81 /* Instantiate a new java storage */
82 jstorage = jstorage_new_instance(env);
85 jxbt_throw_jni(env, "java storage instantiation failed");
89 /* get a global reference to the newly created storage */
90 jstorage = jstorage_ref(env, jstorage);
93 jxbt_throw_jni(env, "new global ref allocation failed");
96 /* Sets the java storage name */
97 (*env)->SetObjectField(env, jstorage, jstorage_field_Storage_name, jname);
98 /* bind the java storage and the native storage */
99 jstorage_bind(jstorage, storage, env);
101 /* the native storage data field is set with the global reference to the
102 * java storage returned by this function
104 xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage);
107 /* return the global reference to the java storage instance */
108 return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL);
112 JNIEXPORT jlong JNICALL
113 Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
114 msg_storage_t storage = jstorage_get_native(env, jstorage);
117 jxbt_throw_notbound(env, "storage", jstorage);
121 return (jlong) MSG_storage_get_size(storage);
124 JNIEXPORT jlong JNICALL
125 Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
126 msg_storage_t storage = jstorage_get_native(env, jstorage);
129 jxbt_throw_notbound(env, "storage", jstorage);
133 return (jlong) MSG_storage_get_free_size(storage);
136 JNIEXPORT jlong JNICALL
137 Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
138 msg_storage_t storage = jstorage_get_native(env, jstorage);
141 jxbt_throw_notbound(env, "storage", jstorage);
145 return (jlong) MSG_storage_get_used_size(storage);
148 JNIEXPORT jobject JNICALL
149 Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
150 msg_storage_t storage = jstorage_get_native(env, jstorage);
153 jxbt_throw_notbound(env, "storage", jstorage);
156 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
158 const char *property = MSG_storage_get_property_value(storage, name);
162 jobject jproperty = (*env)->NewStringUTF(env, property);
164 (*env)->ReleaseStringUTFChars(env, jname, name);
169 JNIEXPORT void JNICALL
170 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
171 msg_storage_t storage = jstorage_get_native(env, jstorage);
174 jxbt_throw_notbound(env, "storage", jstorage);
177 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
178 const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0);
179 char *value = xbt_strdup(value_java);
181 MSG_storage_set_property_value(storage, name, value, xbt_free_f);
183 (*env)->ReleaseStringUTFChars(env, jvalue, value);
184 (*env)->ReleaseStringUTFChars(env, jname, name);
188 JNIEXPORT jobject JNICALL
189 Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
190 msg_storage_t storage = jstorage_get_native(env, jstorage);
193 jxbt_throw_notbound(env, "storage", jstorage);
196 const char *host_name = MSG_storage_get_host(storage);
200 jobject jhost_name = (*env)->NewStringUTF(env, host_name);
205 JNIEXPORT jobjectArray JNICALL
206 Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
212 msg_storage_t storage;
214 xbt_dynar_t table = MSG_storages_as_dynar();
215 int count = xbt_dynar_length(table);
217 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
223 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
226 jxbt_throw_jni(env, "Storages table allocation failed");
230 for (index = 0; index < count; index++) {
231 storage = xbt_dynar_get_as(table,index,msg_storage_t);
232 jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL));
235 jname = (*env)->NewStringUTF(env, MSG_storage_get_name(storage));
236 jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
239 (*env)->SetObjectArrayElement(env, jtable, index, jstorage);
241 xbt_dynar_free(&table);