summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/native/JNIUtil.cpp
blob: 2adcf2f72f93b39d394c3aa46a0bc70af7e2a851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
/**
 * @copyright
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 * @endcopyright
 *
 * @file JNIUtil.cpp
 * @brief Implementation of the class JNIUtil
 */

/* Include apr.h first, or INT64_C won't be defined properly on some C99
   compilers, when other headers include <stdint.h> before defining some
   macros.

   See apr.h for the ugly details */
#include <apr.h>

#include "JNIUtil.h"
#include "Array.h"

#include <sstream>
#include <vector>
#include <locale.h>

#include <apr_strings.h>
#include <apr_tables.h>
#include <apr_general.h>
#include <apr_lib.h>
#include <apr_file_info.h>
#include <apr_time.h>

#include "svn_pools.h"
#include "svn_error.h"
#include "svn_fs.h"
#include "svn_ra.h"
#include "svn_utf.h"
#include "svn_wc.h"
#include "svn_dso.h"
#include "svn_path.h"
#include "svn_cache_config.h"
#include "private/svn_atomic.h"
#include "private/svn_utf_private.h"
#include "svn_private_config.h"

#include "SVNBase.h"
#include "JNIMutex.h"
#include "JNICriticalSection.h"
#include "JNIStringHolder.h"
#include "Pool.h"


#include "jniwrapper/jni_env.hpp"

// Static members of JNIUtil are allocated here.
apr_pool_t *JNIUtil::g_pool = NULL;
std::list<SVNBase*> JNIUtil::g_finalizedObjects;
JNIMutex *JNIUtil::g_finalizedObjectsMutex = NULL;
JNIMutex *JNIUtil::g_logMutex = NULL;
JNIMutex *JNIUtil::g_configMutex = NULL;
bool JNIUtil::g_initException;
int JNIUtil::g_logLevel = JNIUtil::noLog;
std::ofstream JNIUtil::g_logStream;

/* The error code we will use to signal a Java exception */
static const apr_status_t
SVN_ERR_JAVAHL_WRAPPED = SVN_ERR_MALFUNC_CATEGORY_START
                         + SVN_ERR_CATEGORY_SIZE - 10;

/**
 * Return the JNI environment to use
 * @return the JNI environment
 */
JNIEnv *JNIUtil::getEnv()
{
  return Java::Env().get();
}

/**
 * Initialize the environment for all requests.
 * @param env   the JNI environment for this request
 */
bool JNIUtil::JNIInit(JNIEnv *env)
{
  // Clear all standing exceptions.
  env->ExceptionClear();

  // Lock the list of finalized objects.
  JNICriticalSection cs(*g_finalizedObjectsMutex) ;
  if (isExceptionThrown())
    return false;

  // Delete all finalized, but not yet deleted objects.
  for (std::list<SVNBase*>::iterator it = g_finalizedObjects.begin();
       it != g_finalizedObjects.end();
       ++it)
    {
      delete *it;
    }
  g_finalizedObjects.clear();

  return true;
}

/* Forwarder for calling JNIGlobalInit from JNI_OnLoad(). */
bool initialize_jni_util(JNIEnv *env)
{
  return JNIUtil::JNIGlobalInit(env);
}

namespace {

volatile svn_atomic_t *gentle_crash_write_loc = NULL;

svn_error_t *
gently_crash_the_jvm(svn_boolean_t can_return,
                     const char *file, int line, const char *expr)
{
  if (!can_return)
    {
      // Try not to abort; aborting prevents the JVM from creating
      // a crash log, which is oh so useful for debugging.
      // We can't just raise a SEGV signal, either, because it will
      // be not be caught in the context that we're interested in
      // getting the stack trace from.

      // Try reading from and writing to the zero page
      const svn_atomic_t zeropage = svn_atomic_read(gentle_crash_write_loc);
      svn_atomic_set(gentle_crash_write_loc, zeropage);
    }

  // Forward to the standard malfunction handler, which does call
  // abort when !can_return; this will only happen if the write to the
  // zero page did not cause a SEGV.
  return svn_error_raise_on_malfunction(can_return, file, line, expr);
}
} // Anonymous namespace

/**
 * Initialize the environment for all requests.
 * This method must be called in a single-threaded context.
 * @param env   the JNI environment for this request
 */
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 http://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;
}

/**
 * Returns the global (not request specific) pool.
 * @return global pool
 */
apr_pool_t *JNIUtil::getPool()
{
  return g_pool;
}

void JNIUtil::raiseThrowable(const char *name, const char *message)
{
  if (getLogLevel() >= errorLog)
    {
      JNICriticalSection cs(*g_logMutex);
      g_logStream << "Throwable raised <" << message << ">" << std::endl;
    }

  JNIEnv *env = getEnv();
  jclass clazz = env->FindClass(name);
  if (isJavaExceptionThrown())
    return;

  env->ThrowNew(clazz, message);
}

void
JNIUtil::throwNativeException(const char *className, const char *msg,
                              const char *source, int aprErr)
{
  JNIEnv *env = getEnv();
  jclass clazz = env->FindClass(className);

  // Create a local frame for our references
  env->PushLocalFrame(LOCAL_FRAME_SIZE);
  if (JNIUtil::isJavaExceptionThrown())
    return;

  if (getLogLevel() >= exceptionLog)
    {
      JNICriticalSection cs(*g_logMutex);
      g_logStream << "Subversion JavaHL exception thrown, message:<";
      g_logStream << msg << ">";
      if (source)
        g_logStream << " source:<" << source << ">";
      if (aprErr != -1)
        g_logStream << " apr-err:<" << aprErr << ">";
      g_logStream << std::endl;
    }
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NOTHING();

  jstring jmessage = makeJString(msg);
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NOTHING();
  jstring jsource = makeJString(source);
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NOTHING();

  jmethodID mid = env->GetMethodID(clazz, "<init>",
                                   "(Ljava/lang/String;Ljava/lang/String;I)V");
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NOTHING();
  jobject nativeException = env->NewObject(clazz, mid, jmessage, jsource,
                                           static_cast<jint>(aprErr));
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NOTHING();

  env->Throw(static_cast<jthrowable>(env->PopLocalFrame(nativeException)));
}

void
JNIUtil::putErrorsInTrace(svn_error_t *err,
                          std::vector<jobject> &stackTrace)
{
  if (!err)
    return;

  JNIEnv *env = getEnv();

  // First, put all our child errors in the stack trace
  putErrorsInTrace(err->child, stackTrace);

  // Now, put our own error in the stack trace
  jclass stClazz = env->FindClass("java/lang/StackTraceElement");
  if (isJavaExceptionThrown())
    return;

  static jmethodID ctor_mid = 0;
  if (ctor_mid == 0)
    {
      ctor_mid = env->GetMethodID(stClazz, "<init>",
                                  "(Ljava/lang/String;Ljava/lang/String;"
                                  "Ljava/lang/String;I)V");
      if (isJavaExceptionThrown())
        return;
    }

  jstring jdeclClass = makeJString("native");
  if (isJavaExceptionThrown())
    return;

  char *tmp_path;
  char *path = svn_dirent_dirname(err->file, err->pool);
  while ((tmp_path = strchr(path, '/')))
    *tmp_path = '.';

  jstring jmethodName = makeJString(path);
  if (isJavaExceptionThrown())
    return;

  jstring jfileName = makeJString(svn_dirent_basename(err->file, err->pool));
  if (isJavaExceptionThrown())
    return;

  jobject jelement = env->NewObject(stClazz, ctor_mid, jdeclClass, jmethodName,
                                    jfileName, (jint) err->line);

  stackTrace.push_back(jelement);

  env->DeleteLocalRef(stClazz);
  env->DeleteLocalRef(jdeclClass);
  env->DeleteLocalRef(jmethodName);
  env->DeleteLocalRef(jfileName);
}

namespace {
struct MessageStackItem
{
  apr_status_t m_code;
  std::string m_message;
  bool m_generic;

  MessageStackItem(apr_status_t code, const char* message,
                   bool generic = false)
    : m_code(code),
      m_message(message),
      m_generic(generic)
    {}
};
typedef std::vector<MessageStackItem> ErrorMessageStack;

/*
 * Build the error message from the svn error into buffer.  This
 * method iterates through all the chained errors
 *
 * @param err               the subversion error
 * @param buffer            the buffer where the formated error message will
 *                          be stored
 * @return An array of error codes and messages
 */
ErrorMessageStack assemble_error_message(
    svn_error_t *err, std::string &result)
{
  // buffer for a single error message
  char errbuf[1024];
  apr_status_t parent_apr_err = 0;
  ErrorMessageStack message_stack;

  /* Pretty-print the error */
  /* Note: we can also log errors here someday. */

  for (int depth = 0; err;
       ++depth, parent_apr_err = err->apr_err, err = err->child)
    {
      /* When we're recursing, don't repeat the top-level message if its
       * the same as before. */
      if ((depth == 0 || err->apr_err != parent_apr_err)
          && err->apr_err != SVN_ERR_JAVAHL_WRAPPED)
        {
          const char *message;
          /* Is this a Subversion-specific error code? */
          if ((err->apr_err > APR_OS_START_USEERR)
              && (err->apr_err <= APR_OS_START_CANONERR))
            message = svn_strerror(err->apr_err, errbuf, sizeof(errbuf));
          /* Otherwise, this must be an APR error code. */
          else
            {
              /* Messages coming from apr_strerror are in the native
                 encoding, it's a good idea to convert them to UTF-8. */
              apr_strerror(err->apr_err, errbuf, sizeof(errbuf));
              svn_error_t* utf8_err =
                svn_utf_cstring_to_utf8(&message, errbuf, err->pool);
              if (utf8_err)
                {
                  /* Use fuzzy transliteration instead. */
                  svn_error_clear(utf8_err);
                  message = svn_utf_cstring_from_utf8_fuzzy(errbuf, err->pool);
                }
            }

          message_stack.push_back(
              MessageStackItem(err->apr_err, message, true));
        }
      if (err->message)
        {
          message_stack.push_back(
              MessageStackItem(err->apr_err, err->message));
        }
    }

  for (ErrorMessageStack::const_iterator it = message_stack.begin();
       it != message_stack.end(); ++it)
    {
      if (!it->m_generic)
        result += "svn: ";
      result += it->m_message;
      result += '\n';
    }
  return message_stack;
}

jobject construct_Jmessage_stack(const ErrorMessageStack& message_stack)
{
  JNIEnv *env = JNIUtil::getEnv();
  env->PushLocalFrame(LOCAL_FRAME_SIZE);
  if (JNIUtil::isJavaExceptionThrown())
    return NULL;

  jclass list_clazz = env->FindClass("java/util/ArrayList");
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;
  jmethodID mid = env->GetMethodID(list_clazz, "<init>", "(I)V");
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;
  jmethodID add_mid = env->GetMethodID(list_clazz, "add",
                                       "(Ljava/lang/Object;)Z");
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;
  jobject jlist = env->NewObject(list_clazz, mid, jint(message_stack.size()));
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  jclass clazz = env->FindClass(JAVAHL_CLASS("/ClientException$ErrorMessage"));
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;
  mid = env->GetMethodID(clazz, "<init>",
                         "(ILjava/lang/String;Z)V");
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  for (ErrorMessageStack::const_iterator it = message_stack.begin();
       it != message_stack.end(); ++it)
    {
      jobject jmessage = JNIUtil::makeJString(it->m_message.c_str());
      if (JNIUtil::isJavaExceptionThrown())
        POP_AND_RETURN_NULL;
      jobject jitem = env->NewObject(clazz, mid,
                                     jint(it->m_code), jmessage,
                                     jboolean(it->m_generic));
      if (JNIUtil::isJavaExceptionThrown())
        POP_AND_RETURN_NULL;
      env->CallBooleanMethod(jlist, add_mid, jitem);
      if (JNIUtil::isJavaExceptionThrown())
        POP_AND_RETURN_NULL;

      env->DeleteLocalRef(jmessage);
      env->DeleteLocalRef(jitem);
    }
  return env->PopLocalFrame(jlist);
}
} // anonymous namespace

std::string JNIUtil::makeSVNErrorMessage(svn_error_t *err,
                                         jstring *jerror_message,
                                         jobject *jmessage_stack)
{
  if (jerror_message)
    *jerror_message = NULL;
  if (jmessage_stack)
    *jmessage_stack = NULL;

  std::string buffer;
  err = svn_error_purge_tracing(err);
  if (err == NULL || err->apr_err == 0
      || !(jerror_message || jmessage_stack))
  return buffer;

  ErrorMessageStack message_stack = assemble_error_message(err, buffer);
  if (jerror_message)
    *jerror_message = makeJString(buffer.c_str());
  if (jmessage_stack)
    *jmessage_stack = construct_Jmessage_stack(message_stack);
  return buffer;
}

jthrowable JNIUtil::wrappedCreateClientException(svn_error_t *err, jthrowable jcause)
{
  jstring jmessage;
  jobject jstack;
  std::string msg = makeSVNErrorMessage(err, &jmessage, &jstack);
  if (JNIUtil::isJavaExceptionThrown())
    return NULL;

  const char *source = NULL;
#ifdef SVN_DEBUG
#ifndef SVN_ERR__TRACING
  if (err->file)
    {
      std::ostringstream buf;
      buf << err->file;
      if (err->line > 0)
        buf << ':' << err->line;
      source = buf.str().c_str();
    }
#endif
#endif

  if (!jcause)
    jcause = JNIUtil::unwrapJavaException(err);

  // Much of the following is stolen from throwNativeException().  As much as
  // we'd like to call that function, we need to do some manual stack
  // unrolling, so it isn't feasible.

  JNIEnv *env = getEnv();

  // Create a local frame for our references
  env->PushLocalFrame(LOCAL_FRAME_SIZE);
  if (JNIUtil::isJavaExceptionThrown())
    return NULL;

  jclass clazz = env->FindClass(JAVAHL_CLASS("/ClientException"));
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  if (getLogLevel() >= exceptionLog)
    {
      JNICriticalSection cs(*g_logMutex);
      g_logStream << "Subversion JavaHL exception thrown, message:<";
      g_logStream << msg << ">";
      if (source)
        g_logStream << " source:<" << source << ">";
      if (err->apr_err != -1)
        g_logStream << " apr-err:<" << err->apr_err << ">";
      g_logStream << std::endl;
    }
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  jstring jsource = makeJString(source);
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  jmethodID mid = env->GetMethodID(clazz, "<init>",
                                   "(Ljava/lang/String;"
                                   "Ljava/lang/Throwable;"
                                   "Ljava/lang/String;I"
                                   "Ljava/util/List;)V");
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;
  jobject nativeException = env->NewObject(clazz, mid, jmessage, jcause,
                                           jsource, jint(err->apr_err),
                                           jstack);
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

#ifdef SVN_ERR__TRACING
  // Add all the C error stack trace information to the Java Exception

  // Get the standard stack trace, and vectorize it using the Array class.
  static jmethodID mid_gst = 0;
  if (mid_gst == 0)
    {
      mid_gst = env->GetMethodID(clazz, "getStackTrace",
                                 "()[Ljava/lang/StackTraceElement;");
      if (isJavaExceptionThrown())
        POP_AND_RETURN_NULL;
    }
  Array stackTraceArray((jobjectArray) env->CallObjectMethod(nativeException,
                                                             mid_gst));
  std::vector<jobject> oldStackTrace = stackTraceArray.vector();

  // Build the new stack trace elements from the chained errors.
  std::vector<jobject> newStackTrace;
  putErrorsInTrace(err, newStackTrace);

  // Join the new elements with the old ones
  for (std::vector<jobject>::const_iterator it = oldStackTrace.begin();
            it < oldStackTrace.end(); ++it)
    {
      newStackTrace.push_back(*it);
    }

  jclass stClazz = env->FindClass("java/lang/StackTraceElement");
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  const jsize stSize = static_cast<jsize>(newStackTrace.size());
  if (stSize < 0 || stSize != newStackTrace.size())
    {
      env->ThrowNew(env->FindClass("java.lang.ArithmeticException"),
                    "Overflow converting C size_t to JNI jsize");
      POP_AND_RETURN_NULL;
    }
  jobjectArray jStackTrace = env->NewObjectArray(stSize, stClazz, NULL);
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  int i = 0;
  for (std::vector<jobject>::const_iterator it = newStackTrace.begin();
       it < newStackTrace.end(); ++it)
    {
      env->SetObjectArrayElement(jStackTrace, i, *it);
      ++i;
    }

  // And put the entire trace back into the exception
  static jmethodID mid_sst = 0;
  if (mid_sst == 0)
    {
      mid_sst = env->GetMethodID(clazz, "setStackTrace",
                                 "([Ljava/lang/StackTraceElement;)V");
      if (isJavaExceptionThrown())
        POP_AND_RETURN_NULL;
    }
  env->CallVoidMethod(nativeException, mid_sst, jStackTrace);
  if (isJavaExceptionThrown())
    POP_AND_RETURN_NULL;
#endif

  return static_cast<jthrowable>(env->PopLocalFrame(nativeException));
}

jthrowable JNIUtil::createClientException(svn_error_t *err, jthrowable jcause)
{
  jthrowable jexc = NULL;
  try {
    jexc = wrappedCreateClientException(err, jcause);
  } catch (...) {
    svn_error_clear(err);
    throw;
  }
  svn_error_clear(err);
  return jexc;
}

void JNIUtil::handleSVNError(svn_error_t *err, jthrowable jcause)
{
  jthrowable jexc = createClientException(err, jcause);
  if (jexc)
    getEnv()->Throw(jexc);
}

void JNIUtil::putFinalizedClient(SVNBase *object)
{
  enqueueForDeletion(object);
}

void JNIUtil::enqueueForDeletion(SVNBase *object)
{
  JNICriticalSection cs(*g_finalizedObjectsMutex);
  if (!isExceptionThrown())
    g_finalizedObjects.push_back(object);
}

/**
 * Handle an apr error (those are not expected) by throwing an error.
 * @param error the apr error number
 * @param op the apr function returning the error
 */
void JNIUtil::handleAPRError(int error, const char *op)
{
  char buffer[2048];

  apr_snprintf(buffer, sizeof(buffer),
               _("an error occurred in function %s with return value %d"),
               op, error);

  throwError(buffer);
}

namespace {
const char* known_exception_to_cstring(apr_pool_t* pool)
{
  JNIEnv *env = JNIUtil::getEnv();
  jthrowable t = env->ExceptionOccurred();
  jclass cls = env->GetObjectClass(t);

  jstring jclass_name;
  {
    jmethodID mid = env->GetMethodID(cls, "getClass", "()Ljava/lang/Class;");
    jobject clsobj = env->CallObjectMethod(t, mid);
    jclass basecls = env->GetObjectClass(clsobj);
    mid = env->GetMethodID(basecls, "getName", "()Ljava/lang/String;");
    jclass_name = (jstring) env->CallObjectMethod(clsobj, mid);
  }

  jstring jmessage;
  {
    jmethodID mid = env->GetMethodID(cls, "getMessage",
                                     "()Ljava/lang/String;");
    jmessage = (jstring) env->CallObjectMethod(t, mid);
  }

  JNIStringHolder class_name(jclass_name);
  if (jmessage)
    {
      JNIStringHolder message(jmessage);
      return apr_pstrcat(pool, class_name.c_str(), ": ", message.c_str(), NULL);
    }
  else
    return class_name.pstrdup(pool);
  // ### Conditionally add t.printStackTrace() to msg?
}

const char* exception_to_cstring(apr_pool_t* pool)
{
  const char *msg;
  if (JNIUtil::getEnv()->ExceptionCheck())
    {
      msg = known_exception_to_cstring(pool);
    }
  else
    {
      msg = NULL;
    }
  return msg;
}
} // anonymous namespace

const char *
JNIUtil::thrownExceptionToCString(SVN::Pool &in_pool)
{
  return exception_to_cstring(in_pool.getPool());
}

svn_error_t*
JNIUtil::checkJavaException(apr_status_t errorcode)
{
  if (!getEnv()->ExceptionCheck())
    return SVN_NO_ERROR;
  svn_error_t* err = svn_error_create(errorcode, NULL, NULL);
  const char* const msg = known_exception_to_cstring(err->pool);
  if (msg)
    err->message = apr_psprintf(err->pool, _("Java exception: %s"), msg);
  else
    err->message = _("Java exception");
  return err;
}

/**
 * Create a Java string from a native UTF-8 string.
 * @param txt   native UTF-8 string
 * @return the Java string. It is a local reference, which should be deleted
 *         as soon a possible
 */
jstring JNIUtil::makeJString(const char *txt)
{
  if (txt == NULL)
    // A NULL pointer is equates to a null java.lang.String.
    return NULL;

  JNIEnv *env = getEnv();
  return env->NewStringUTF(txt);
}

/**
 * Initialite the log file.
 * @param level the log level
 * @param the name of the log file
 */
void JNIUtil::initLogFile(int level, jstring path)
{
  // lock this operation
  JNICriticalSection cs(*g_logMutex);
  if (g_logLevel > noLog) // if the log file has been opened
    g_logStream.close();

  // remember the log level
  g_logLevel = level;
  JNIStringHolder myPath(path);
  if (g_logLevel > noLog) // if a new log file is needed
    {
      // open it
      g_logStream.open(myPath, std::ios::app);
    }
}

/**
 * Returns the current log level.
 * @return the log level
 */
int JNIUtil::getLogLevel()
{
  return g_logLevel;
}

/**
 * Write a message to the log file if needed.
 * @param the log message
 */
void JNIUtil::logMessage(const char *message)
{
  // lock the log file
  JNICriticalSection cs(*g_logMutex);
  g_logStream << message << std::endl;
}

/**
 * Create a java.util.Date object from an apr time.
 * @param time  the apr time
 * @return the java.util.Date. This is a local reference.  Delete as
 *         soon as possible
 */
jobject JNIUtil::createDate(apr_time_t time)
{
  jlong javatime = time /1000;
  JNIEnv *env = getEnv();
  jclass clazz = env->FindClass("java/util/Date");
  if (isJavaExceptionThrown())
    return NULL;

  static jmethodID mid = 0;
  if (mid == 0)
    {
      mid = env->GetMethodID(clazz, "<init>", "(J)V");
      if (isJavaExceptionThrown())
        return NULL;
    }

  jobject ret = env->NewObject(clazz, mid, javatime);
  if (isJavaExceptionThrown())
    return NULL;

  env->DeleteLocalRef(clazz);

  return ret;
}

apr_time_t
JNIUtil::getDate(jobject jdate)
{
  JNIEnv *env = getEnv();
  jclass clazz = env->FindClass("java/util/Date");
  if (isJavaExceptionThrown())
    return 0;

  static jmethodID mid = 0;
  if (mid == 0)
    {
      mid = env->GetMethodID(clazz, "getTime", "()J");
      if (isJavaExceptionThrown())
        return 0;
    }

  jlong jmillis = env->CallLongMethod(jdate, mid);
  if (isJavaExceptionThrown())
    return 0;

  env->DeleteLocalRef(clazz);

  return jmillis * 1000;
}

/**
 * Create a Java byte array from an array of characters.
 * @param data      the character array
 * @param length    the number of characters in the array
 */
jbyteArray JNIUtil::makeJByteArray(const void *data, int length)
{
  // a NULL will create no Java array
  if (!data)
    return NULL;

  JNIEnv *env = getEnv();

  // Allocate the Java array.
  jbyteArray ret = env->NewByteArray(length);
  if (isJavaExceptionThrown() || ret == NULL)
      return NULL;

  // Access the bytes.
  jbyte *retdata = env->GetByteArrayElements(ret, NULL);
  if (isJavaExceptionThrown())
    return NULL;

  // Copy the bytes.
  memcpy(retdata, data, length);

  // Release the bytes.
  env->ReleaseByteArrayElements(ret, retdata, 0);
  if (isJavaExceptionThrown())
    return NULL;

  return ret;
}

/**
 * Create a Java byte array from an svn_string_t.
 * @param str       the string
 */
jbyteArray JNIUtil::makeJByteArray(const svn_string_t *str)
{
  // a NULL will create no Java array
  if (!str)
    return NULL;

  return JNIUtil::makeJByteArray(str->data, static_cast<int>(str->len));
}

/**
 * Throw a Java NullPointerException.  Used when input parameters
 * which should not be null are that.
 *
 * @param message   the name of the parameter that is null
 */
void JNIUtil::throwNullPointerException(const char *message)
{
  if (getLogLevel() >= errorLog)
    logMessage("NullPointerException thrown");

  JNIEnv *env = getEnv();
  jclass clazz = env->FindClass("java/lang/NullPointerException");
  if (isJavaExceptionThrown())
    return;

  env->ThrowNew(clazz, message);
}

svn_error_t *JNIUtil::preprocessPath(const char *&path, apr_pool_t *pool)
{
  /* URLs and wc-paths get treated differently. */
  if (svn_path_is_url(path))
    {
      /* No need to canonicalize a URL's case or path separators. */

      /* Convert to URI. */
      path = svn_path_uri_from_iri(path, pool);

      /* Auto-escape some ASCII characters. */
      path = svn_path_uri_autoescape(path, pool);

      /* The above doesn't guarantee a valid URI. */
      if (! svn_path_is_uri_safe(path))
        return svn_error_createf(SVN_ERR_BAD_URL, NULL,
                                 _("URL '%s' is not properly URI-encoded"),
                                 path);

      /* Verify that no backpaths are present in the URL. */
      if (svn_path_is_backpath_present(path))
        return svn_error_createf(SVN_ERR_BAD_URL, NULL,
                                 _("URL '%s' contains a '..' element"),
                                 path);

      /* strip any trailing '/' */
      path = svn_uri_canonicalize(path, pool);
    }
  else  /* not a url, so treat as a path */
    {
      /* Normalize path to subversion internal style */

      /* ### In Subversion < 1.6 this method on Windows actually tried
         to lookup the path on disk to fix possible invalid casings in
         the passed path. (An extremely expensive operation; especially
         on network drives).

         This 'feature'is now removed as it penalizes every correct
         path passed, and also breaks behavior of e.g.
           'svn status .' returns '!' file, because there is only a "File"
             on disk.
            But when you then call 'svn status file', you get '? File'.

         As JavaHL is designed to be platform independent I assume users
         don't want this broken behavior on non round-trippable paths, nor
         the performance penalty.
       */

      path = svn_dirent_internal_style(path, pool);

      /* For kicks and giggles, let's absolutize it. */
      SVN_ERR(svn_dirent_get_absolute(&path, path, pool));
    }

  return NULL;
}

/* Tag to use on the apr_pool_t to store a WrappedException reference */
static const char *WrapExceptionTag = "org.apache.subversion.JavaHL.svnerror";

class WrappedException
{
  JNIEnv *m_env;
  jthrowable m_exception;
#ifdef SVN_DEBUG
  bool m_fetched;
#endif
public:
  WrappedException(JNIEnv *env)
  {
    m_env = env;

    // Fetch exception inside local frame
    jthrowable exceptionObj = env->ExceptionOccurred();

    // Now clear exception status
    env->ExceptionClear();

    // As adding a reference in exception state fails
    m_exception = static_cast<jthrowable>(env->NewGlobalRef(exceptionObj));

#ifdef SVN_DEBUG
    m_fetched = false;
#endif
  }

  static jthrowable get_exception(apr_pool_t *pool)
  {
      void *data;
      if (! apr_pool_userdata_get(&data, WrapExceptionTag, pool))
      {
          WrappedException *we = reinterpret_cast<WrappedException *>(data);

          if (we)
          {
#ifdef SVN_DEBUG
              we->m_fetched = TRUE;
#endif
              // Create reference in local frame, as the pool will be cleared
              return static_cast<jthrowable>(
                            we->m_env->NewLocalRef(we->m_exception));
          }
      }
      return NULL;
  }

private:
  ~WrappedException()
  {
#ifdef SVN_DEBUG
      if (!m_fetched)
          SVN_DBG(("Cleared svn_error_t * before Java exception was fetched"));
#endif
      m_env->DeleteGlobalRef(m_exception);
  }
public:
  static apr_status_t cleanup(void *data)
  {
    WrappedException *we = reinterpret_cast<WrappedException *>(data);

    delete we;
    return APR_SUCCESS;
  }
};

svn_error_t* JNIUtil::wrapJavaException()
{
  if (!isExceptionThrown())
    return SVN_NO_ERROR;

  svn_error_t *err = svn_error_create(SVN_ERR_JAVAHL_WRAPPED, NULL,
                                      "Wrapped Java Exception");
  apr_pool_userdata_set(new WrappedException(getEnv()), WrapExceptionTag,
                        WrappedException::cleanup, err->pool);
  return err;
}

jthrowable JNIUtil::unwrapJavaException(const svn_error_t *err)
{
    if (!err)
        return NULL;
    return
        WrappedException::get_exception(err->pool);
}