Previous Topic Next topic Print topic


Example of Using a Managed COBOL Wrapper Class

The following example illustrates how you can expose a procedural program to managed applications by wrapping the program in a managed 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 two of them.

You are going to use a simple procedural COBOL program, a calculator, which performs the basic arithmetic operations. You will use Visual COBOL to create a managed project to hold the code of the procedural program and a managed COBOL wrapper class for the procedural code. You will also create a project for a managed COBOL front-end application that will communicate with the procedural module using the wrapper class.

Create a managed project

First, you need to create a managed COBOL project to hold the procedural program and the wrapper class:

  1. In Eclipse, create a managed COBOL project - click File > New > COBOL JVM Project.
  2. Specify a name such as calclib, and click Finish.

    This creates a calclib project in your workspace. The project does not include any COBOL programs or class files.

  3. Add a program to hold your procedural code - right-click the project, and click New > COBOL Program.

    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.

  4. Specify Calc.cbl in the Name field, and click Finish.

    This adds the new program to the project and opens it in the editor.

  5. Replace the code in the program with the following:
           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.
    
  6. Click File > Save to save the change.

    By default, Eclipse is set to build projects automatically so saving your changes starts a build. Your project should compile without any errors.

Create wrapper classes

You are now going to create one of the wrapper classes for the procedural program:

  1. Add a new managed COBOL class to the project - right-click the project, and click New > COBOL JVM Class.

    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.

  2. Specify a name such as WrapperClass, and click Finish.

    This adds the file to the project and opens it in the editor.

  3. Replace the code in WrapperClass with the following and save the file:
           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.

  4. The methods for the other three arithmetic operations in the procedural program are not included in this topic so you can write these now.

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.

  1. Add a new COBOL JVM class, ClassDTO, to the project and paste the following code to it:
           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 managed 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.

  2. You can now write the methods for the other arithmetic operations in the procedural program.

Write a managed front-end application in COBOL

You are now going to create a project for a front-end application written in managed COBOL that will communicate with both wrapper classes:

  1. Create a new COBOL JVM project - click New > COBOL JVM Project.
  2. Specify a name such as OOCalc, and then click Finish.
  3. Add a new COBOL program to the project, Program1.cbl, and add to it the managed COBOL code that will interact with the wrapper classes in the calclib project:
           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.

  4. Right-click OOCalc, and click Properties.
  5. Click Micro Focus > JVM Build Path.
  6. Click the Projects tab, and then click Add.
  7. Enable the checkbox in front of calclib, and then click OK.

    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.

Previous Topic Next topic Print topic