Jsmooth patch to allow -X jvm options

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@869 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-12-07 09:25:07 +00:00
parent 1b67a58656
commit 618cc4a3ea
2 changed files with 643 additions and 0 deletions

View File

@ -0,0 +1,422 @@
/*
JSmooth: a VM wrapper toolkit for Windows
Copyright (C) 2003-2007 Rodrigo Reyes <reyes@charabia.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "SunJVMDLL.h"
#include "JClassProxy.h"
#include "JniSmooth.h"
SunJVMDLL::SunJVMDLL(const std::string& jvmdll, const Version& v)
{
m_dllpath = jvmdll;
m_version = v;
m_statusCode = SunJVMDLL::JVM_NOT_STARTED;
m_vmlib = NULL;
}
SunJVMDLL::~SunJVMDLL()
{
if (m_vmlib != NULL)
{
FreeLibrary(m_vmlib);
}
}
bool SunJVMDLL::run(const std::string& mainclass, bool waitDeath)
{
if (m_statusCode == SunJVMDLL::JVM_NOT_STARTED)
instanciate();
if (m_statusCode == SunJVMDLL::JVM_LOADED)
{
JClassProxy disp(this, mainclass);
jstring emptystr = newUTFString(std::string(""));
jobjectArray mainargs = newObjectArray(m_arguments.size(), "java.lang.String", emptystr);
for (int i =0; i<m_arguments.size(); i++)
{
env()->SetObjectArrayElement(mainargs, i, newUTFString(m_arguments[i]));
}
printf("arguments array = %d\n", mainargs);
jvalue ma[1];
ma[0].l = mainargs;
disp.invokeStatic(std::string("void main(java.lang.String[] args)"), ma);
if (waitDeath == true)
m_javavm->DestroyJavaVM();
return true;
}
return false;
}
void SunJVMDLL::join()
{
if (m_statusCode == SunJVMDLL::JVM_LOADED)
{
m_javavm->DestroyJavaVM();
}
}
bool SunJVMDLL::instanciate()
{
m_vmlib = LoadLibrary(m_dllpath.c_str());
if (m_vmlib == NULL)
{
m_statusCode = SunJVMDLL::JVM_DLL_CANT_LOAD;
return false;
}
CreateJavaVM_t CreateJavaVM = (CreateJavaVM_t)GetProcAddress(m_vmlib, "JNI_CreateJavaVM");
GetDefaultJavaVMInitArgs_t GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)GetProcAddress(m_vmlib, "JNI_GetDefaultJavaVMInitArgs");
if ((CreateJavaVM == NULL) || (GetDefaultJavaVMInitArgs == NULL))
{
m_statusCode = SunJVMDLL::JVM_CANT_USE_VM;
return false;
}
DEBUG("VM Created successfully");
m_javavm = new JavaVM();
m_javaenv = new JNIEnv();
DEBUG("DLL Setup on " + m_version.toString());
bool res;
if ((m_version.getMajor() == 1) && (m_version.getMinor() == 1))
res = setupVM11DLL(CreateJavaVM, GetDefaultJavaVMInitArgs);
else
res = setupVM12DLL(CreateJavaVM, GetDefaultJavaVMInitArgs);
registerJniSmooth();
DEBUG("Result code on DLL: " + StringUtils::toString(res));
if (res)
{
m_statusCode = SunJVMDLL::JVM_LOADED;
return true;
}
m_statusCode = SunJVMDLL::JVM_CANT_USE_VM;
return false;
}
bool SunJVMDLL::setupVM12DLL(CreateJavaVM_t CreateJavaVM, GetDefaultJavaVMInitArgs_t GetDefaultJavaVMInitArgs)
{
vector<string> jpropstrv;
for (int i=0; i<m_properties.size(); i++)
if(m_properties[i].getName()[0]=='-') {
jpropstrv.push_back( StringUtils::requoteForCommandLine(m_properties[i].getName()));
} else {
jpropstrv.push_back( StringUtils::requoteForCommandLine("-D" + m_properties[i].getName()) + "=" + StringUtils::requoteForCommandLine(m_properties[i].getValue()));
}
// DEBUG("MAXHEAP: " + StringUtils::toString(m_maxHeap));
// DEBUG("INITIALHEAP: " + StringUtils::toString(m_initialHeap));
if (m_maxHeap > 0)
{
jpropstrv.push_back("-Xmx" +StringUtils::toString(m_maxHeap));
}
if (m_initialHeap > 0)
{
jpropstrv.push_back("-Xms" + StringUtils::toString(m_initialHeap));
}
JavaVMInitArgs vm_args;
GetDefaultJavaVMInitArgs(&vm_args);
JavaVMOption options[1 + jpropstrv.size()];
std::string cpoption = "-Djava.class.path=" + StringUtils::join(m_pathElements, ";");
DEBUG("Classpath: " + cpoption);
options[0].optionString = (char*)cpoption.c_str();
vm_args.version = 0x00010002;
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 1 + jpropstrv.size();
for (int i=0; i<jpropstrv.size(); i++)
{
options[1 + i].optionString = (char*)jpropstrv[i].c_str();
DEBUG(string("Option added:") + options[1+i].optionString);
}
vm_args.ignoreUnrecognized = JNI_TRUE;
//
// Create the VM
if (CreateJavaVM( &m_javavm, &m_javaenv, &vm_args) != 0)
{
DEBUG("Can't create VM");
m_statusCode = SunJVMDLL::JVM_CANT_CREATE_VM;
return false;
}
DEBUG("VM 1.2+ Created successfully !!");
return true;
}
bool SunJVMDLL::setupVM11DLL(CreateJavaVM_t CreateJavaVM, GetDefaultJavaVMInitArgs_t GetDefaultJavaVMInitArgs)
{
JDK1_1InitArgs vm_args;
vm_args.version = 0x00010001;
GetDefaultJavaVMInitArgs(&vm_args);
if (m_maxHeap > 0)
vm_args.maxHeapSize = m_maxHeap;
if (m_initialHeap > 0)
vm_args.minHeapSize = m_initialHeap;
//
// create the properties array
//
char const * props[m_properties.size()+1];
vector<string> jpropstrv;
for (int i=0; i<m_properties.size(); i++)
jpropstrv[i] = m_properties[i].getName() + "=" + m_properties[i].getValue();
for (int i=0; i<m_properties.size(); i++)
props[i] = jpropstrv[i].c_str();
props[m_properties.size()] = NULL;
vm_args.properties = (char**)props;
/* Append USER_CLASSPATH to the default system class path */
std::string classpath = vm_args.classpath;
classpath += StringUtils::join(m_pathElements, ";");
DEBUG("Classpath = " + classpath);
vm_args.classpath = (char*)classpath.c_str();
//
// Create the VM
if (CreateJavaVM( &m_javavm, &m_javaenv, &vm_args) != 0)
{
DEBUG("Can't create VM");
m_statusCode = SunJVMDLL::JVM_CANT_CREATE_VM;
return false;
}
DEBUG("VM 1.1 Created successfully !!");
return true;
}
jclass SunJVMDLL::findClass(const std::string& clazz)
{
std::string classname = StringUtils::replace(clazz,".", "/");
DEBUG("Looking up for class <" + classname + ">");
jclass cls = env()->FindClass(classname.c_str());
if (cls == 0)
DEBUG("Can't find class " + classname + " !");
return cls;
}
jmethodID SunJVMDLL::findMethod(jclass& cls, const std::string& methodname, const std::string& signature, bool isStatic)
{
std::string sig = StringUtils::replace(signature, ".", "/");
jmethodID mid;
if (isStatic)
mid = env()->GetStaticMethodID(cls, methodname.c_str(), sig.c_str());
else
mid = env()->GetMethodID(cls, methodname.c_str(), sig.c_str());
return mid;
}
JavaVM* SunJVMDLL::getJavaVM()
{
return m_javavm;
}
void SunJVMDLL::setIntField(jclass cls, jobject obj, const std::string& fieldName, int value)
{
jfieldID binding = env()->GetFieldID(cls, fieldName.c_str(), "I");
env()->SetIntField(obj, binding, (jint)value);
}
void SunJVMDLL::setLongField(jclass cls, jobject obj, const std::string& fieldName, jlong value)
{
jfieldID binding = env()->GetFieldID(cls, fieldName.c_str(), "J");
env()->SetLongField(obj, binding, (jlong)value);
}
void SunJVMDLL::setObjectField(jclass cls, jobject obj, const std::string& fieldName, const std::string& fieldclass, jobject value)
{
std::string fc = "L" + StringUtils::replace(fieldclass, "." , "/") + ";";
jfieldID binding = env()->GetFieldID(cls, fieldName.c_str(), fc.c_str());
env()->SetObjectField(obj, binding, value);
}
jstring SunJVMDLL::newUTFString(const std::string& str)
{
return env()->NewStringUTF(str.c_str());
}
jobject SunJVMDLL::newObject(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->NewObjectA(clazz, methodid, arguments);
}
jobjectArray SunJVMDLL::newObjectArray(int size, jclass clazz, jobject initialValue)
{
return env()->NewObjectArray((jsize)size, clazz, initialValue);
}
jobjectArray SunJVMDLL::newObjectArray(int size, const std::string& classname, jobject initialValue)
{
jclass cls = findClass(classname);
return newObjectArray(size, cls, initialValue);
}
//
// Static method invocation
//
void SunJVMDLL::invokeVoidStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
env()->CallStaticVoidMethodA(clazz, methodid, arguments);
}
jboolean SunJVMDLL::invokeBooleanStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticBooleanMethodA(clazz, methodid, arguments);
}
jbyte SunJVMDLL::invokeByteStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticByteMethodA(clazz, methodid, arguments);
}
jchar SunJVMDLL::invokeCharStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticCharMethodA(clazz, methodid, arguments);
}
jshort SunJVMDLL::invokeShortStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticShortMethodA(clazz, methodid, arguments);
}
jint SunJVMDLL::invokeIntStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticIntMethodA(clazz, methodid, arguments);
}
jlong SunJVMDLL::invokeLongStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticLongMethodA(clazz, methodid, arguments);
}
jfloat SunJVMDLL::invokeFloatStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticFloatMethodA(clazz, methodid, arguments);
}
jdouble SunJVMDLL::invokeDoubleStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticDoubleMethodA(clazz, methodid, arguments);
}
jobject SunJVMDLL::invokeObjectStatic(jclass clazz, jmethodID& methodid, jvalue arguments[])
{
return env()->CallStaticObjectMethodA(clazz, methodid, arguments);
}
//
// method invocation
//
void SunJVMDLL::invokeVoid(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
env()->CallVoidMethodA(obj, methodid, arguments);
}
jboolean SunJVMDLL::invokeBoolean(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallBooleanMethodA(obj, methodid, arguments);
}
jbyte SunJVMDLL::invokeByte(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallByteMethodA(obj, methodid, arguments);
}
jchar SunJVMDLL::invokeChar(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallCharMethodA(obj, methodid, arguments);
}
jshort SunJVMDLL::invokeShort(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallShortMethodA(obj, methodid, arguments);
}
jint SunJVMDLL::invokeInt(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallIntMethodA(obj, methodid, arguments);
}
jlong SunJVMDLL::invokeLong(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallLongMethodA(obj, methodid, arguments);
}
jfloat SunJVMDLL::invokeFloat(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallFloatMethodA(obj, methodid, arguments);
}
jdouble SunJVMDLL::invokeDouble(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallDoubleMethodA(obj, methodid, arguments);
}
jobject SunJVMDLL::invokeObject(jobject& obj, jmethodID& methodid, jvalue arguments[])
{
return env()->CallObjectMethodA(obj, methodid, arguments);
}
bool SunJVMDLL::registerMethod(const std::string& classname, const std::string& methodname, const std::string& signature,
void* fn)
{
jclass cc = this->findClass(classname);
if (cc == 0)
return false;
JNINativeMethod jnm;
jnm.name = (char*)methodname.c_str();
jnm.signature = (char*)signature.c_str();
jnm.fnPtr = fn;
int res = env()->RegisterNatives(cc, &jnm, 1);
if (res != 0)
return false;
return true;
}
bool SunJVMDLL::registerJniSmooth()
{
registerNativeMethods(this);
return true;
}

View File

@ -0,0 +1,221 @@
/*
JSmooth: a VM wrapper toolkit for Windows
Copyright (C) 2003-2007 Rodrigo Reyes <reyes@charabia.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "SunJVMExe.h"
#include <vector>
#include <string>
#include "Process.h"
#include "FileUtils.h"
SunJVMExe::SunJVMExe(const std::string& jrehome)
{
m_jrehome = jrehome;
}
SunJVMExe::SunJVMExe(const std::string& jrehome, const Version& v)
{
m_jrehome = jrehome;
m_version = v;
}
bool SunJVMExe::run(const std::string& mainclass, bool useconsole)
{
if (!m_version.isValid())
{
m_version = guessVersion();
}
if (!m_version.isValid())
return false;
std::vector<std::string> execv;
execv.push_back(StringUtils::requoteForCommandLine(lookUpExecutable(useconsole)));
if (m_maxHeap > 0)
{
if ((m_version.getMajor()==1)&&(m_version.getMinor()==1))
execv.push_back("-mx" + StringUtils::toString(m_maxHeap));
else
execv.push_back("-Xmx" + StringUtils::toString(m_maxHeap));
}
if (m_initialHeap > 0)
{
if ((m_version.getMajor()==1)&&(m_version.getMinor()==1))
execv.push_back("-ms" + StringUtils::toString(m_initialHeap));
else
execv.push_back("-Xms" + StringUtils::toString(m_initialHeap));
}
for (int i=0; i<m_properties.size(); i++)
if(m_properties[i].getName()[0]=='-') {
execv.push_back( StringUtils::requoteForCommandLine(m_properties[i].getName()));
} else {
execv.push_back( StringUtils::requoteForCommandLine("-D" + m_properties[i].getName()) + "=" + StringUtils::requoteForCommandLine(m_properties[i].getValue()));
}
std::string classpath;
if ((m_version.getMajor()==1)&&(m_version.getMinor()==1))
classpath = getClassPath(true);
else
classpath = getClassPath(false);
if (classpath.size() > 0)
execv.push_back("-classpath " + StringUtils::requoteForCommandLine(classpath));
execv.push_back(mainclass);
for (int i=0; i<m_arguments.size(); i++)
{
execv.push_back( StringUtils::requoteForCommandLine(m_arguments[i]) );
}
std::string execmd = StringUtils::join(execv, " ");
DEBUG("COMMAND: <" + execmd + ">");
Process proc(execmd, useconsole);
if (proc.run())
{
DEBUG("Started successfully");
proc.join();
m_exitCode = proc.getExitCode();
return true;
}
else
{
DEBUG("Failed running " + execmd);
}
return false;
}
std::string SunJVMExe::lookUpExecutable(bool useconsole)
{
std::string java;
if (m_jrehome.size() == 0)
{
return useconsole?"java.exe":"javaw.exe";
}
if (useconsole)
{
if (FileUtils::fileExists(m_jrehome, "bin\\java.exe"))
java = FileUtils::concFile(m_jrehome, "bin\\java.exe");
else if (FileUtils::fileExists(m_jrehome, "bin\\jre.exe"))
java = FileUtils::concFile(m_jrehome, "bin\\jre.exe");
else
{
std::vector<std::string> javas = FileUtils::recursiveSearch(m_jrehome, "java.exe");
DEBUG("REC: " + StringUtils::toString(javas));
if (javas.size() == 0)
javas = FileUtils::recursiveSearch(m_jrehome, "jre.exe");
if (javas.size() > 0)
java = javas[0];
}
}
else
{
if (FileUtils::fileExists(m_jrehome, "bin\\javaw.exe"))
java = FileUtils::concFile(m_jrehome, "bin\\javaw.exe");
else if (FileUtils::fileExists(m_jrehome, "bin\\jrew.exe"))
java = FileUtils::concFile(m_jrehome, "bin\\jrew.exe");
else
{
std::vector<std::string> javas = FileUtils::recursiveSearch(m_jrehome, "javaw.exe");
DEBUG("REC: " + StringUtils::toString(javas));
if (javas.size() == 0)
javas = FileUtils::recursiveSearch(m_jrehome, "jrew.exe");
if (javas.size() > 0)
java = javas[0];
}
}
return java;
}
Version SunJVMExe::guessVersion()
{
std::string exepath = lookUpExecutable(true);
string exeline = exepath + " -version";
Version result;
// Return immediatly if the exe does not exist
if (!FileUtils::fileExists(exepath))
return result;
string tmpfilename = FileUtils::createTempFileName(".tmp");
Process proc(exeline, true);
proc.setRedirect(tmpfilename);
proc.run();
proc.join();
std::string voutput = FileUtils::readFile(tmpfilename);
vector<string> split = StringUtils::split(voutput, " \t\n\r", "\"");
for (vector<string>::iterator i=split.begin(); i != split.end(); i++)
{
Version v(*i);
if (v.isValid())
{
result = v;
break;
}
}
FileUtils::deleteOnReboot(tmpfilename);
return result;
}
std::string SunJVMExe::getClassPath(bool full)
{
std::vector<std::string> cp;
for (std::vector<std::string>::const_iterator i=m_pathElements.begin(); i!=m_pathElements.end(); i++)
cp.push_back(*i);
if (full)
{
std::string javaexe = lookUpExecutable(true);
std::string home = FileUtils::getParent( FileUtils::getParent( javaexe ));
if (FileUtils::fileExists(home))
{
vector<string> cpzips = FileUtils::recursiveSearch(home, "*.zip");
cp.insert(cp.end(), cpzips.begin(), cpzips.end());
vector<string> cpjars = FileUtils::recursiveSearch(home, "*.jar");
cp.insert(cp.end(), cpjars.begin(), cpjars.end());
}
}
return StringUtils::join(cp, ";");
}
int SunJVMExe::getExitCode()
{
return m_exitCode;
}