OO Cobol in z/OS - Columbus State University
Download
Report
Transcript OO Cobol in z/OS - Columbus State University
OO Cobol in z/OS
Enterprise Cobol Features
• Allows you to define classes with methods
and in Cobol
• Create instances of Java and Cobol
classes
• Invoke methods on Java and Cobol
objects
• Write classes that inherit from Java or
Cobol classes
• Overload methods
Java Interoperability
• OO Cobol programs must be compiled
with the THREAD option in order to work
with Java programs
• Cobol object instances are freed by the
Java runtime system GC
JNI Support
• Basic Java features are accessed using
Cobol syntax
• In Enterprise Cobol you can also invoke
the Java Native Interface (JNI) to obtain
other Java-oriented capabilities
• For example, converting PIC X type data
to Java String objects
Wrapper Classes
• Legacy procedural Cobol code can be
wrapped as a Cobol class to allow existing
code to be called from Java
• Java code can create instances of Cobol
classes and invoke Cobol class methods
• Java code can extend Cobol classes
Restrictions
• Cobol class definitions can’t contain EXEC
SQL statements
• Cobol class definitions can’t contain EXEC
CICS statements
A Cobol Driver to Invoke Java
cbl dll,thread
Identification division.
Program-id. "tstDoor" recursive.
Environment division.
Configuration section.
Repository.
class Door is "Door".
Data Division.
Procedure Division.
DISPLAY "COBOL PROGRAM tstDoor ENTERED"
INVOKE Door "doSomething"
DISPLAY "RETURNED FROM JAVA Door TO tstDoor“
GOBACK.
END PROGRAM "tstDoor".
Defining a Cobol Class
Identification division
Environment division (configuration section only)
Factory definition
– Identification division
– Data division
– Procedure division (containing one or more method
definitions)
Object definition
– Identification division
– Data division
– Procedure division (containing one or more method
definitions)
Creating a Java Class in UNIX
1) Use TSO OMVS to move to UNIX
2) Build a directory for the class (cd, mkdir)
3) Save an env file in the Directory (see
env panel that follows)
4) Use TSO OEDIT to create a .java file in
the directory
5) Compile the .java file with javac. Make
sure you allocate enough space when
you sign on to TSO
Door.java
public class Door
{
private String state;
public Door()
{
System.out.println("building a closed door...");
state = "closed";
}
public Door(String s)
{
System.out.println("building the door...");
if (s.equals("open"))
{
state = "open";
}
else
{
state = "closed";
}
}
Door.java
public void open()
{
System.out.println("About to open the door...");
if (state.equals("open"))
{
System.out.println("The door is already open.");
}
else
{
state = "open";
}
}
public void close()
{
System.out.println("About to close the door...");
if (state.equals("closed"))
{
Door.java
System.out.println("The door is already closed.");
}
else
{
state = "closed";
}
}
public String toString()
{
return "The door is " + state + ".";
}
public static void doSomething()
{
System.out.println("I did something.");
}
public void print()
{
System.out.println("The door is " + state + ".");
}
}
An env File
PATH=/bin:/u/Java5_31/J5.0/bin.
LIBPATH=/lib:/usr/lib:/u/Java5_31/J5.0/bin:/u/Java5_
31/J5.0/bin/j9vm
CLASSPATH=/u/csup004/Door
The CLASSPATH should point to the
directories that contains your .class files
Compile, Link, Run JCL
//CSUP004O JOB 'WOOLBRIGHT',CLASS=A,MSGCLASS=A,TIME=(0,5),
//
LINES=5,MSGLEVEL=(1,1),NOTIFY=&SYSUID,REGION=0M
// SET COBPRFX='IGY410'
// SET LIBPRFX='CEE'
//*
//COMPILE EXEC PGM=IGYCRCTL,
// PARM='SIZE(5000K),LIB'
//SYSLIN DD DSNAME=&&OBJECT(TSTDOOR),UNIT=SYSDA,DISP=(NEW,PASS),
//
SPACE=(CYL,(1,1,1))
//SYSPRINT DD SYSOUT=*
//STEPLIB DD DSN=&COBPRFX..SIGYCOMP,DISP=SHR
//
DD DSN=&LIBPRFX..SCEERUN,DISP=SHR
//SYSUT1
DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT2
DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT3
DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT4
DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT5
DD UNIT=SYSDA,SPACE=(CYL,(1,1))
Compile, Link, Run JCL
//SYSUT6
DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT7
DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSIN
DD *
cbl dll,thread
Identification division.
Program-id. "tstDoor" recursive.
Environment division.
Configuration section.
Repository.
class Door is "Door".
Data Division.
Procedure Division.
DISPLAY "COBOL PROGRAM tstDoor ENTERED"
INVOKE Door "doSomething"
DISPLAY "RETURNED FROM JAVA Door TO tstDoor"
Compile, Link, Run JCL
DISPLAY "RETURNED FROM JAVA Door TO tstDoor"
GOBACK.
END PROGRAM "tstDoor".
/*
//LKED EXEC PGM=IEWL,
//
PARM='RENT,LIST,LET,DYNAM(DLL),CASE(MIXED)'
//SYSLIB
DD DSN=&LIBPRFX..SCEELKED,DISP=SHR
//
DD DSN=&LIBPRFX..SCEELKEX,DISP=SHR
//SYSTERM DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
//SYSLMOD DD DSN=&&GOSET(TSTDOOR),DISP=(MOD,PASS),
// UNIT=SYSALLDA,SPACE=(CYL,(1,1,1)),DSNTYPE=LIBRARY
//SYSDEFSD DD DUMMY
//OBJMOD
DD DSN=&&OBJECT,DISP=(OLD,DELETE)
//SYSLIN
DD *
INCLUDE OBJMOD(TSTDOOR)
INCLUDE '/u/Java5_31/J5.0/bin/j9vm/libjvm.x'
INCLUDE '/usr/lpp/cobol/lib/igzcjava.x'
/*
Compile, Link, Run JCL
//GO EXEC PGM=TSTDOOR,
//
PARM='/ENVAR("_CEE_ENVFILE=/u/csup004/env")XPLINK(ON)
//
POSIX(ON)'
//*
POSIX(ON)'
//*STEPLIB DD DSN=CSUP004.LOAD,DISP=SHR
//STEPLIB DD DSN=*.LKED.SYSLMOD,DISP=SHR
//
DD DSN=&LIBPRFX..SCEERUN2,DISP=SHR
//
DD DSN=&LIBPRFX..SCEERUN,DISP=SHR
//SYSOUT
DD SYSOUT=*
//CEEDUMP DD SYSOUT=*
//SYSUDUMP DD DUMMY
//JAVAOUT DD PATH='/u/csup004/javaout',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR,SIRGRP)
//
Sample Compile, Link, Run JCL
• CSU.PUBLIC.OO(OOMAIN1) OR
CSU.PUBLIC.OO(CJSTATIC)
INVOKE
INVOKE NEW
• Use INVOKE class NEW to instantiate
an object
• The returned reference must be saved in
an object reference variable
01 myObj object reference DOOR.
…
INVOKE Door NEW
RETURNING myObj
INVOKE NEW
• The constructor call based on the pattern of
parameters that are passed.
INVOKE Door NEW
using by value jstring1
returning myDoor
01 jstring1 object reference jstring.
INVOKE A STATIC METHOD
INVOKE Door "doSomething "
• Door is the class.
• “doSomething” is the static method in Door
INVOKING INSTANCE METHODS
01 myObj object reference Door.
================================
INVOKE Door NEW
RETURNING myObj
INVOKE myObj "open"
INVOKE myObj "print"
• Class Door contains instance methods
“open” and “print” which have no parms
Interoperable Types
Interoperable Arrays and Strings
Passing Parms, Returning Values
working-storage Section.
01 myObj object reference Door.
01 intval10
pic s9(9) BINARY value 10.
01 intval20
pic s9(9) BINARY value 20.
01 intval30
pic s9(9) BINARY value 0.
Procedure Division.
DISPLAY "COBOL PROGRAM tstCalc ENTERED"
INVOKE Calculator NEW
using by value intval10
returning myObj
INVOKE myObj "add"
using by value intval20
returning intval30
USING Parameters in INVOKE
• Pass parameters BY VALUE when using
INVOKE
• The BY VALUE phrase specifies that the value
of the argument is passed, not a reference to the
sending data item. The invoked method can
modify the formal parameter that corresponds to
an argument passed by value, but changes do
not affect the argument because the invoked
method has access only to a temporary copy of
the sending data item.
Returning Constraints
If the RETURNING item is not an object reference, the
following rules apply:
– If the target method is implemented in COBOL, the
returning item in the INVOKE statement and the
RETURNING item in the target method must have an
identical data description entry.
– If the target method is implemented in Java, the returning
item in the INVOKE statement must correspond to the
Java type of the method result, as described in
“Interoperable Types”
Returning Constraints
If the RETURNING item is an object
reference, the RETURNING item specified
in the INVOKE statement must be an
object reference typed exactly to the class
of the returning item specified in the target
method. That is, the class specified in the
returning clause must not be a subclass or
a superclass of the class of the returning
item in the target method.
OO Exercise #1
• In UNIX, build a Java class called Point that stores a
pair of integers (x and y coordinates). Put in the normal
things that Java Classes should have (Constructor,
accessors, mutators, toString)
• Build a second Java class called PointStuff that has
a static method called distance which is passed two
Point objects. The method computes the distance
between the two points and returns the distance
√((x1-x2)2 –(y1-y2)2.
• Write a Cobol program that creates two Point objects
and passes them to distance. Print out the result the
method returns.
Building Cobol Classes
IDENTIFICATION DIVISION.
CLASS-ID. Account inherits Base.
The class ID names the class you are
defining. Base represents
java.lang.Object and is referenced in the
Repository.
Repository Paragraph
Environment Division.
Configuration Section.
Repository.
Class Base is “java.lang.Object”
Class Account is “Account”.
• The Repository is used to relate the internal
class names to external class names.
• The Repository has rules for generating external
names if they are omitted.
• Extenal names can include fully qualified
package names.
Object Paragraph
Identification Division
Object.
… data and methods
End Object.
• The Object paragraph is used to define the
instance data and methods of the class.
• The Object paragraph is immediately preceded
by an Identification Division header. (Different
from the one that starts the class.)
Working Storage in Object
Identification Division
Object.
Data Division.
Working Storage Section.
01 balance pic s9(6) value 0.
…
End Object.
Working Storage in Object
• Working Storage fields become instance
data for Cobol objects.
• Instance fields are initialized with value
clauses in Working Storage
• Fields are allocated when the object is
instantiated
• Fields exist until the object is garbage
collected by the Java run time
Working Storage
• To initialize more complicated objects, you need
to write special initialization methods in a
Factory
• Essentially, there is one constructor for Cobol
objects
• Fields are equivalent to Java private nonstatic
member data
• No other class or subclass can reference Cobol
instance data directly
• Provide sets and gets to make fields accessible
Instance Methods in Object
•
•
Instance methods are defined in the
Procedure Division of the Object
paragraph
Each method has four divisions followed
by END METHOD marker
Instance Method Divisions
Identification Division.
Method-id. “init”
Environment Division
Input-Output Section.
File-Control.
Select...myFile assign to MYFILE
… used to relate filenames used in
method to external names (optional)
Instance Method Divisions
Data Division.
File Section.
…used to define a file record
layout
Local-Storage Section.
…used to define local variables
which are allocated upon entry and
freed upon return. Put a value
clause to initialize a variable upon
entry
Instance Method Divisions
Data Division.
…
Working-Storage Section.
… One copy of this storage is
allocated at runtime. Data
persists in its last used
state. One copy exists for all
threads.
Instance Method Divisions
Data Division.
…
Linkage Section.
… Used to describe parameters
and fields that are returned
from the method.
Instance Method Divisions
Procedure Division.
… used to define the executable code for the method
You may not code:
ENTRY, EXIT, ALTER, GOTO, SEGMENT-LIMIT,
USE FOR DEBUGGING
SORT or MERGE
You may code:
EXIT METHOD
GOBACK
Passing Parameters
• Specifiy parms in the division header.
• Parms must be passed by value.
• Describe each parm in Linkage as 01 or 77 level
items
• Data types must be interoperable with Java
Linkage Section.
01 X pic s9(9) binary.
01 Y pic s9(9) binary.
Procedure Division using
by value X, Y
Returning a Parm
• A method can return a value. This is
evident when the method is invoked:
INVOKE Calculator “add”
using by value X Y
returning Total
Variables that are returned must be
described as 01 or 77 items in the Linkage
Section
OO Exercise #2
• Build a Calculator class that has a single private
variable “regValue” that will hold an integer (Pic
S9(9) Binary).
• Build an “setRegValue” method that changes the
stored value
• Build a “getRegValue” method that returns the
stored value
• Build an “add” method that passes an integer,
adds it to the stored value and returns the sum
• Build a “print” method that prints the object