-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaProxy.cpp
259 lines (226 loc) · 7.98 KB
/
JavaProxy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2013-2014 Josh Blum
// 2019 Nicholas Corgan
// SPDX-License-Identifier: BSL-1.0
#include "JavaProxy.hpp"
#include <Pothos/Callable.hpp>
#include <Pothos/Plugin.hpp>
#include <Pothos/System/Paths.hpp>
#include <iostream>
#include <Poco/Format.h>
#include <Poco/RecursiveDirectoryIterator.h>
#include <Poco/Format.h>
#include <Poco/Path.h>
#include <Poco/SingletonHolder.h>
#include <Poco/String.h>
#include <cstdint>
#include <mutex>
/***********************************************************************
* JVM per process manager -- we only get 1 JVM per process
**********************************************************************/
//! wrapper knows how to delete jvm
struct MyJvmWrapper
{
MyJvmWrapper(void):
jvm(nullptr)
{
return;
}
~MyJvmWrapper(void)
{
//This is hanging on windows and apparently never really works.
//Since a JVM is once per process, it doesnt really hurt to not clean it up.
//if (jvm != nullptr) jvm->DestroyJavaVM();
}
JavaVM *jvm;
};
static std::mutex &getJvmMutex(void)
{
static Poco::SingletonHolder<std::mutex> sh;
return *sh.get();
}
static MyJvmWrapper &getJvmWrapper(void)
{
static Poco::SingletonHolder<MyJvmWrapper> sh;
return *sh.get();
}
/***********************************************************************
* JavaProxyEnvironment - create JVM and JNIEnv
**********************************************************************/
static Poco::Path getModulesDir()
{
return Poco::Path(Pothos::System::getPothosDevLibraryPath())
.append("Pothos")
.append("modules" + Pothos::System::getAbiVersion())
.absolute();
}
static std::vector<std::string> getAllJARFiles()
{
Poco::Path dirPath(getModulesDir());
std::vector<std::string> jarFiles;
Poco::SimpleRecursiveDirectoryIterator end;
for(Poco::SimpleRecursiveDirectoryIterator dirIter(dirPath);
dirIter != end;
++dirIter)
{
Poco::Path path(dirIter->path());
if(path.getExtension() == "jar")
{
jarFiles.emplace_back(path.toString());
}
}
return jarFiles;
}
static std::string getJavaClassPathParameter()
{
std::string pathSeparatorString;
pathSeparatorString.push_back(Poco::Path::pathSeparator());
auto jarFiles = getAllJARFiles();
auto appendedJARFiles = Poco::cat(
pathSeparatorString,
jarFiles.begin(),
jarFiles.end());
std::string ret;
if(!appendedJARFiles.empty())
{
ret = "-Djava.class.path=" + appendedJARFiles;
}
return ret;
}
JavaProxyEnvironment::JavaProxyEnvironment(const Pothos::ProxyEnvironmentArgs &args)
{
std::lock_guard<std::mutex> lock(getJvmMutex());
MyJvmWrapper &wrapper = getJvmWrapper();
if (wrapper.jvm != nullptr)
{
jint ret = wrapper.jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
if (ret < 0) throw Pothos::ProxyEnvironmentFactoryError("JavaProxyEnvironment::GetEnv()");
}
else
{
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
std::vector<std::string> optionsManaged;
for (const auto &entry : args)
{
optionsManaged.push_back("-D" + entry.first + "=" + entry.second);
}
optionsManaged.push_back("-verbose:jni");
std::string classPathParam = getJavaClassPathParameter();
if(!classPathParam.empty())
{
optionsManaged.emplace_back(std::move(classPathParam));
}
JavaVMOption* options = new JavaVMOption[optionsManaged.size()];
for (size_t i = 0; i < optionsManaged.size(); i++)
{
options[i].optionString = (char *)optionsManaged[i].c_str();
}
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = args.size();
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
jint ret = JNI_CreateJavaVM(&wrapper.jvm, (void**)&env, &vm_args);
delete options;
if (ret < 0) throw Pothos::ProxyEnvironmentFactoryError("JavaProxyEnvironment::JNI_CreateJavaVM()");
}
this->jvm = wrapper.jvm;
}
Pothos::Proxy JavaProxyEnvironment::makeHandle(jvalue value, char sig)
{
auto env = std::dynamic_pointer_cast<JavaProxyEnvironment>(this->shared_from_this());
return Pothos::Proxy(new JavaProxyHandle(env, value, sig));
}
std::shared_ptr<JavaProxyHandle> JavaProxyEnvironment::getHandle(const Pothos::Proxy &proxy)
{
Pothos::Proxy myProxy = proxy;
if (proxy.getEnvironment() != this->shared_from_this())
{
auto local = proxy.getEnvironment()->convertProxyToObject(proxy);
myProxy = this->convertObjectToProxy(local);
}
return std::dynamic_pointer_cast<JavaProxyHandle>(myProxy.getHandle());
}
Pothos::Proxy JavaProxyEnvironment::findProxy(const std::string &name)
{
jvalue value;
value.l = this->forName(name.c_str());
if (value.l == nullptr)
{
throw Pothos::ProxyEnvironmentFindError("JavaProxyEnvironment::findProxy("+name+")");
}
return this->makeHandle(value, 'L');
}
void JavaProxyEnvironment::serialize(const Pothos::Proxy &proxy, std::ostream &os)
{
try
{
auto b = this->findProxy("java.io.ByteArrayOutputStream").call("new");
auto o = this->findProxy("java.io.ObjectOutputStream").call("new", b);
o.call("writeObject", proxy);
const auto bytes = b.call<std::vector<int8_t>>("toByteArray");
os.write((const char *)bytes.data(), bytes.size());
}
catch (const Pothos::Exception &ex)
{
throw Pothos::ProxySerializeError("JavaProxyEnvironment::serialize()", ex);
}
}
Pothos::Proxy JavaProxyEnvironment::deserialize(std::istream &is)
{
is.seekg (0, std::ios_base::end);
const auto length = is.tellg();
is.seekg (0, std::ios_base::beg);
std::vector<Poco::Int8> bytes(length);
is.read((char *)bytes.data(), bytes.size());
try
{
auto b = this->findProxy("java.io.ByteArrayInputStream").call("new", bytes);
auto o = this->findProxy("java.io.ObjectInputStream").call("new", b);
return o.call("readObject");
}
catch (const Pothos::Exception &ex)
{
throw Pothos::ProxySerializeError("JavaProxyEnvironment::deserialize()", ex);
}
}
Pothos::Object JavaProxyEnvironment::convertProxyToObject(const Pothos::Proxy &proxy_)
{
// First, try the parent method.
try
{
return Pothos::ProxyEnvironment::convertProxyToObject(proxy_);
}
catch (const Pothos::ProxyEnvironmentConvertError&) {}
Pothos::Proxy proxy = proxy_;
auto javaHandle = std::dynamic_pointer_cast<JavaProxyHandle>(proxy.getHandle());
while(javaHandle->hasSuperclass())
{
proxy = javaHandle->getProxyWithSuperclassName();
javaHandle = std::dynamic_pointer_cast<JavaProxyHandle>(proxy.getHandle());
try
{
return Pothos::ProxyEnvironment::convertProxyToObject(proxy);
}
catch(const Pothos::ProxyEnvironmentConvertError&) {}
}
// At this point, we've tried everything up to java.lang.Object, so if
// there's no conversion so far, there's no conversion at all.
throw Pothos::ProxyEnvironmentConvertError(
"JavaProxyEnvironment::convertProxyToObject()",
Poco::format("cannot convert %s or any superclasses to Pothos::Object",
proxy_.getClassName()));
}
/***********************************************************************
* factory registration
**********************************************************************/
Pothos::ProxyEnvironment::Sptr makeJavaProxyEnvironment(const Pothos::ProxyEnvironmentArgs &args)
{
return Pothos::ProxyEnvironment::Sptr(new JavaProxyEnvironment(args));
}
pothos_static_block(pothosRegisterJavaProxy)
{
Pothos::PluginRegistry::addCall(
"/proxy/environment/java",
&makeJavaProxyEnvironment);
}