bool JNIUtil::JNIGlobalInit()

in subversion/bindings/javahl/native/JNIUtil.cpp [159:286]


bool JNIUtil::JNIGlobalInit(JNIEnv *env)
{
  svn_error_t *err;

  /* This has to happen before any pools are created. */
  if ((err = svn_dso_initialize2()))
    {
      if (stderr && err->message)
        fprintf(stderr, "%s", err->message);

      svn_error_clear(err);
      return FALSE;
    }

  /* Create our top-level pool.
     N.B.: APR was initialized by JNI_OnLoad. */
  g_pool = svn_pool_create(NULL);

  apr_allocator_t* allocator = apr_pool_allocator_get(g_pool);

  if (allocator)
    {
      /* Keep a maximum of 1 free block, to release memory back to the JVM
         (and other modules). */
      apr_allocator_max_free_set(allocator, 1);
    }

  svn_utf_initialize2(FALSE, g_pool); /* Optimize character conversions */

  // Initialize the libraries we use
  err = svn_fs_initialize(g_pool);
  if (!err)
    err = svn_ra_initialize(g_pool);
  if (err)
    {
      if (stderr && err->message)
        fprintf(stderr, "%s", err->message);

      svn_error_clear(err);
      return FALSE;
    }

  /* We shouldn't fill the JVMs memory with FS cache data unless
     explicitly requested. And we don't either, because the caches get
     allocated outside the JVM heap. Duh. */
  {
    svn_cache_config_t settings = *svn_cache_config_get();
    settings.single_threaded = FALSE;
    svn_cache_config_set(&settings);
  }

#ifdef ENABLE_NLS
#ifdef WIN32
  {
    WCHAR ucs2_path[MAX_PATH];
    const char *utf8_path;
    const char *internal_path;
    svn_error_t *err;
    apr_pool_t *pool = svn_pool_create(g_pool);

    /* get dll name - our locale info will be in '../share/locale' */
    HINSTANCE moduleHandle = GetModuleHandle("libsvnjavahl-1");
    GetModuleFileNameW(moduleHandle, ucs2_path,
                       sizeof(ucs2_path) / sizeof(ucs2_path[0]));
    err = svn_utf__win32_utf16_to_utf8(&utf8_path, ucs2_path, NULL, pool);
    if (err)
      {
        if (stderr)
          svn_handle_error2(err, stderr, false, "svn: ");
        svn_error_clear(err);
        return false;
      }

    internal_path = svn_dirent_internal_style(utf8_path, pool);
    /* get base path name */
    internal_path = svn_dirent_dirname(internal_path, pool);
    internal_path = svn_dirent_join(internal_path, SVN_LOCALE_RELATIVE_PATH,
                                  pool);
    bindtextdomain(PACKAGE_NAME, internal_path);
    svn_pool_destroy(pool);
  }
#else
  bindtextdomain(PACKAGE_NAME, SVN_LOCALE_DIR);
#endif
#endif

#if defined(WIN32) || defined(__CYGWIN__)
  /* See https://svn.apache.org/repos/asf/subversion/trunk/notes/asp-dot-net-hack.txt */
  /* ### This code really only needs to be invoked by consumers of
     ### the libsvn_wc library, which basically means SVNClient. */
  if (getenv("SVN_ASP_DOT_NET_HACK"))
    {
      err = svn_wc_set_adm_dir("_svn", g_pool);
      if (err)
        {
          if (stderr)
            {
              fprintf(stderr,
                      "%s: error: SVN_ASP_DOT_NET_HACK failed: %s\n",
                      "svnjavahl", err->message);
            }
          svn_error_clear(err);
          return FALSE;
        }
    }
#endif

  svn_error_set_malfunction_handler(svn_error_raise_on_malfunction);

  // Build all mutexes.
  g_finalizedObjectsMutex = new JNIMutex(g_pool);
  if (isExceptionThrown())
    return false;

  g_logMutex = new JNIMutex(g_pool);
  if (isExceptionThrown())
    return false;

  g_configMutex = new JNIMutex(g_pool);
  if (isExceptionThrown())
    return false;

  // Set a malfunction handler that tries not to call abort, because
  // that would prevent the JVM from creating a crash and stack log file.
  svn_error_set_malfunction_handler(gently_crash_the_jvm);

  return true;
}