Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm...

37
Application Development A Cocktail of Java and MCP MCP Guru Series Dan Meyer & Pramod Nair

Transcript of Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm...

Page 1: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

Application Development A Cocktail of Java and MCP

MCP Guru Series Dan Meyer & Pramod Nair

Page 2: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 2

Agenda

Which of the following topics can be found in an Application Development cocktail?

o Calling Java from COBOL-85

o Using Java7 NIO2 features

o Accessing DMSII from Java

o All of the above

o Some of the above

o None of the above

Page 3: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

“A Cocktail of Java and MCP”

Calling Java from COBOL85

Page 4: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 4

Application Development

• “Wouldn’t It Be Nice” by the Beach Boys

“Wouldn't it be nice if we were older Then we wouldn't have to wait so long Wouldn't it be nice to live together In the kind of world where we belong You know its gonna make it that much better When we can say goodnight and stay together”

• Wouldn’t it be nice if:

– Your favorite programming language could be used for everything?

– You could find libraries that could be easily called and not support?

– There were an endless supply of COBOL programmers?

• “What a Wonderful World” that would be …

Page 5: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 5

Mixing Languages Through Libraries

• We Invented It Application Program Interface (Wii API)

– A “was well documented”, in-house developed standard

– Specific application during definition

– Communication over TCPIP (Socket-to-Socket)

• Common Object Request Broker Architecture (CORBA)

– A well-defined Object Management Group (OMG) standard

– Vendor-independent architecture and structure

– Communication over the Internet Inter-Orb Protocol (IIOP)

• Java Native Interface (JNI)

– A well-defined Java standard

– Defined for C

– Java DLL

Page 6: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 6

COBOL ????

• COBOL app needs to access an Oracle database

• COBOL app needs to be web services enabled

• COBOL app needs to create a PDF file

• COBOL app needs to decrypt messages encrypted with …

• COBOL app needs to …

Page 7: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 7

Java Invocation Interface (Jii)

• COBOL-85 (via ALGOL) calling Java methods

• Can invoke standard Java packages

• Usually need minimal Java code

• Native program is in control

• Uses Java as a private library

• JVM ends when the native program ends

Page 8: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 8

Java Invocation Interface (Jii)

• API developed to call Java methods

– Simplified with “com.unisys.mcp.tools.JifGen”

• Reads Java source or class and generates “entrypoint” INCLUDE file

• Does all the ‘dirty work’ of calling JNI methods

– Based on C JNI method definition in a generated ALGOL library

• Calls for identifying methods by name

• Calls for identifying objects by name

• Chatty and “error prone”

JNI

Proxy

Lib

JProcessor

Java

Worker

Java

Worker

JVM

Application JNI

Interfaces

Java

Class

Page 9: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 9

Jii Development Process Demo

• Create or identify a Java class with public methods

– Demo – COBOL program needing to accessing ‘remote’ database using data from a DMSII database

– Java application needs 3 methods

• Connect to the remote database – getConnection()

• Close database connection – releaseConnection()

• Retrieve record using key passed from COBOL-85 and compare data – compareEqual (int id, String word)

JNI

Proxy

Lib

JProcessor

Java

Worker

Java

Worker

JVM

Application JNI

Interfaces

Java

Class

UniverseDemo.zip

Page 10: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 10

Jii Development Process Demo

package com.unisys.mcp.universe;

import java.sql.*;

public class Demo

{

static Connection con = null;

static PreparedStatement pStmt = null;

static String url = “jdbc:unisys:dmsql:Unisys.DMSII”;

public static boolean releaseConnection()

{

try {

if (pStmt <> null) pStmt.close();

if (con <> null) con.close();

return true;

} catch (SQLException seq) {

sqe.printStackTrace();

return false;

}

}

Page 11: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 11

Jii Development Process Demo

public static boolean getConnection()

{

boolean onMCP = System.getProperty("os.name").contains("MCP");

String hostname = onMCP ? “EVLANMCP” : “ECCSK”;

try {

Class.forName ("com.unisys.jdbc.dmsql.Driver“)

url = url + “resource=UNIVERSEDB;”

+ “host=“ + hostname + “;port=1897”

+ “user=JAVA;password=JAVA”);

con = DriverManager.getConnection (url);

if (con == null) return false;

pStmt = con.prepareStatement

("SELECT * FROM universedb.words “

+ “ where word_id = ?");

if (pStmt == null) return false;

return true;

} catch (SQLException sqe) {

sqe.printStackTrace();

return false;

}

}

Page 12: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 12

Jii Development Process Demo

public static boolean compareEqual (int id, String wordA)

{

wordA = wordA.trim();

try {

pStmt.setInt(1, id);

ResultSet rs = pStmt.executeQuery();

if (rs.next() {

String a = rs.getString(“dmsii_word_a”);

if (a.compareTo(wordA) != 0) {

System.err.println (id + “) “ + a + “<>” + wordA);

return false;

}

} else {

System.err.println (id + “) not found [“+ wordA +”]”);

return false;

}

return true;

} catch (SQLException sqe) {

sqe.printStackTrace();

return false;

} }

Page 13: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 13

Jii Development Process Demo

public static void main (String args[])

{

String command = “select * from dmsii_words”);

try {

if (getConnection()) {

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(command);

while (rs.next()) {

int id = rs.getInt (“word_id”);

String wordA = rs.getString (“dmsii_word_a”);

System.out.println (“(“ + id + “) “ + wordA.trim() + “ : “

+ compareEqual (id, wordA.getBytes())

);

}

rs.close();

stmt.close();

releaseConnection();

}

} catch (Exception e) { …; }

}

}

Page 14: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 14

Jii Development Process Demo

• Run JifGen to create files to be used on MCP RUN *DIR/JRE7/BIN/MCPTOOLS

("JifGen -verbose -cobol -d /-/JAVA/USERCODE/JAVA/UD

-classpath /-/JAVA/DIR/JAVA/universedemo.jar

-output DEMO com.unisys.mcp.universe.Demo")

[Creating file /-/JAVA/USERCODE/JAVA/UD/DEMOINCL.C85_M]

[Creating file /-/JAVA/USERCODE/JAVA/UD/DEMO.ALG_M]

JNI

Proxy

Lib

JProcessor

Java

Worker

Java

Worker

JVM

Application JNI

Interfaces

Java

Class

Page 15: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 15

Jii Development Process Demo

• Modify “existing” application to use Java methods

– Include sections from *DIR/JRE7/JDK/INCLUDE/JNILIB/COBOL

– Include sections from "DEMOINCL.C85_M“

– CALL JNI-CREATE-JAVA-VM USING … GIVING RESULT.

– Do work using the following generated entry points • ENTRY PROCEDURE INITIALIZE-DEMO

• ENTRY PROCEDURE DEMO-DEMO-GET-CONNECTION

• ENTRY PROCEDURE DEMO-DEMO-RELEASE-CONNECTION

• ENTRY PROCEDURE DEMO-DEMO-COMPARE-EQUAL

– CALL JNI-DESTROY-JAVA-VM USING … GIVING RESULT.

• Create ALGOL library DEMO/LIBRARY

– COMPILE UD/"DEMO.ALG_M“ AS DEMO/LIBRARY

– SL DEMO = (JAVA)OBJECT/DEMO/LIBRARY

Page 16: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 16

Jii Development Process Demo

LOCAL-STORAGE SECTION.

$$INCLUDE JNICOB="COBOL" ("JNI-PARAMETERS")

$$INCLUDE DEMOCOB="""DEMOINCL.C85_M""" ("DEMO-PARAMETERS")

PROGRAM-LIBRARY SECTION.

$$INCLUDE JNICOB="COBOL" ("JNI-FUNCTIONS")

$$INCLUDE DEMOCOB="""DEMOINCL.C85_M""" ("DEMO-FUNCTIONS")

PROCEDURE DIVISION.

Page 17: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 17

Jii Development Process Demo

Initialize-JVM.

STRING jvmOptions, jvmCP1, jvmCP2, jvmCP3, jvmCP4

DELIMITED BY SIZE INTO ARGUMENTS.

ADD FUNCTION Length ( jvmOptions ),

FUNCTION Length ( jvmCP1 ),

FUNCTION Length ( jvmCP2 ),

FUNCTION Length ( jvmCP3 ),

FUNCTION Length ( jvmCP4 )

GIVING ArgLength.

MOVE MCP-Options TO Arguments-2.

MOVE FUNCTION Length ( MCP-Options ) to Arg2Length.

CALL JNI-CREATE-JAVA-VM

USING JNI-VM, JNI-ENV, Interface-Version,

ARGUMENTS, ArgLength

ARGUMENTS-2, Arg2Length

GIVING RESULT.

CALL INITIALIZE-DEMO

USING JNI-ENV.

CALL DEMO-DEMO-GET-CONNECTION

USING JNI-ENV, bRESULT.

Page 18: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 18

Jii Development Process Demo

OPEN INQUIRY UNIVERSEDB ON EXCEPTION GO TO XIT.

loop-it.

FIND WORD VIA NEXT IDX-WORDS ON EXCEPTION GO TO XIT.

MOVE DMSII-WORD-A TO DataWord.

MOVE WORD-SZ TO WordSz.

CALL DEMO-DEMO-COMPARE-EQUAL

USING JNI-ENV, WORD-ID, DataWord, WordSz, bRESULT.

IF NOT bRESULT THEN

DISPLAY "COMPARE STRING ERROR=", bRESULT.

GO loop-it.

xit.

CLOSE UNIVERSEDB ON EXCEPTION NEXT SENTENCE.

Page 19: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 19

Jii Possibilities?

• Calling Bouncy Castle Crypto API

• Accessing JBoss through its Java Naming and Directory Interface (JNDI)

• Accessing third-party databases

• Creating thumbnails of images

• PDF Generation

• Creating JSON data files

• Using “off-the-shelf” components written in Java http://www.componentsource.com/features/java-components/index.html

Page 20: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 20

Guidelines for Calling Java

• Calling Java from native code is complicated and tedious.

• Crossing between MCP and the Java environment is expensive.

• Try to write “heavy” functions.

• Pass everything as parameters rather than calling back for more information.

• Stay in Java if you can; calling native code is seldom faster.

Page 21: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 21

Java Docs Main Page

Virtual Machine for the Java™ 6.0 Platform on ClearPath MCP

Programming Guide Installation and Administration Guide

ClearPath Common Appliance Configuration Guide JVM for MCP API Specification Java Native Interface for MCP

Readme, Errata and Other Notes Optimizing Java Socket Performance

Example Java Programs

Page 22: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

“A Cocktail of Java and MCP”

Using Java NIO2 Interfaces

Page 23: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 23

Java 7

• Java SE 7 – Available with MCPJAVA in 15.0

• New I/O features:

– Asynchronous I/O

– Ability to extend and customize the I/O interface.

• For MCP this means:

– Ability to query and specify many file attributes.

Page 24: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 24

Java 7 Example

import java.nio.file.*; import com.unisys.mcp.file.*; import static com.unisys.mcp.file.InitialFileAttributes.*; … Path file = FileSystems.getDefault().getPath( args[0] ); file.createFile( FileKind.COBOL85SYMBOL.asFileAttribute(), FileClass.RECORDORIENTED.asFileAttribute(), maximumRecordSize(80), note("This file was created by Java") );

Page 25: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 25

Java 7 Example

• Copies a file identified by the “file” path at ECCSK to the local host as the file identified by the destination path.

• Uses MCP copy operation (BNA, FTP).

• Similar moveTo will change the file name, if possible; otherwise copy and delete the original.

file.copyTo( destination,

StandardCopyOption.ReplaceExisting,

McpCopyOption.Recursive,

McpCopyOption.SourceHost(“ECCSK”),

Page 26: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 26

McpFileAttributes Class

• Provides most MCP attributes for a file.

• All the various time stamps.

• Title

• Note

• and many more …

Page 27: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 27

File Attributes

areaLength accessTimestamp fileClass

areas alterTimestamp fileKind

blockSize attributeModifyTimestamp fileName

clearAreas backupTimestamp fileStructure

compilerKind copyDestinationTimestamp frameSize

ccsVersion copySourceTimestamp hostName

extMode creationTimestamp licenseKey

familyIndex executeTimestamp note

familyName openTimestamp pathName

sensitiveData readTimestamp product

userInfo maximumRecordSize title

Page 28: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 28

Open Options

• Specify any of these when opening a file.

File Options Record Options

Buffers Fold

BufferSize IncludeSequenceNumbers

HostName NoCrLf

IntName StripSpaces

Truncate

Unfold

Page 29: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 29

Initial File Attributes

• Specify any of these when creating a file.

areaLength areas

blockSize clearAreas

familyIndex frameSize

hostName maximumRecordSize

note sensitiveData

userInfo

Page 30: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 30

Java Docs Main Page

Virtual Machine for the Java™ 6.0 Platform on ClearPath MCP

Programming Guide Installation and Administration Guide

ClearPath Common Appliance Configuration Guide JVM for MCP API Specification Java Native Interface for MCP

Readme, Errata and Other Notes Optimizing Java Socket Performance

Example Java Programs

Page 31: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

“A Cocktail of Java and MCP”

Accessing DMSII From Java

Page 32: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 32

Accessing DMSII From Java

DMSII Databases

D

M

S

Q

L

Page 33: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 33

Accessing DMSII From Java

• COBOL & ALGOL access via Host Language constructs

– OPEN, FIND/LOCK, CREATE, STORE, …

– BEGIN-TRANSACTION, END-TRANSACTION, …

• Java access

– Java Host Language Interface (JHLI) and Java DMInterface (JDMI)

– Java EE Connector Architecture (JCA) components

• Resource Adapters

– MCP Transaction Resource Adapter (JRAC)

– Open Distributed Transaction Processing Resource Adapter (DTP-RA)

– Java Database Connectivity (JDBC)

• Resource adapters based on JCA

• Structured Query Language (SQL) based

– Hibernate

Page 34: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 34

Querying DMSII using JDBC SQL Client Tools

DMSII

Page 35: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 35

DMSII-JDBC @ GSA

ClearPath

MCP

SQL Query

Processor

for

ClearPath

MCP

DMSII

JProcessor

Presentation Tier/

Business Tier

JSF/RichFaces

POJO JD

BC

Driver

Page 36: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 36

Summary

• Invocation interface lets ALGOL or COBOL program call Java libraries.

• Java 7 will allow tighter file system integration.

• Consider the alternatives for accessing a DMSII database from Java

Page 37: Application Development, A Cocktail of Java and MCP · 2014. 5. 8. · call jni-create-java-vm using jni-vm, jni-env, interface-version, arguments, arglength arguments-2, arg2length

© 2013 Unisys Corporation. All rights reserved. 37

Questions?