Commits

Sébastien Ferré committed 93b19c2

[camljava/jni] robust to missing camljava.jar on CLASSPATH
- impact: callbacks are not available

Comments (0)

Files changed (2)

camljava/lib/jni.ml

 (*             OCamlJava: Objective Caml / Java interface              *)
 (*                                                                     *)
 (*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
+(*            modified by: Sebastien Ferre, IRISA/Rennes 1             *)
 (*                                                                     *)
 (*  Copyright 2001 Institut National de Recherche en Informatique et   *)
 (*  en Automatique.  All rights reserved.  This file is distributed    *)
 (*                                                                     *)
 (***********************************************************************)
 
-(* $Id: jni.mlp,v 1.4 2005-10-21 08:19:11 xleroy Exp $ *)
-
 (* Low-level Java interface (JNI level) *)
 
 external set_debug: bool -> unit = "camljava_set_debug"
     try
       Sys.getenv "CLASSPATH"
     with Not_found ->
-      failwith "undefined CLASSPATH: camljava.jar required" in
+      prerr_endline "warning: CLASSPATH is undefined";
+      "" in
   init path;
   at_exit shutdown
 
 
 let null = get_null()
 
-exception Null_pointer
-
-let _ = Callback.register_exception "camljava_null_pointer" Null_pointer
-
-exception Exception of obj
-
-let _ = Callback.register "camljava_raise_exception"
-          (fun obj -> raise (Exception obj))
-
-external register_natives: unit -> unit = "camljava_RegisterNatives"
-
-let _ = register_natives()
-
-let _ = Callback.register "Oo.new_method" Oo.new_method
-
 (* String operations *)
 
 external string_to_java: string -> obj = "camljava_MakeJavaString"
 
 (* Auxiliaries for Java->OCaml callbacks *)
 
+exception Null_pointer
+exception Exception of obj
+
+external register_natives: unit -> unit = "camljava_RegisterNatives"
+
+let _ =
+  try
+    Callback.register_exception "camljava_null_pointer" Null_pointer;
+    Callback.register "camljava_raise_exception" (fun obj -> raise (Exception obj));
+    register_natives();
+    Callback.register "Oo.new_method" Oo.new_method
+  with _ ->
+    prerr_endline "JNI warning: camljava.jar was not found in CLASSPATH"
+
 external wrap_caml_object : < .. > -> int64 = "camljava_WrapCamlObject"
 
 let callback_class =
-  find_class "fr/inria/caml/camljava/Callback"
+  lazy (find_class "fr/inria/caml/camljava/Callback")
 let callback_init =
-  get_methodID callback_class "<init>" "(J)V"
+  lazy (get_methodID (Lazy.force callback_class) "<init>" "(J)V")
 let wrap_object camlobj =
-  let javaobj = alloc_object callback_class in
-  call_nonvirtual_void_method javaobj callback_class callback_init
+  let javaobj = alloc_object (Lazy.force callback_class) in
+  call_nonvirtual_void_method javaobj (Lazy.force callback_class) (Lazy.force callback_init)
                               [|Long (wrap_caml_object camlobj)|];
   javaobj
 OCAMLLEX=ocamllex
 OCAMLC=$(OCAMLFIND) ocamlc
 OCAMLOPT=$(OCAMLFIND) ocamlopt
-INCLUDES= -package dbm           # all relevant -I options here
+INCLUDES= -package dbm -I camljava/lib          # all relevant -I options here
 #OCAMLFLAGS= -pp "camlp4o pa_op.cmo" -thread $(INCLUDES)    # add other options for ocamlc here
 OCAMLFLAGS= -pp camlp4o -thread $(INCLUDES)    # add other options for ocamlc here
 
 .SUFFIXES: .mll .ml .mli .cmo .cmi
 
 # Make all
-all: common.cmo threads_common.cmo unicode.cmo bintree.cmo lSet.cmo cis.cmi cis.cmo iterator.cmo intmap.cmo intset.cmo intrel.cmo intrel2.cmo intreln.cmo text_index.cmo setset.cmo cache.cmi cache.cmo ext.cmo suffix_tree.cmo index.cmo seq.cmo persindex.cmo genid.cmo persintset.cmo persintset.cmo stringset.cmo seqset.cmo
+all: common.cmo threads_common.cmo unicode.cmo bintree.cmo lSet.cmo cis.cmi cis.cmo iterator.cmo intmap.cmo intset.cmo intrel.cmo intrel2.cmo intreln.cmo text_index.cmo setset.cmo cache.cmi cache.cmo ext.cmo suffix_tree.cmo index.cmo seq.cmo persindex.cmo genid.cmo persintset.cmo persintset.cmo stringset.cmo seqset.cmo jni_common.cmo
 	echo
 
 # archiving