Using CobolBean for Instance Data

The com.microfocus.cobol.CobolBean class enables you to associate an instance of CobolBean to an instance of a COBOL program's storage section without having to recode the COBOL application to either be thread-safe or take the parameters from the Java application.

For example, the following COBOL program shares the same data when it is called using cobcall() by more than one Java class.

$set intlevel"4" data-context
 working-storage section.
 01 address-rec pic x(30). 

 linkage section.
 01 lnk-address-rec pic x(30).

 procedure division.
    goback. 
 entry "setAddressBook" using lnk-address-rec.
    move lnk-address-rec to address-rec
    exit program returning 0. 
  entry "getAddressBook" using lnk-address-rec.
    move address-rec to lnk-address-rec
    exit program returning 0.

You can create a Java class that extends from com.microfocus.cobol.CobolBean, and this makes an instance (data-context) version of cobcall() available. You need to compile any COBOL programs called from CobolBean.cobcall() with the Compiler directive DATA-CONTEXT rather than using any threading directive such as RE-ENTRANT(1/2), SERIAL. The Compiler directive DATA-CONTEXT informs the COBOL run-time system to allocate a new COBOL Working-Storage Section whenever it is being used from CobolBean.cobcall().

In the following example, if the COBOL program shared data, bean1.getAddress() would be the same as bean2.getAddress(), causing problems. However because the Java program uses the cobcall() from CobolBean, the COBOL Working-Storage Section is associated with the bean.

import com.microfocus.cobol.*;
import com.microfocus.cobol.lang.*;

public class MyBean extends com.microfocus.cobol.CobolBean
{
   private StringBuffer address = new StringBuffer(30);

   public MyBean() throws Exception
   {
     super();
     super.cobload("addbook");
   }

   public String getAddress() throws Exception
   {
     Pointer addressPointer = 
	       new Pointer(this.address.toString(),30);    
     super.cobcall("getAddressBook", 
	       new ParameterList().add(addressPointer));
     this.address.setLength(0);
     this.address.append(addressPointer.toString());
     return address.toString();
   }

   public void setAddress(String address) throws Exception
   {
     super.cobcall("setAddressBook", 
	       new ParameterList().add(new Pointer(address,30)));
   }

   public static void main(String[] args) throws Exception
   {
      MyBean bean1 =new MyBean();
      bean1.setAddress("Mr A");

      MyBean bean2 =new MyBean();
      bean2.setAddress("Mr B");

      System.out.println("bean1.getAddress="+
		                    bean1.getAddress());
      System.out.println("bean2.getAddress="+
		                    bean2.getAddress());
   }
}