Calling Java From OO COBOL
Calling Java From OO COBOL
domain.
Overview
Micro Focus Java support enables you to send messages to Java objects from OO COBOL programs and classes. Java is supported through a Java domain in OO COBOL. The Java domain enables you to declare Java classes inside a COBOL program. You can then send messages to the Java classes. The Java domain support works by creating a COBOL proxy object for each Java object, as shown in Figure 10-1. The class itself that you declare is a proxy for the static methods of the Java class.
Figure 10-1: The Java proxy Your program needs to have a Class-Control Section, and use the INVOKE verb each time you want to call a Java method, although it does not have to be written as an OO COBOL class. The COBOL run-time system converts parameters sent with messages from COBOL data types to Java data types. If a method returns a parameter, it is converted from a Java data type to a COBOL data type. Using the techniques described in this chapter, you can call any Java API, such as JDBC, Sockets and Swing. You need to have at least a basic knowledge of the Java language to be able to use this technology effectively. Sun's Java site is a good starting place. You can also call COBOL programs from Java without using the OO COBOL Java domain. This is described in the chapter Calling Procedural COBOL from Java. You can also send messages from Java classes to COBOL. This is covered in the chapter Calling COBOL from Java.
where:
COBOL-classname java class-name
is the name by which the class is known to this COBOL program. is the name of the Java domain. is the name by which the class is known to the Java domain. This is usually determined by the name under which the class has been registered with the object model for that domain.
For example:
repository. class jRectangle as "$java$java.awt.Rectangle" .
This declares jRectangle as a COBOL proxy object for the Rectangle class in the java.awt package. The package must be available on your Java classpath or your program will fail at run-time.
http://supportline.microfocus.com/documentation/books/nx40/dijafc.htm
Each Java class has one or more constructor methods to instantiate objects. In Java, constructor methods have the same name as the class. To enable you to invoke these methods from COBOL, they are mapped to the "new" method name on the COBOL proxy object. The different constructors on a Java class take different numbers and combinations of parameters to initialize the instance you are creating. For example, the Java Rectangle class can be instantiated in several different ways, including the two shown below in Java code:
Rectangle r1 = new Rectangle () // rectangle (x,y) = 0,0, width=0, height=0 Rectangle r2 = new Rectangle(4, 5, 10, 20) // rectangle (x,y) = (4,5), width=10, height=20
The COBOL run-time system uses the number and type of parameters to call the appropriate constructor on the Java class. You must be careful to provide parameters of the types expected by the Java class, as Java is a strongly typed language. The chapter Java Data Types explains how COBOL data types are mapped on to Java data types. The copyfile javatypes.cpy also defines a set of data types for use in COBOL which correspond directly to Java data types; you are recommended to use these for transferring data between Java and COBOL.
http://supportline.microfocus.com/documentation/books/nx40/dijafc.htm
This sample of COBOL code sets the static variable classVal, and then retrieves the member instVal.
$set ooctrl(-f+p) repository. class x as "$Java$x" . working-storage section. copy "javatypes.cpy". 01 anX object reference. 01 anInt jint. procedure division. invoke x "setclassVal" using by value 4 invoke x "new" returning anX invoke anX "getinstVal" returning anInt
2. Write an exception handler (either a method in a class, or an entry-point) and create a Callback or EntryCallback to it. For example, a Callback looks like this:
invoke Callback "new" using anObject z"methodName" returning aHandler
Now all Java exceptions thrown by classes you are calling through the OO COBOL Java domain get sent to your exception handler. This is an example of a COBOL program which catches an exception thrown by a Java program:
$set ooctrl (+p-f) program-id. ExceptionCatcher. class-control.
http://supportline.microfocus.com/documentation/books/nx40/dijafc.htm
JavaExceptionManager is class "javaexpt" ExceptionManager is class "exptnmgr" . working-storage section. 01 theInstance object 01 wsCallback object local-storage section. 01 filler pic x. *> dummy storage to *> point to be used linkage section. 01 lnkException object
reference. reference. allow the local entry for the callback reference.
procedure division. *>---Set up Exception handler invoke EntryCallback "new" using z"JException" returning wsCallback invoke ExceptionManager "register" using javaexceptionmanager wsCallback *>---Instantiate the class invoke SimpleClass "new" returning theInstance display "instantiated" invoke theInstance "TestException" display "excepted" stop run.
The Local-Storage Section in the above program is simply to allow recursion - the COBOL run-time system treats invoking the EntryCallback as a recursive call. Without a Local-Storage Section, this will result in a run-time system error. This is the Java code for SimpleClass:
import java.lang.* ; public class SimpleClass { public SimpleClass() { } public void TestException() throws Exception { Exception e = new Exception ("Test error" ); throw e; } }
*> procedure division. invoke javasup "getEnv" returning jEnv *> Map the pointer passed in JEnv to the *> JNINativeInterface structure so that we *> can call JNI functions.
http://supportline.microfocus.com/documentation/books/nx40/dijafc.htm
You can now call JNI functions using the names provided by the JNINativeInterface type definition. You can see an example of this in the section Example of Throwing an Exception in the chapter Calling Procedural COBOL from Java. For more information on JNI, see Sun's Java site. For more information on the javasup class, see your Class Library Reference.
The returning parameter is important, as otherwise the COBOL run-time system passes the message on to the actual Java object instead of destroying the COBOL proxy. If the message is passed to the Java object, it invokes this function:
public void finalize()
This function is either inherited or implemented by all Java classes, and is called by the Java garbage collector before it destroys an object. In the unlikely event that your Java class implements a finalize() method which returns a value, use the "delete" method to destroy the OO COBOL proxy instead:
invoke javaobject "delete" returning javaobject
Copyright 2003 Micro Focus International Limited. All rights reserved. This document and the proprietary marks and names used herein are protected by international law.
http://supportline.microfocus.com/documentation/books/nx40/dijafc.htm