GridGain C++
utils.h
1 /*
2  * Copyright 2019 GridGain Systems, Inc. and Contributors.
3  *
4  * Licensed under the GridGain Community Edition License (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef _IGNITE_JNI_UTILS
17 #define _IGNITE_JNI_UTILS
18 
19 #include <string>
20 
21 #include <ignite/common/common.h>
22 #include <ignite/common/concurrent.h>
23 #include <ignite/jni/java.h>
24 
25 namespace ignite
26 {
27  namespace jni
28  {
32  class AttachHelper
33  {
34  public:
38  ~AttachHelper();
39 
43  static void OnThreadAttach();
44  };
45 
49  class IGNITE_IMPORT_EXPORT JavaGlobalRef
50  {
51  public:
56  obj(NULL)
57  {
58  // No-op.
59  }
60 
67  JavaGlobalRef(common::concurrent::SharedPointer<java::JniContext>& ctx, jobject obj)
68  : obj(NULL)
69  {
70  Init(ctx, obj);
71  }
72 
79  : obj(NULL)
80  {
81  Init(other.ctx, other.obj);
82  }
83 
91  {
92  if (this != &other)
93  {
94  java::JniContext::Release(obj);
95 
96  Init(other.ctx, other.obj);
97  }
98 
99  return *this;
100  }
101 
106  {
107  java::JniContext::Release(obj);
108  }
109 
115  jobject Get()
116  {
117  return obj;
118  }
119 
120  private:
122  void Init(const common::concurrent::SharedPointer<java::JniContext>& ctx0, jobject obj0) {
123  ctx = ctx0;
124 
125  if (ctx.IsValid())
126  this->obj = ctx.Get()->Acquire(obj0);
127  }
128 
130  common::concurrent::SharedPointer<java::JniContext> ctx;
131 
133  jobject obj;
134  };
135 
144  IGNITE_IMPORT_EXPORT std::string FindJvmLibrary(const std::string& path);
145 
152  IGNITE_IMPORT_EXPORT bool LoadJvmLibrary(const std::string& path);
153 
161  IGNITE_IMPORT_EXPORT std::string CreateIgniteHomeClasspath(const std::string& home, bool forceTest);
162 
170  IGNITE_IMPORT_EXPORT std::string CreateIgniteClasspath(const std::string& usrCp, const std::string& home);
171 
185  IGNITE_IMPORT_EXPORT std::string ResolveIgniteHome(const std::string& path = "");
186  }
187 }
188 
189 #endif //_IGNITE_JNI_UTILS
ignite::jni::AttachHelper
Helper class to manage attached threads.
Definition: utils.h:32
ignite
Ignite API.
Definition: cache.h:47
ignite::jni::JavaGlobalRef::JavaGlobalRef
JavaGlobalRef(common::concurrent::SharedPointer< java::JniContext > &ctx, jobject obj)
Constructor.
Definition: utils.h:67
ignite::jni::JavaGlobalRef::operator=
JavaGlobalRef & operator=(const JavaGlobalRef &other)
Assignment operator.
Definition: utils.h:90
ignite::jni::JavaGlobalRef::JavaGlobalRef
JavaGlobalRef(const JavaGlobalRef &other)
Copy constructor.
Definition: utils.h:78
ignite::jni::AttachHelper::OnThreadAttach
static void OnThreadAttach()
Callback invoked on successful thread attach ot JVM.
Definition: core/src/jni/os/linux/utils.cpp:68
ignite::jni::JavaGlobalRef
Represents global reference to Java object.
Definition: utils.h:49
ignite::jni::AttachHelper::~AttachHelper
~AttachHelper()
Destructor.
Definition: core/src/jni/os/linux/utils.cpp:63
ignite::jni::JavaGlobalRef::JavaGlobalRef
JavaGlobalRef()
Default constructor.
Definition: utils.h:55
ignite::jni::JavaGlobalRef::~JavaGlobalRef
~JavaGlobalRef()
Destructor.
Definition: utils.h:105
ignite::jni::JavaGlobalRef::Get
jobject Get()
Get object.
Definition: utils.h:115