14
14
* limitations under the License.
17
18
#include <iostream>
23
25
#include "JavaVMSingelton.h"
25
namespace zorba { namespace xslfo {
26
#include <zorba/util/path.h>
27
#include <zorba/util/file.h>
28
#include <zorba/zorba.h>
31
namespace zorba { namespace jvm {
26
32
JavaVMSingelton* JavaVMSingelton::instance = NULL;
28
34
JavaVMSingelton::JavaVMSingelton(const char* classPath)
36
std::cout << "JavaVMSingelton::JavaVMSingelton classPath: " << classPath << "\n"; std::cout.flush();
30
38
memset(&args, 0, sizeof(args));
62
70
JavaVMSingelton::~JavaVMSingelton()
67
m_vm->DestroyJavaVM();
69
delete[] classPathOption;
76
//m_vm->DestroyJavaVM();
80
delete[] classPathOption;
72
83
JavaVMSingelton* JavaVMSingelton::getInstance(const char* classPath)
74
if (instance == NULL) {
75
instance = new JavaVMSingelton(classPath);
86
// // If pointer to instance of JavaVMSingelton exists (true) then return instance pointer else look for
87
// // instance pointer in memory mapped pointer. If the instance pointer does not exist in
88
// // memory mapped pointer, return a newly created pointer to an instance of Abc.
91
// instance : (instance = (JavaVMSingelton*) MemoryMappedPointers::getPointer("JavaVMSingelton")) ?
92
// instance : (instance = (JavaVMSingelton*) MemoryMappedPointers::createEntry("JavaVMSingelton",(void*)new JavaVMSingelton(classPath)));
96
// If pointer to instance of JavaVMSingelton exists (true) then return instance pointer
97
// else return a newly created pointer to an instance of JavaVMSingelton.
102
if ( JNI_GetCreatedJavaVMs(&jvms, 1, &nVMs)==0 )
104
//std::cout << "Got JVMs " << nVMs << "\n"; std::cout.flush();
109
if( jvm->AttachCurrentThread((void **)&env, NULL) ==0 )
111
// if there is a jvm opened already by a diffrent dynamic lib
112
// make a singleton for this lib with that jvm
113
instance = new JavaVMSingelton(jvm, env);
118
if (instance == NULL)
120
instance = new JavaVMSingelton(classPath);
80
void JavaVMSingelton::destroyInstance() {
127
JavaVMSingelton* JavaVMSingelton::getInstance(const zorba::StaticContext* aStaticContext)
129
if (instance == NULL)
131
String cp = computeClassPath(aStaticContext);
132
return getInstance(cp.c_str());
84
138
JavaVM* JavaVMSingelton::getVM()
94
}} // namespace zorba, xslfo
149
String JavaVMSingelton::computeClassPath(const zorba::StaticContext* aStaticContext)
153
// get classpath from global Properties
154
PropertiesGlobal * properties = Zorba::getInstance(NULL)->getProperties();
155
std::string globalClassPath;
156
properties->getJVMClassPath(globalClassPath);
157
cp += globalClassPath;
159
std::vector<String> lCPV;
160
aStaticContext->getFullLibPath(lCPV);
162
String pathSeparator(filesystem_path::get_path_separator());
163
String dirSeparator(filesystem_path::get_directory_separator());
165
for (std::vector<String>::iterator lIter = lCPV.begin();
166
lIter != lCPV.end(); ++lIter)
168
// verify it contains a jars dir
169
const filesystem_path baseFsPath((*lIter).str());
170
const filesystem_path jarsFsPath(std::string("jars"));
171
filesystem_path jarsDirPath(baseFsPath, jarsFsPath);
173
file jarsDir(jarsDirPath);
175
if ( jarsDir.exists() && jarsDir.is_directory())
177
std::vector<std::string> list;
180
for (std::vector<std::string>::iterator itemIter = list.begin();
181
itemIter != list.end(); ++itemIter)
183
filesystem_path itemLocalFS(*itemIter);
184
filesystem_path itemFS(jarsDirPath, itemLocalFS);
185
file itemFile(itemFS);
186
if ( itemFile.exists() && itemFile.is_file() )
188
std::string itemName = itemFile.get_path();
189
std::string suffix = "-classpath.txt";
191
found = itemName.rfind(suffix);
192
if (found!=std::string::npos &&
193
found + suffix.length() == itemName.length() )
195
std::auto_ptr<std::istream> pathFile;
196
pathFile.reset(new std::ifstream (itemName.c_str ()));
197
if (!pathFile->good() || pathFile->eof() )
199
std::cerr << "file {" << itemName << "} not found or not readable." << std::endl;
205
while( !pathFile->eof() && !pathFile->bad() && !pathFile->fail())
207
pathFile->getline(line, sizeof(line));
208
std::string lineStr(line);
210
if ( lineStr.size() == 0 )
213
//std::cout << "line: '" << lineStr << "'" << std::endl; std::cout.flush();
215
const std::string normalizedPath =
216
filesystem_path::normalize_path( lineStr, jarsDirPath.get_path());
218
cp += pathSeparator + normalizedPath;
226
properties->setJVMClassPath(cp.str());
228
//std::cout << "JavaVMSingelton::computeClassPath: '" << cp << "'" << std::endl; std::cout.flush();
232
}} // namespace zorba, jvm