JNIEnvironment.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the JNI API for jAssimp */
  35. #if (defined ASSIMP_JNI_EXPORT)
  36. #include "JNIEnvironment.h"
  37. #include "JNILogger.h"
  38. using namespace Assimp;
  39. namespace Assimp {
  40. namespace JNIBridge {
  41. /*static*/ jclass JNIEnvironment::Class_java_lang_String = 0;
  42. /*static*/ jmethodID JNIEnvironment::MID_String_getBytes = 0;
  43. /*static*/ jmethodID JNIEnvironment::MID_String_init = 0;
  44. // ------------------------------------------------------------------------------------------------
  45. bool JNIEnvironment::AttachToCurrentThread (JNIEnv* pcEnv)
  46. {
  47. ai_assert(NULL != pcEnv);
  48. // first initialize some members
  49. if (0 == Class_java_lang_String)
  50. {
  51. if( 0 == (Class_java_lang_String = pcEnv->FindClass("java.lang.String")))
  52. return false;
  53. }
  54. if (0 == MID_String_getBytes)
  55. {
  56. if( 0 == (MID_String_getBytes = pcEnv->GetStaticMethodID(
  57. Class_java_lang_String,"getBytes","()[byte")))
  58. return false;
  59. }
  60. if (0 == MID_String_init)
  61. {
  62. if( 0 == (MID_String_init = pcEnv->GetStaticMethodID(
  63. Class_java_lang_String,"String","([byte)V")))
  64. return false;
  65. }
  66. // now initialize the thread-local storage
  67. if (NULL == this->ptr.get())
  68. {
  69. // attach to the current thread
  70. JavaVM* vm;
  71. pcEnv->GetJavaVM(&vm);
  72. vm->AttachCurrentThread((void **) &pcEnv, NULL);
  73. this->ptr.reset(new JNIThreadData(pcEnv));
  74. }
  75. // increase the reference counter
  76. else this->ptr->m_iNumRef++;
  77. // attach the logger
  78. ((JNILogDispatcher*)DefaultLogger::get())->OnAttachToCurrentThread(this->ptr.get());
  79. return true;
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. bool JNIEnvironment::DetachFromCurrentThread ()
  83. {
  84. ai_assert(NULL != pcEnv);
  85. // detach the logger
  86. ((JNILogDispatcher*)DefaultLogger::get())->OnDetachFromCurrentThread(this->ptr.get());
  87. // decrease the reference counter
  88. if (NULL != this->ptr.get())
  89. {
  90. this->ptr->m_iNumRef--;
  91. if (0 == this->ptr->m_iNumRef)
  92. {
  93. JavaVM* vm;
  94. this->ptr->m_pcEnv->GetJavaVM(&vm);
  95. vm->DetachCurrentThread();
  96. }
  97. }
  98. return true;
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. JNIThreadData* JNIEnvironment::GetThread()
  102. {
  103. ai_assert(NULL != this->ptr.get());
  104. return this->ptr.get();
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. jstring JNU_NewStringNative(JNIEnv *env, const char *str)
  108. {
  109. jstring result;
  110. jbyteArray bytes = 0;
  111. int len;
  112. if (env->EnsureLocalCapacity( 2) < 0)
  113. {
  114. return NULL; /* out of memory error */
  115. }
  116. len = strlen(str);
  117. bytes = env->NewByteArray(len);
  118. if (bytes != NULL)
  119. {
  120. env->SetByteArrayRegion(bytes, 0, len,
  121. (jbyte *)str);
  122. result = (jstring)env->NewObject(JNIEnvironment::Class_java_lang_String,
  123. JNIEnvironment::MID_String_init, bytes);
  124. env->DeleteLocalRef(bytes);
  125. return result;
  126. } /* else fall through */
  127. return NULL;
  128. }
  129. // ------------------------------------------------------------------------------------------------
  130. char *JNU_GetStringNativeChars(JNIEnv *env, jstring jstr)
  131. {
  132. jbyteArray bytes = 0;
  133. jthrowable exc;
  134. char *result = 0;
  135. if (env->EnsureLocalCapacity(2) < 0)
  136. {
  137. return 0; /* out of memory error */
  138. }
  139. bytes = (jbyteArray)env->CallObjectMethod(jstr,JNIEnvironment::MID_String_getBytes);
  140. exc = env->ExceptionOccurred();
  141. if (!exc)
  142. {
  143. jint len = env->GetArrayLength(bytes);
  144. result = (char *)malloc(len + 1);
  145. if (result == 0)
  146. {
  147. env->DeleteLocalRef(bytes);
  148. return 0;
  149. }
  150. env->GetByteArrayRegion(bytes, 0, len,
  151. (jbyte *)result);
  152. result[len] = 0; /* NULL-terminate */
  153. }
  154. else
  155. {
  156. env->DeleteLocalRef(exc);
  157. }
  158. env->DeleteLocalRef(bytes);
  159. return result;
  160. }
  161. };};
  162. #endif // ! JNI only