diff --git a/jsmooth-0.9.9-7-patch/skeletons/commonjava/SunJVMDLL.cpp b/jsmooth-0.9.9-7-patch/skeletons/commonjava/SunJVMDLL.cpp new file mode 100644 index 00000000..2566a26d --- /dev/null +++ b/jsmooth-0.9.9-7-patch/skeletons/commonjava/SunJVMDLL.cpp @@ -0,0 +1,422 @@ +/* + JSmooth: a VM wrapper toolkit for Windows + Copyright (C) 2003-2007 Rodrigo Reyes + + 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; iSetObjectArrayElement(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 jpropstrv; + for (int i=0; i 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 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 jpropstrv; + + for (int i=0; i"); + 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; +} diff --git a/jsmooth-0.9.9-7-patch/skeletons/commonjava/SunJVMExe.cpp b/jsmooth-0.9.9-7-patch/skeletons/commonjava/SunJVMExe.cpp new file mode 100644 index 00000000..4a292e90 --- /dev/null +++ b/jsmooth-0.9.9-7-patch/skeletons/commonjava/SunJVMExe.cpp @@ -0,0 +1,221 @@ +/* + JSmooth: a VM wrapper toolkit for Windows + Copyright (C) 2003-2007 Rodrigo Reyes + + 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 +#include +#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 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"); + + 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 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 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 split = StringUtils::split(voutput, " \t\n\r", "\""); + for (vector::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 cp; + for (std::vector::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 cpzips = FileUtils::recursiveSearch(home, "*.zip"); + cp.insert(cp.end(), cpzips.begin(), cpzips.end()); + vector cpjars = FileUtils::recursiveSearch(home, "*.jar"); + cp.insert(cp.end(), cpjars.begin(), cpjars.end()); + } + } + + return StringUtils::join(cp, ";"); +} + +int SunJVMExe::getExitCode() +{ + return m_exitCode; +}