File: com_sun_grid_drmaa_SessionImpl.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (1115 lines) | stat: -rw-r--r-- 37,714 bytes parent folder | download | duplicates (6)
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
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <strings.h>
#include <string.h>
#include <errno.h>

#include "japi/drmaa.h"
#include "japi/msg_drmaa.h"
#include "com_sun_grid_drmaa_SessionImpl.h"

#define BUFFER_LENGTH 1024
#define TEMPLATE_LIST_LENGTH 1024

enum {
   /* -------------- these are relevant to all sections ---------------- */
   DRMAAJ_ERRNO_SUCCESS = 0, /* Routine returned normally with success. */

   DRMAAJ_ERRNO_INTERNAL_ERROR, /* Unexpected or internal DRMAA error like
                                   memory allocation, system call failure,
                                   etc. */
   DRMAAJ_ERRNO_DRM_COMMUNICATION_FAILURE, /* Could not contact DRM system for
                                              this request. */
   DRMAAJ_ERRNO_AUTH_FAILURE, /* The specified request is not processed
                                 successfully due to authorization failure. */
   DRMAAJ_ERRNO_INVALID_ARGUMENT, /* The input value for an argument is
                                     invalid. */
   DRMAAJ_ERRNO_NO_ACTIVE_SESSION, /* Exit routine failed because there is no
                                      active session */
   DRMAAJ_ERRNO_NO_MEMORY, /* failed allocating memory */

   /* -------------- init and exit specific --------------- */
   DRMAAJ_ERRNO_INVALID_CONTACT_STRING, /* Initialization failed due to invalid
                                           contact string. */
   DRMAAJ_ERRNO_DEFAULT_CONTACT_STRING_ERROR, /* DRMAA could not use the default
                                                 contact string to connect to
                                                 DRM system. */
   DRMAAJ_ERRNO_NO_DEFAULT_CONTACT_STRING_SELECTED, /* No defaults contact
                                                       string was provided or
                                                       selected. DRMAA requires
                                                       that the default contact
                                                       string is selected when
                                                       there is more than one
                                                       default contact string
                                                       due to multiple DRMAA
                                                       implementation contained
                                                       in the binary module. */
   DRMAAJ_ERRNO_DRMS_INIT_FAILED, /* Initialization failed due to failure to
                                     init DRM system. */
   DRMAAJ_ERRNO_ALREADY_ACTIVE_SESSION, /* Initialization failed due to existing
                                           DRMAA session. */
   DRMAAJ_ERRNO_DRMS_EXIT_ERROR, /* DRM system disengagement failed. */

   /* ---------------- job attributes specific -------------- */
   DRMAAJ_ERRNO_INVALID_ATTRIBUTE_FORMAT, /* The format for the job attribute
                                             value is invalid. */
   DRMAAJ_ERRNO_INVALID_ATTRIBUTE_VALUE, /* The value for the job attribute is
                                            invalid. */
   DRMAAJ_ERRNO_CONFLICTING_ATTRIBUTE_VALUES, /* The value of this attribute is
                                                 conflicting with a previously
                                                 set attributes. */

   /* --------------------- job submission specific -------------- */
   DRMAAJ_ERRNO_TRY_LATER, /* Could not pass job now to DRM system. A retry may
                              succeed however (saturation). */
   DRMAAJ_ERRNO_DENIED_BY_DRM, /* The DRM system rejected the job. The job will
                                  never be accepted due to DRM configuration or
                                  job template settings. */

   /* ------------------------------- job control specific ---------------- */
   DRMAAJ_ERRNO_INVALID_JOB, /* The job specified by the 'jobid' does not
                                exist. */
   DRMAAJ_ERRNO_RESUME_INCONSISTENT_STATE, /* The job has not been suspended.
                                              The RESUME request will not be
                                              processed. */
   DRMAAJ_ERRNO_SUSPEND_INCONSISTENT_STATE, /* The job has not been running, and
                                               it cannot be suspended. */
   DRMAAJ_ERRNO_HOLD_INCONSISTENT_STATE, /* The job cannot be moved to a HOLD
                                           state. */
   DRMAAJ_ERRNO_RELEASE_INCONSISTENT_STATE, /* The job is not in a HOLD
                                               state. */
   DRMAAJ_ERRNO_EXIT_TIMEOUT, /* We have encountered a time-out condition for
                                 drmaa_synchronize or drmaa_wait. */
   DRMAAJ_ERRNO_NO_RUSAGE, /* This error code is returned by drmaa_wait() when a
                              job has finished but no rusage and stat data could
                              be provided. */
   DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE, /* This error code is returned when an
                                         invalid job template is passed to a
                                         function. */
   DRMAAJ_ERRNO_NULL_POINTER, /* This error code is used for
                                 NullPointerExceptions */
/* DRMAAJ_ERRNO_NO_MORE_ELEMENTS is not listed here because it is unused in the
 * Java language binding. */
   DRMAAJ_NO_ERRNO
};

#define NO_EXECEPTION_CLASS "Unable to locate class, %s, for DRMAA error: %s: %s"

static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
static drmaa_job_template_t **job_templates = NULL;
static int list_length = 0;

static void print_message_and_throw_exception(JNIEnv *env, int errnum,
                                              const char *format, ...);
static void throw_exception (JNIEnv *env, int errnum, const char *message);
static jclass get_exception_class(JNIEnv *env, int errnum, const char *message);
static char *get_exception_class_name (int errnum);
static drmaa_job_template_t *get_from_list (int id);
static int insert_into_list (drmaa_job_template_t *jt);

JNIEXPORT void JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeControl
  (JNIEnv *env, jobject object, jstring jobId, jint action)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   const char *job_id = NULL;
   
   if (jobId == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S, "job id");
      
      return;
   }
   
   job_id = (*env)->GetStringUTFChars(env, jobId, NULL);

   errnum = drmaa_control (job_id, action, error, DRMAA_ERROR_STRING_BUFFER);

   (*env)->ReleaseStringUTFChars(env, jobId, job_id);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   }  
}

JNIEXPORT void JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeExit
  (JNIEnv *env, jobject object)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   int count = 0;
   
   /* Free all job templates */
   pthread_mutex_lock(&list_mutex);
   
   for (count = 0; count < list_length; count++) {
      if (job_templates[count] != NULL) {
         errnum = drmaa_delete_job_template(job_templates[count], error,
                                            DRMAA_ERROR_STRING_BUFFER);
         
         if (errnum != DRMAAJ_ERRNO_SUCCESS) {
            pthread_mutex_unlock(&list_mutex);
            
            throw_exception(env, errnum, error);
            
            return;
         }
         
         job_templates[count] = NULL;
      }
   }
   
   pthread_mutex_unlock(&list_mutex);
   
   errnum = drmaa_exit (error, DRMAA_ERROR_STRING_BUFFER);

   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   }  
}

JNIEXPORT jstring JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeGetContact
  (JNIEnv *env, jobject object)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   char contact[DRMAA_CONTACT_BUFFER + 1];
   
   errnum = drmaa_get_contact (contact, DRMAA_CONTACT_BUFFER, error,
                              DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
      
      return NULL;
   }
   
   return (*env)->NewStringUTF (env, contact);
}

JNIEXPORT jstring JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeGetDRMSInfo
  (JNIEnv *env, jobject object)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   char system[DRMAA_DRM_SYSTEM_BUFFER + 1];
   
   errnum = drmaa_get_DRM_system (system, DRMAA_DRM_SYSTEM_BUFFER, error,
                                 DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
      
      return NULL;
   }
   
   return (*env)->NewStringUTF (env, system);
}

JNIEXPORT jint JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeGetJobProgramStatus
  (JNIEnv *env, jobject object, jstring jobId)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   int status = 0;
   const char *job_id = NULL;
   
   if (jobId == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S, "job id");
      
      return -1;
   }
   
   job_id = (*env)->GetStringUTFChars(env, jobId, NULL);

   errnum = drmaa_job_ps (job_id, &status, error, DRMAA_ERROR_STRING_BUFFER);

   (*env)->ReleaseStringUTFChars(env, jobId, job_id);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);

      return -1;
   }
  
   return status;
}

JNIEXPORT void JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeInit
  (JNIEnv *env, jobject object, jstring contactString)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   const char *contact = NULL;

   if (contactString != NULL) {
      contact = (*env)->GetStringUTFChars(env, contactString, NULL);
   }

   errnum = drmaa_init (contact, error, DRMAA_ERROR_STRING_BUFFER);

   if (contactString != NULL) {
      (*env)->ReleaseStringUTFChars(env, contactString, contact);
   }
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   }  
}

JNIEXPORT jobjectArray JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeRunBulkJobs
  (JNIEnv *env, jobject object, jint id, jint start, jint end, jint step)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   char buffer[DRMAA_JOBNAME_BUFFER + 1];
   drmaa_job_template_t *jt = NULL;
   drmaa_job_ids_t *ids = NULL;
   int num_elem = 0;
   int count = 0;
   jobjectArray ret_val = NULL;
   jclass clazz = NULL;
   jstring tmp_str = NULL;
   
   jt = get_from_list(id);
   
   if (jt == NULL) {
      print_message_and_throw_exception(env, DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE,
                                        MSG_JDRMAA_BAD_JOB_TEMPLATE);

      return NULL;
   }
   
   errnum = drmaa_run_bulk_jobs(&ids, jt, start, end, step, error,
                                DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception(env, errnum, error);
      drmaa_release_job_ids (ids);
      
      return NULL;
   }

   errnum = drmaa_get_num_job_ids(ids, &num_elem);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception(env, errnum, NULL);
      drmaa_release_job_ids (ids);
      
      return NULL;
   }
   
   clazz = (*env)->FindClass (env, "java/lang/String");
   ret_val = (*env)->NewObjectArray(env, num_elem, clazz, NULL);

   for (count = 0; count < num_elem; count++) {
      errnum = drmaa_get_next_job_id(ids, buffer, DRMAA_JOBNAME_BUFFER);
      
      if (errnum != DRMAAJ_ERRNO_SUCCESS) {
         throw_exception(env, errnum, "Reported incorrect number of job ids");
         drmaa_release_job_ids (ids);

         return NULL;
      }
      
      tmp_str = (*env)->NewStringUTF (env, buffer);
      (*env)->SetObjectArrayElement(env, ret_val, count, tmp_str);
   }

   drmaa_release_job_ids(ids);
   ids = NULL;
   
   return ret_val;
}

JNIEXPORT jstring JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeRunJob
  (JNIEnv *env, jobject object, jint id)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   char job_id[DRMAA_JOBNAME_BUFFER + 1];
   drmaa_job_template_t *jt = NULL;
   
   jt = get_from_list (id);
   
   if (jt == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE,
                                         MSG_JDRMAA_BAD_JOB_TEMPLATE);

      return NULL;
   }
   
   errnum = drmaa_run_job (job_id, DRMAA_JOBNAME_BUFFER, jt, error,
                          DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
      
      return NULL;
   }
   
   return (*env)->NewStringUTF (env, job_id);
}

JNIEXPORT void JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeSynchronize
  (JNIEnv *env, jobject object, jobjectArray ids, jlong timeout,
   jboolean dispose)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   const char **job_ids = NULL;
   jsize length = 0;
   jobject tmp_obj = NULL;
   jsize count = 0;

   if (ids == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S,
                                         "job ids list");
      
      return;
   }
   
   length = (*env)->GetArrayLength(env, ids);
   job_ids = (const char**)malloc ((length + 1) * sizeof (char *));
   
   for (count = 0; count < length; count++) {
      tmp_obj = (*env)->GetObjectArrayElement(env, ids, count);
      job_ids[count] = (*env)->GetStringUTFChars(env, (jstring)tmp_obj, NULL);
   }
   
   job_ids[count] = NULL;
   
   errnum = drmaa_synchronize (job_ids, (signed long)timeout, dispose, error,
                              DRMAA_ERROR_STRING_BUFFER);
   
   for (count = 0; count < length; count++) {
      tmp_obj = (*env)->GetObjectArrayElement(env, ids, count);
      (*env)->ReleaseStringUTFChars(env, (jstring)tmp_obj, job_ids[count]);
   }
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   }
}

JNIEXPORT jobject JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeWait
  (JNIEnv *env, jobject object, jstring jobId, jlong timeout)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   char buffer[DRMAA_JOBNAME_BUFFER + 1];
   char rbuffer[BUFFER_LENGTH + 1];
   char signal[DRMAA_SIGNAL_BUFFER + 1];
   const char *job_id = NULL;
   jobject job_info = NULL;
   jmethodID meth = NULL;
   jclass clazz = NULL;
   jobjectArray resources = NULL;
   int status = -1;
   drmaa_attr_values_t *rusage = NULL;
   jstring tmp_str = NULL;
   int signaled = 0;
   int count = 0;
   int length = 0;
   int has_resources = 1;
   
   if (jobId == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S, "job id");
      
      return NULL;
   }
   
   job_id = (*env)->GetStringUTFChars (env, jobId, NULL);
   
   errnum = drmaa_wait (job_id, buffer, DRMAA_JOBNAME_BUFFER, &status,
                        (signed long)timeout, &rusage, error,
                        DRMAA_ERROR_STRING_BUFFER);
   (*env)->ReleaseStringUTFChars (env, jobId, job_id);

   if (errnum == DRMAAJ_ERRNO_NO_RUSAGE) {
       has_resources = 0;
   } else if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
      drmaa_release_attr_values (rusage);   
   
      return NULL;
   }

   if (has_resources == 1) {
       errnum = drmaa_get_num_attr_values(rusage, &length);

       if (errnum != DRMAAJ_ERRNO_SUCCESS) {
          throw_exception(env, errnum, NULL);
          drmaa_release_attr_values (rusage);   

          return NULL;
       }

       clazz = (*env)->FindClass (env, "java/lang/String");
       resources = (*env)->NewObjectArray(env, length, clazz, NULL);

       for (count = 0; count < length; count++) {
          errnum = drmaa_get_next_attr_value (rusage, rbuffer, BUFFER_LENGTH);

          if (errnum != DRMAAJ_ERRNO_SUCCESS) {
             throw_exception (env, errnum, "Reported incorrect number of resource usage entries");
             drmaa_release_attr_values (rusage);   

             return NULL;
          }

          tmp_str = (*env)->NewStringUTF (env, rbuffer);
          (*env)->SetObjectArrayElement(env, resources, count, tmp_str);
       }

       drmaa_release_attr_values (rusage);
   }

   errnum = drmaa_wifsignaled (&signaled, status, error,
                              DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   
      return NULL;
   }
   else if (signaled != 0) {
      errnum = drmaa_wtermsig (signal, DRMAA_SIGNAL_BUFFER, status, error,
                              DRMAA_ERROR_STRING_BUFFER);

      if (errnum != DRMAAJ_ERRNO_SUCCESS) {
         throw_exception (env, errnum, error);

         return NULL;
      }
      
      tmp_str = (*env)->NewStringUTF (env, signal);
   }
   
   clazz = (*env)->FindClass (env, "com/sun/grid/drmaa/JobInfoImpl");
   meth = (*env)->GetMethodID (env, clazz, "<init>",
                 "(Ljava/lang/String;I[Ljava/lang/String;Ljava/lang/String;)V");
   job_info = (*env)->NewObject (env, clazz, meth,
                                 (*env)->NewStringUTF (env, buffer), status,
                                 resources, tmp_str);
   
   return job_info;
}

JNIEXPORT jint JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeAllocateJobTemplate
  (JNIEnv *env, jobject object)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   drmaa_job_template_t *jt = NULL;
   
   errnum = drmaa_allocate_job_template(&jt, error, DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
      return -1;
   }
   
   return insert_into_list (jt);
}

JNIEXPORT void JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeSetAttributeValue
  (JNIEnv *env, jobject object, jint id, jstring nameStr, jstring valueStr)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   drmaa_job_template_t *jt = get_from_list (id);
   const char *name = NULL;
   const char *value = NULL;
   
   if (jt == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE,
                                         MSG_JDRMAA_BAD_JOB_TEMPLATE);
      
      return;
   }
   
   if (nameStr == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S,
                                         "attribute name");
      
      return;
   }
   
   if (valueStr == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S,
                                         "attribute value");
      
      return;
   }
   
   name = (*env)->GetStringUTFChars (env, nameStr, NULL);
   value = (*env)->GetStringUTFChars (env, valueStr, NULL);
   
   errnum = drmaa_set_attribute (jt, name, value, error,
                                DRMAA_ERROR_STRING_BUFFER);

   (*env)->ReleaseStringUTFChars (env, nameStr, name);
   (*env)->ReleaseStringUTFChars (env, valueStr, value);

   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   
      return;
   }
}

JNIEXPORT void JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeSetAttributeValues
  (JNIEnv *env, jobject object, jint id, jstring nameStr, jobjectArray values)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   drmaa_job_template_t *jt = NULL;
   const char *name = NULL;
   const char **value = NULL;
   jsize length = 0;
   jobject tmp_obj = NULL;
   jsize count = 0;
   
   jt = get_from_list (id);
   
   if (jt == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE,
                                         MSG_JDRMAA_BAD_JOB_TEMPLATE);

      return;
   }

   if (nameStr == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S,
                                         "attribute name");
      
      return;
   }

   if (values == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S,
                                         "attribute names list");
      
      return;
   }
   
   length = (*env)->GetArrayLength(env, values);
   
   /* Get the strings out of the Strings. */
   name = (*env)->GetStringUTFChars (env, nameStr, NULL);
   value = (const char**)malloc ((length + 1) * sizeof (char *));
   
   for (count = 0; count < length; count++) {
      tmp_obj = (*env)->GetObjectArrayElement(env, values, count);
      value[count] = (*env)->GetStringUTFChars(env, (jstring)tmp_obj, NULL);
   }

   value[count] = NULL;
   
   errnum = drmaa_set_vector_attribute (jt, name, value, error,
                                       DRMAA_ERROR_STRING_BUFFER);

   /* Release the strings. */
   (*env)->ReleaseStringUTFChars (env, nameStr, name);

   for (count = 0; count < length; count++) {
      tmp_obj = (*env)->GetObjectArrayElement(env, values, count);
      (*env)->ReleaseStringUTFChars(env, (jstring)tmp_obj, value[count]);
   }

   free (value);
   value = NULL;
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   }
}

JNIEXPORT jobjectArray JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeGetAttributeNames
  (JNIEnv *env, jobject object, jint id)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   char buffer[BUFFER_LENGTH + 1];
   jobjectArray retval = NULL;
   jclass clazz = NULL;
   jstring tmp_str = NULL;
   drmaa_attr_names_t *names = NULL;
   drmaa_attr_names_t *vnames = NULL;
   int size = 0;
   int vsize = 0;
   int count = 0;
   
   errnum = drmaa_get_attribute_names (&names, error,
                                       DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
   
      return NULL;
   }
   
   errnum = drmaa_get_vector_attribute_names (&vnames, error,
                                             DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, error);
      drmaa_release_attr_names (names);
   
      return NULL;
   }
   
   errnum = drmaa_get_num_attr_names (names, &size);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, NULL);
      drmaa_release_attr_names (names);
      drmaa_release_attr_names (vnames);
   
      return NULL;
   }
   
   errnum = drmaa_get_num_attr_names (vnames, &vsize);
   
   if (errnum != DRMAAJ_ERRNO_SUCCESS) {
      throw_exception (env, errnum, NULL);
      drmaa_release_attr_names (names);
      drmaa_release_attr_names (vnames);
   
      return NULL;
   }
   
   clazz = (*env)->FindClass (env, "java/lang/String");
   retval = (*env)->NewObjectArray(env, size + vsize, clazz, NULL);

   for (count = 0; count < size; count++) {
      errnum = drmaa_get_next_attr_name(names, buffer, BUFFER_LENGTH);
      
      if (errnum != DRMAAJ_ERRNO_SUCCESS) {
         throw_exception (env, errnum, "Reported incorrect number of attribute names");
         drmaa_release_attr_names (names);
         drmaa_release_attr_names (vnames);

         return NULL;
      }

      tmp_str = (*env)->NewStringUTF (env, buffer);
      (*env)->SetObjectArrayElement(env, retval, count, tmp_str);
   }
   
   drmaa_release_attr_names (names);
   
   for (count = 0; count < vsize; count++) {
      errnum = drmaa_get_next_attr_name(vnames, buffer, BUFFER_LENGTH);
      
      if (errnum != DRMAAJ_ERRNO_SUCCESS) {
         throw_exception (env, errnum, "Reported incorrect number of attribute names");
         drmaa_release_attr_names (vnames);

         return NULL;
      }

      tmp_str = (*env)->NewStringUTF (env, buffer);
      (*env)->SetObjectArrayElement(env, retval, count + size, tmp_str);
   }
   
   drmaa_release_attr_names (vnames);
   
   return retval;
}

JNIEXPORT jobjectArray JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeGetAttribute
  (JNIEnv *env, jobject object, jint id, jstring name)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   drmaa_job_template_t *jt = get_from_list (id);
   jobjectArray retval = NULL;
   drmaa_attr_names_t *names = NULL;
   drmaa_attr_values_t *values = NULL;
   char buffer[BUFFER_LENGTH + 1];
   const char *name_str = NULL;
   bool is_vector = false;
   jclass clazz = NULL;
   jstring tmp_str = NULL;
   
   if (jt == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE,
                                         MSG_JDRMAA_BAD_JOB_TEMPLATE);

      return NULL;
   }

   if (name == NULL) {
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_NULL_POINTER,
                                         MSG_JDRMAA_NULL_POINTER_S,
                                         "attribute name");
      
      return NULL;
   }
   
   name_str = (*env)->GetStringUTFChars(env, name, NULL);
   
   errnum = drmaa_get_vector_attribute_names(&names, error,
                                             DRMAA_ERROR_STRING_BUFFER);
   
   if (errnum == DRMAAJ_ERRNO_SUCCESS) {
      while (drmaa_get_next_attr_name(names, buffer, BUFFER_LENGTH)
                                                      == DRMAAJ_ERRNO_SUCCESS) {
         if (strcmp (buffer, name_str) == 0) {
            is_vector = true;
            break;
         }
      }
   }
   else {
      (*env)->ReleaseStringUTFChars(env, name, name_str);
      throw_exception (env, errnum, error);
   
      return NULL;
   }
   
   drmaa_release_attr_names (names);
   
   if (is_vector) {
      errnum = drmaa_get_vector_attribute (jt, name_str, &values, error,
                                           DRMAA_ERROR_STRING_BUFFER);
      (*env)->ReleaseStringUTFChars(env, name, name_str);

      if (errnum == DRMAAJ_ERRNO_INVALID_ATTRIBUTE_VALUE) {
         return NULL;
      }
      else if(errnum != DRMAAJ_ERRNO_SUCCESS) {
         throw_exception (env, errnum, error);

         return NULL;
      }
      else {
         int count = 0;
         int size = 0;
         
         errnum = drmaa_get_num_attr_values(values, &size);
         
         if (errnum != DRMAAJ_ERRNO_SUCCESS) {
            throw_exception(env, errnum, NULL);
            drmaa_release_attr_values(values);
            
            return NULL;
         }
         
         clazz = (*env)->FindClass (env, "java/lang/String");
         retval = (*env)->NewObjectArray(env, size, clazz, NULL);

         for (count = 0; count < size; count++) {
            errnum = drmaa_get_next_attr_value(values, buffer, BUFFER_LENGTH);
            
            if (errnum != DRMAAJ_ERRNO_SUCCESS) {
               throw_exception(env, errnum, "Reported incorrect number of attribute value elements");
               drmaa_release_attr_values(values);

               return NULL;
            }
            
            tmp_str = (*env)->NewStringUTF (env, buffer);
            (*env)->SetObjectArrayElement(env, retval, count, tmp_str);
         }

         drmaa_release_attr_values (values);
      }
   }
   else {
      errnum = drmaa_get_attribute (jt, name_str, buffer, BUFFER_LENGTH, error,
                                   DRMAA_ERROR_STRING_BUFFER);
      (*env)->ReleaseStringUTFChars(env, name, name_str);
      
      if (errnum == DRMAAJ_ERRNO_INVALID_ATTRIBUTE_VALUE) {
         return NULL;
      }
      else if(errnum != DRMAAJ_ERRNO_SUCCESS) {
         throw_exception (env, errnum, error);

         return NULL;
      }
      else {
         clazz = (*env)->FindClass (env, "java/lang/String");   
         retval = (*env)->NewObjectArray(env, 1, clazz, NULL);
         tmp_str = (*env)->NewStringUTF (env, buffer);
         (*env)->SetObjectArrayElement(env, retval, 0, tmp_str);
      }
   }
   
   return retval;
}

JNIEXPORT void JNICALL Java_com_sun_grid_drmaa_SessionImpl_nativeDeleteJobTemplate
  (JNIEnv *env, jobject object, jint id)
{
   char error[DRMAA_ERROR_STRING_BUFFER + 1];
   int errnum = DRMAAJ_ERRNO_SUCCESS;
   drmaa_job_template_t *jt = NULL;
   
   pthread_mutex_lock(&list_mutex);
      
   if ((job_templates != NULL) && (id < list_length)) {
      jt = job_templates[id];
   }
   
   if (jt != NULL) {
      errnum = drmaa_delete_job_template (jt, error, DRMAA_ERROR_STRING_BUFFER);
      
      if (errnum != DRMAAJ_ERRNO_SUCCESS) {
         pthread_mutex_unlock(&list_mutex);
         
         throw_exception (env, errnum, error);

         return;
      }
      
      job_templates[id] = NULL;
   
      pthread_mutex_unlock(&list_mutex);
   }
   else {   
      pthread_mutex_unlock(&list_mutex);
      
      print_message_and_throw_exception (env, DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE,
                                         MSG_JDRMAA_BAD_JOB_TEMPLATE);

      return;
   }
}

static void print_message_and_throw_exception(JNIEnv *env, int errnum,
                                              const char *format, ...)
{
   char message[MAX_STRING_SIZE + 1];

   if (format != NULL) {
      va_list ap;

      va_start(ap, format);
      vsnprintf(message, MAX_STRING_SIZE, format, ap);
      va_end(ap);
      throw_exception(env, errnum, message);
   } else {
      throw_exception(env, errnum, NULL);
   }
}

static void throw_exception(JNIEnv *env, int errnum, const char *message)
{
   const char *error = message;
   jclass newExcCls = NULL;

   if (error == NULL) {
      error = drmaa_strerror(errnum);
   }

   newExcCls = get_exception_class(env, errnum, error);

   if (newExcCls != NULL) {
      (*env)->ThrowNew(env, newExcCls, error);
   }
}

static jclass get_exception_class(JNIEnv *env, int errnum, const char *message)
{
   jclass newExcCls = NULL;

   newExcCls = (*env)->FindClass(env, get_exception_class_name(errnum));

   /* If we can't find the exception class, throw a RuntimeException. */
   if (newExcCls == NULL) {
      char no_class_message[MAX_STRING_SIZE];

      /* If we can't find the right exception, default to something we
       * really expect to be able to find. */
      jclass runtime = (*env)->FindClass(env,
                                         "java/lang/ClassNotFoundException");

      /* If it's still not found, give up. */
      if (runtime == NULL) {
         fprintf (stderr, NO_EXECEPTION_CLASS,
                  get_exception_class_name (errnum), drmaa_strerror (errnum),
                  message);

         /* This if-else structure should now dump the thread of control out at
          * the end of the method.  Not doing so is an error. */
      }
      /* Otherwise, throw the Runtime exception. */
      else {
         snprintf (no_class_message, MAX_STRING_SIZE, NO_EXECEPTION_CLASS,
                   get_exception_class_name (errnum), drmaa_strerror (errnum),
                   message);

         /* Throw an exception saying we couldn't find the exception. */
         (*env)->ThrowNew(env, runtime, no_class_message);
      }
   }

   return newExcCls;
}

static char *get_exception_class_name (int errnum)
{
   switch (errnum) {
      case DRMAAJ_ERRNO_INTERNAL_ERROR:
         return "org/ggf/drmaa/InternalException";
      case DRMAAJ_ERRNO_DRM_COMMUNICATION_FAILURE:
         return "org/ggf/drmaa/DrmCommunicationException";
      case DRMAAJ_ERRNO_AUTH_FAILURE:
         return "org/ggf/drmaa/AuthorizationException";
      case DRMAAJ_ERRNO_INVALID_ARGUMENT:
         return "java/lang/IllegalArgumentException";
      case DRMAAJ_ERRNO_NO_ACTIVE_SESSION:
         return "org/ggf/drmaa/NoActiveSessionException";
      case DRMAAJ_ERRNO_NO_MEMORY:
         return "java/lang/OutOfMemoryError";
      case DRMAAJ_ERRNO_INVALID_CONTACT_STRING:
         return "org/ggf/drmaa/InvalidContactStringException";
      case DRMAAJ_ERRNO_DEFAULT_CONTACT_STRING_ERROR:
         return "org/ggf/drmaa/DefaultContactStringException";
      case DRMAAJ_ERRNO_NO_DEFAULT_CONTACT_STRING_SELECTED:
         return "org/ggf/drmaa/NoDefaultContactStringException";
      case DRMAAJ_ERRNO_DRMS_INIT_FAILED:
         return "org/ggf/drmaa/DrmsInitException";
      case DRMAAJ_ERRNO_ALREADY_ACTIVE_SESSION:
         return "org/ggf/drmaa/AlreadyActiveSessionException";
      case DRMAAJ_ERRNO_DRMS_EXIT_ERROR:
         return "org/ggf/drmaa/DrmsExitException";
      case DRMAAJ_ERRNO_INVALID_ATTRIBUTE_FORMAT:
         return "org/ggf/drmaa/InvalidAttributeFormatException";
      case DRMAAJ_ERRNO_INVALID_ATTRIBUTE_VALUE:
         return "org/ggf/drmaa/InvalidAttributeValueException";
      case DRMAAJ_ERRNO_CONFLICTING_ATTRIBUTE_VALUES:
         return "org/ggf/drmaa/ConflictingAttributeValuesException";
      case DRMAAJ_ERRNO_TRY_LATER:
         return "org/ggf/drmaa/TryLaterException";
      case DRMAAJ_ERRNO_DENIED_BY_DRM:
         return "org/ggf/drmaa/DeniedByDrmException";
      case DRMAAJ_ERRNO_INVALID_JOB:
         return "org/ggf/drmaa/InvalidJobException";
      case DRMAAJ_ERRNO_RESUME_INCONSISTENT_STATE:
         return "org/ggf/drmaa/ResumeInconsistentStateException";
      case DRMAAJ_ERRNO_SUSPEND_INCONSISTENT_STATE:
         return "org/ggf/drmaa/SuspendInconsistentStateException";
      case DRMAAJ_ERRNO_HOLD_INCONSISTENT_STATE:
         return "org/ggf/drmaa/HoldInconsistentStateException";
      case DRMAAJ_ERRNO_RELEASE_INCONSISTENT_STATE:
         return "org/ggf/drmaa/ReleaseInconsistentStateException";
      case DRMAAJ_ERRNO_EXIT_TIMEOUT:
         return "org/ggf/drmaa/ExitTimeoutException";
       case DRMAAJ_ERRNO_INVALID_JOB_TEMPLATE:
         return "org/ggf/drmaa/InvalidJobTemplateException";
      case DRMAAJ_ERRNO_NULL_POINTER:
         return "java/lang/NullPointerException";
      default:
         return "java/lang/RuntimeException";
   }
}

static int insert_into_list (drmaa_job_template_t *jt)
{
   int count = 0;
   drmaa_job_template_t **tmp_list = NULL;
   int tmp_length = 0;
   
   pthread_mutex_lock(&list_mutex);

   /* If we haven't initialized the template list yet, do so. */
   if (job_templates == NULL) {
      list_length = TEMPLATE_LIST_LENGTH;
      job_templates = (drmaa_job_template_t **)malloc
                                (sizeof (drmaa_job_template_t *) * list_length);
      memset (job_templates, 0, list_length * sizeof (drmaa_job_template_t *));
   }

   /* Search for an empty slot. */
   for (count = 0; count < list_length; count++) {
      if (job_templates[count] == NULL) {
         /* Insert the template and return the index. */
         job_templates[count] = jt;
   
         pthread_mutex_unlock(&list_mutex);

         return count;
      }
   }

   /* If there are no empty slots, double the size of the list. */
   tmp_length = list_length * 2;
   tmp_list = (drmaa_job_template_t **)malloc (sizeof(drmaa_job_template_t *) *
                                               tmp_length);
   memcpy (tmp_list, job_templates, list_length *
                                               sizeof (drmaa_job_template_t *));
   memset (&tmp_list[count], 0, list_length * sizeof (drmaa_job_template_t *));
   
   list_length = tmp_length;
   free (job_templates);
   job_templates = tmp_list;
   
   /* Insert the template and return the index. */
   job_templates[count] = jt;
   
   pthread_mutex_unlock(&list_mutex);
   
   return count;
}

static drmaa_job_template_t *get_from_list (int id)
{
   drmaa_job_template_t *retval = NULL;
   
   if (id >= 0) {
      pthread_mutex_lock(&list_mutex);

      if ((job_templates != NULL) && (id < list_length)) {
         retval = job_templates[id];
      }

      pthread_mutex_unlock(&list_mutex);
   }
   
   return retval;
}