ZT1_jnicache.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "ZT1_jnicache.h"
  28. #include "ZT1_jniutils.h"
  29. JniCache::JniCache()
  30. : m_jvm(NULL)
  31. , m_classes()
  32. , m_fields()
  33. , m_staticFields()
  34. , m_methods()
  35. , m_staticMethods()
  36. {
  37. LOGD("JNI Cache Created");
  38. }
  39. JniCache::JniCache(JavaVM *jvm)
  40. : m_jvm(jvm)
  41. , m_classes()
  42. , m_fields()
  43. , m_staticFields()
  44. , m_methods()
  45. , m_staticMethods()
  46. {
  47. LOGD("JNI Cache Created");
  48. }
  49. JniCache::~JniCache()
  50. {
  51. LOGD("JNI Cache Destroyed");
  52. clearCache();
  53. }
  54. void JniCache::clearCache()
  55. {
  56. if(m_jvm)
  57. {
  58. JNIEnv *env = NULL;
  59. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  60. return;
  61. for(ClassMap::iterator iter = m_classes.begin(), end = m_classes.end();
  62. iter != end; ++iter)
  63. {
  64. env->DeleteGlobalRef(iter->second);
  65. }
  66. }
  67. m_classes.clear();
  68. m_fields.clear();
  69. m_staticFields.clear();
  70. m_methods.clear();
  71. m_staticMethods.clear();
  72. }
  73. void JniCache::setJavaVM(JavaVM *jvm)
  74. {
  75. LOGD("Assigned JVM to object");
  76. m_jvm = jvm;
  77. }
  78. jclass JniCache::findClass(const std::string &name)
  79. {
  80. if(!m_jvm)
  81. return NULL;
  82. ClassMap::iterator found = m_classes.find(name);
  83. if(found == m_classes.end())
  84. {
  85. // get the class from the JVM
  86. JNIEnv *env = NULL;
  87. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  88. {
  89. LOGE("Error retreiving JNI Environment");
  90. return NULL;
  91. }
  92. jclass localCls = env->FindClass(name.c_str());
  93. if(env->ExceptionCheck())
  94. {
  95. LOGE("Error finding class: %s", name.c_str());
  96. return NULL;
  97. }
  98. jclass cls = (jclass)env->NewGlobalRef(localCls);
  99. m_classes.insert(std::make_pair(name, cls));
  100. return cls;
  101. }
  102. LOGD("Returning cached %s", name.c_str());
  103. return found->second;
  104. }
  105. jmethodID JniCache::findMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
  106. {
  107. if(!m_jvm)
  108. return NULL;
  109. std::string id = methodName + methodSig;
  110. MethodMap::iterator found = m_methods.find(id);
  111. if(found == m_methods.end())
  112. {
  113. JNIEnv *env = NULL;
  114. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  115. {
  116. return NULL;
  117. }
  118. jmethodID mid = env->GetMethodID(cls, methodName.c_str(), methodSig.c_str());
  119. if(env->ExceptionCheck())
  120. {
  121. return NULL;
  122. }
  123. m_methods.insert(std::make_pair(id, mid));
  124. return mid;
  125. }
  126. return found->second;
  127. }
  128. jmethodID JniCache::findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
  129. {
  130. if(!m_jvm)
  131. return NULL;
  132. std::string id = methodName + methodSig;
  133. MethodMap::iterator found = m_staticMethods.find(id);
  134. if(found == m_staticMethods.end())
  135. {
  136. JNIEnv *env = NULL;
  137. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  138. {
  139. return NULL;
  140. }
  141. jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), methodSig.c_str());
  142. if(env->ExceptionCheck())
  143. {
  144. return NULL;
  145. }
  146. m_staticMethods.insert(std::make_pair(id, mid));
  147. return mid;
  148. }
  149. return found->second;
  150. }
  151. jfieldID JniCache::findField(jclass cls, const std::string &fieldName, const std::string &typeStr)
  152. {
  153. if(!m_jvm)
  154. return NULL;
  155. std::string id = fieldName + typeStr;
  156. FieldMap::iterator found = m_fields.find(id);
  157. if(found == m_fields.end())
  158. {
  159. JNIEnv *env = NULL;
  160. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  161. {
  162. return NULL;
  163. }
  164. jfieldID fid = env->GetFieldID(cls, fieldName.c_str(), typeStr.c_str());
  165. if(env->ExceptionCheck())
  166. {
  167. return NULL;
  168. }
  169. m_fields.insert(std::make_pair(id, fid));
  170. return fid;
  171. }
  172. return found->second;
  173. }
  174. jfieldID JniCache::findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr)
  175. {
  176. if(!m_jvm)
  177. return NULL;
  178. std::string id = fieldName + typeStr;
  179. FieldMap::iterator found = m_staticFields.find(id);
  180. if(found == m_staticFields.end())
  181. {
  182. JNIEnv *env = NULL;
  183. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  184. {
  185. return NULL;
  186. }
  187. jfieldID fid = env->GetStaticFieldID(cls, fieldName.c_str(), typeStr.c_str());
  188. if(env->ExceptionCheck())
  189. {
  190. return NULL;
  191. }
  192. m_staticFields.insert(std::make_pair(id, fid));
  193. return fid;
  194. }
  195. return found->second;
  196. }