Commit 7d4f2b4e853d2733b205eec8ecdfa3319ebaaf5e

Authored by perry.werneck@gmail.com
1 parent 324ba943

Mudando carga da JVM para dinâmica em windows para evitar problemas de naminh e path

@@ -408,7 +408,8 @@ fi @@ -408,7 +408,8 @@ fi
408 # Java link options depends on OS 408 # Java link options depends on OS
409 case "$host" in 409 case "$host" in
410 *-mingw32) 410 *-mingw32)
411 - jvm_libs="-L\"\$(JDK_HOME)/lib\" -ljvm" 411 +# jvm_libs="-L\"\$(JDK_HOME)/lib\" -ljvm"
  412 + jvm_libs="-L\"\$(JRE_HOME)/bin/client\" -ljvm"
412 ;; 413 ;;
413 414
414 *-apple-*) 415 *-apple-*)
src/java/jni3270.cbp
@@ -41,6 +41,7 @@ @@ -41,6 +41,7 @@
41 <Unit filename="../../configure.ac" /> 41 <Unit filename="../../configure.ac" />
42 <Unit filename="../classlib/exception.cc" /> 42 <Unit filename="../classlib/exception.cc" />
43 <Unit filename="../classlib/local.cc" /> 43 <Unit filename="../classlib/local.cc" />
  44 + <Unit filename="../classlib/module.cc" />
44 <Unit filename="../classlib/remote.cc" /> 45 <Unit filename="../classlib/remote.cc" />
45 <Unit filename="../classlib/session.cc" /> 46 <Unit filename="../classlib/session.cc" />
46 <Unit filename="../classlib/testprogram.cc" /> 47 <Unit filename="../classlib/testprogram.cc" />
src/java/plugin.cc
@@ -27,6 +27,21 @@ @@ -27,6 +27,21 @@
27 * 27 *
28 */ 28 */
29 29
  30 +#if defined WIN32
  31 +
  32 + // http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
  33 + #ifndef LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
  34 + #define LOAD_LIBRARY_SEARCH_DEFAULT_DIRS 0x00001000
  35 + #endif // LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
  36 +
  37 + #ifndef LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
  38 + #define LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 0x00000100
  39 + #endif // LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
  40 +
  41 + #include <windows.h>
  42 +
  43 +#endif
  44 +
30 #include "private.h" 45 #include "private.h"
31 46
32 #include <libintl.h> 47 #include <libintl.h>
@@ -322,6 +337,87 @@ extern &quot;C&quot; { @@ -322,6 +337,87 @@ extern &quot;C&quot; {
322 337
323 } 338 }
324 339
  340 +#ifdef _WIN32
  341 + /*
  342 + * Dynamically load jvm to avoid naming and path problems
  343 + *
  344 + */
  345 + static void load_jvm(GtkWidget *widget, HMODULE &hModule) {
  346 +
  347 + // Dynamically load jvm library to avoid naming and path problems.
  348 + HMODULE kernel;
  349 + HANDLE WINAPI (*AddDllDirectory)(PCWSTR NewDirectory);
  350 + BOOL WINAPI (*RemoveDllDirectory)(HANDLE Cookie);
  351 +
  352 + struct _dlldir {
  353 + const gchar * env;
  354 + const gchar * path;
  355 + HANDLE cookie;
  356 + } dlldir[] = {
  357 + { "JRE_HOME", "bin\\client", 0 },
  358 + { "JDK_HOME", "jre\\bin\\client", 0 }
  359 + };
  360 +
  361 + kernel = LoadLibrary("kernel32.dll");
  362 + AddDllDirectory = (HANDLE WINAPI (*)(PCWSTR)) GetProcAddress(kernel,"AddDllDirectory");
  363 + RemoveDllDirectory = (BOOL WINAPI (*)(HANDLE)) GetProcAddress(kernel,"RemoveDllDirectory");
  364 +
  365 + // Acrescenta mais caminhos para achar a dll
  366 + for(size_t f = 0; f < G_N_ELEMENTS(dlldir); f++) {
  367 +
  368 + const char *env = getenv(dlldir[f].env);
  369 + if(env && AddDllDirectory) {
  370 +
  371 + gchar *p = g_build_filename(env,dlldir[f].path,NULL);
  372 +
  373 + debug("Adicionando diretório \"%s\"",p);
  374 +
  375 + wchar_t *path = (wchar_t *) malloc(4096*sizeof(wchar_t));
  376 + mbstowcs(path, p, 4095);
  377 + dlldir[f].cookie = AddDllDirectory(path);
  378 + free(path);
  379 +
  380 + g_free(p);
  381 +
  382 + }
  383 + }
  384 +
  385 + hModule = LoadLibrary("jvm.dll");
  386 +
  387 + if(!hModule) {
  388 +
  389 + GtkWidget *dialog = gtk_message_dialog_new( GTK_WINDOW(gtk_widget_get_toplevel(widget)),
  390 + GTK_DIALOG_DESTROY_WITH_PARENT,
  391 + GTK_MESSAGE_ERROR,
  392 + GTK_BUTTONS_CANCEL,
  393 + "%s", _( "Can't load java virtual machine" ) );
  394 +
  395 + gtk_window_set_title(GTK_WINDOW(dialog),_( "JVM error" ));
  396 + gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),"%s", session::win32_strerror(GetLastError()).c_str());
  397 +
  398 + gtk_dialog_run(GTK_DIALOG (dialog));
  399 + gtk_widget_destroy(dialog);
  400 +
  401 + }
  402 +
  403 + // Libera caminhos extras.
  404 + for(size_t f = 0; f < G_N_ELEMENTS(dlldir); f++) {
  405 +
  406 + if(dlldir[f].cookie && RemoveDllDirectory) {
  407 +
  408 + RemoveDllDirectory(dlldir[f].cookie);
  409 +
  410 + }
  411 + }
  412 +
  413 + FreeLibrary(kernel);
  414 +
  415 +
  416 + }
  417 +
  418 +
  419 +#endif // _WIN32
  420 +
325 421
326 LIB3270_EXPORT int pw3270_plugin_start(GtkWidget *window) 422 LIB3270_EXPORT int pw3270_plugin_start(GtkWidget *window)
327 { 423 {
@@ -371,6 +467,28 @@ extern &quot;C&quot; @@ -371,6 +467,28 @@ extern &quot;C&quot;
371 467
372 v3270_set_script(widget,'J',TRUE); 468 v3270_set_script(widget,'J',TRUE);
373 469
  470 +#ifdef _WIN32
  471 +
  472 + HMODULE hJVM = 0;
  473 +
  474 + load_jvm(widget,hJVM);
  475 +
  476 + if(!hJVM) {
  477 + v3270_set_script(widget,'J',FALSE);
  478 +
  479 +#if GTK_CHECK_VERSION(2,32,0)
  480 + g_mutex_unlock(&mutex);
  481 +#else
  482 + g_static_mutex_unlock(&mutex);
  483 +#endif // GTK_CHECK_VERSION
  484 +
  485 + return;
  486 +
  487 + }
  488 +
  489 +#endif // _WIN32
  490 +
  491 +
374 // Start JNI 492 // Start JNI
375 JavaVMInitArgs vm_args; 493 JavaVMInitArgs vm_args;
376 JavaVMOption options[5]; 494 JavaVMOption options[5];
@@ -426,7 +544,14 @@ extern &quot;C&quot; @@ -426,7 +544,14 @@ extern &quot;C&quot;
426 g_free(myDir); 544 g_free(myDir);
427 g_free(exports); 545 g_free(exports);
428 546
429 - rc = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args); 547 + // Windows, first find the method, then call it.
  548 + jint JNICALL (*CreateJavaVM)(JavaVM **, void **, void *) = (jint JNICALL (*)(JavaVM **, void **, void *)) GetProcAddress(hJVM,"JNI_CreateJavaVM");
  549 +
  550 + if(!CreateJavaVM) {
  551 + rc = ENOENT;
  552 + } else {
  553 + rc = CreateJavaVM(&jvm,(void **)&env,&vm_args);
  554 + }
430 555
431 #else 556 #else
432 557
@@ -438,6 +563,7 @@ extern &quot;C&quot; @@ -438,6 +563,7 @@ extern &quot;C&quot;
438 options[vm_args.nOptions++].optionString = g_strdup_printf("-Djava.class.path=%s:%s",JARDIR,dirname); 563 options[vm_args.nOptions++].optionString = g_strdup_printf("-Djava.class.path=%s:%s",JARDIR,dirname);
439 #endif // JNIDIR 564 #endif // JNIDIR
440 565
  566 + // Linux, just create JVM
441 rc = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args); 567 rc = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
442 568
443 #endif // _WIn32 569 #endif // _WIn32
@@ -532,6 +658,11 @@ extern &quot;C&quot; @@ -532,6 +658,11 @@ extern &quot;C&quot;
532 jvm->DestroyJavaVM(); 658 jvm->DestroyJavaVM();
533 } 659 }
534 660
  661 +#ifdef _WIN32
  662 + if(hJVM)
  663 + FreeLibrary(hJVM);
  664 +#endif // _WIN32
  665 +
535 // And release 666 // And release
536 v3270_set_script(widget,'J',FALSE); 667 v3270_set_script(widget,'J',FALSE);
537 668