The following example illustrates how you can expose a procedural program to JVM COBOL applications by wrapping the program in a JVM COBOL wrapper class first. You then let other managed applications talk to the wrapper class using Object-Oriented (OO) syntax. The wrapper class, in its turn, talks to the procedural program.
There are different ways to write wrapper classes and this example demonstrates two of them.
You are going to use a simple procedural COBOL program, a calculator, which performs the basic arithmetic operations. You will use Enterprise Developer to create a JVM COBOL project to hold the code of the procedural program and a JVM COBOL wrapper class for the procedural code. You will also create a project for a JVM COBOL front-end application that will communicate with the procedural module using the wrapper class.
First, you need to create a JVM COBOL project to hold the procedural program and the wrapper class:
This creates a calclib project in your workspace. The project does not include any COBOL programs or class files.
This opens the New COBOL Program wizard, which will create the program in the src subfolder of the project, and will add it to the default package.
This adds the new program to the project and opens it in the editor.
program-id. Calc. data division. working-storage section. linkage section. 01 operands. 03 op1 pic 9(4) comp-5. 03 op2 pic 9(4) comp-5. 01 the-result pic 9(8) comp-5. 01 func-code pic x. 78 add-op value '+'. 78 sub-op value '-'. 78 mult-op value 'x'. 78 div-op value '/'. procedure division using by reference func-code operands the-result. evaluate func-code when add-op add op1 to op2 giving the-result when sub-op subtract op2 from op1 giving the-result when mult-op multiply op1 by op2 giving the-result when div-op divide op1 by op2 giving the-result end-evaluate. goback. end program Calc.
By default, Eclipse is set to build projects automatically so saving your changes starts a build. Your project should compile without any errors.
You are now going to create one of the wrapper classes for the procedural program:
This starts the New COBOL JVM Class wizard. You are going to create a public class in the default package that will be saved in the src subfolder of the project.
This adds the file to the project and opens it in the editor.
class-id calclib.WrapperClass. working-storage section. method-id Add. local-storage section. 01 operands. 03 op1 pic 9(4) comp-5. 03 op2 pic 9(4) comp-5. linkage section. 01 the-result pic 9(8) comp-5. procedure division using by value p1 as binary-short p2 as binary-short returning the-result. move p1 to op1 move p2 to op2 call "calc" using '+' operands the-result goback. end method. end class.
WrapperClass defines a method, Add, which uses the same comp-5 data items which are used in the procedural code program. The method maps op1 and op2 to the variables p1 and p2 which are of the equivalent type in managed code. The method then calls the procedural program to perform the addition operation.
Other managed languages can invoke the Add method using the OO syntax in order to access the functionality of the original procedural program.
You are now going to create a slightly different wrapper class that will use a Data Transfer Object (DTO) also known as a property class. You are going to expose the data items in the original program as properties of the wrapper class in order to pass them to other managed languages.
class-id calclib.ClassDTO. working-storage section. 01 operands property all elementary. 03 op1 pic 9(4) comp-5. 03 op2 pic 9(4) comp-5. method-id Add. linkage section. 01 the-result pic 9(8) comp-5. procedure division returning the-result. call "calc" using '+' operands the-result goback. end method. end class.
The use of the property all elementary on operands means that all elementary group items are exposed as properties of ClassDTO and can be accessed by other managed programs using the OO syntax (for example, ClassDTO::op1 if accessed by other JVM COBOL programs).
ClassDTO defines a similar method Add which calls the procedural program to perform the arithmetic operations.
An alternative of using the property keyword on the entire group item would be to set it on each individual data item as follows:
01 operands. 03 op1 pic 9(4) comp-5 property as "Operand1". 03 op2 pic 9(4) comp-5 property as "Operand2".
This would expose only the individual data items as properties of the class under new names, Operand1 and Operand2.
You are now going to create a project for a front-end application written in JVM COBOL that will communicate with both wrapper classes:
program-id. Program1 as "OOCalc.Program1". data division. working-storage section. 01 calc1 type calclib.WrapperClass. 01 the-result pic 9(8) comp-5. 01 calc2 type calclib.ClassDTO. procedure division. set calc1 to new type calclib.WrapperClass set the-result to calc1::Add(2, 2) set calc2 to new calclib.ClassDTO set calc2::op1 to 5 set calc2::op2 to 4 set the-result to calc2::Add goback. end program Program1.
The automatic build fails to compile the code as it does not recognize the classes calclib.ClassDTO and calclib.WrapperClass from the calclib project. To resolve this, you need to add the calclib project to the JVM build path for the OOCalc project.
As calclib is now on the JVM build path for OOCalc, the code in OOCalc finds the classes defined in calclib and the subsequent build is successful.
Program1.cbl defines two objects - calc1 of type of WrapperClass and calc2 of type ClassDTO.
During the program execution, the code creates instances of calc1 and calc2. Then, the example code demonstrates how to create an instance of calc1 and invoke the Add method in WrapperClass to perform the addition operation.
Next, an instance of calc2 is created and it accesses op1 and op2 that are exposed as properties of ClassDTO to assign them values. Then, the Add method of ClassDTO is invoked to perform the addition.