<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The ramblings of a yorkshire tyke &#187; JVM</title>
	<atom:link href="http://www.gennard.net/blog/category/jvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gennard.net/blog</link>
	<description>Life, Rants and Programming In A Blog</description>
	<lastBuildDate>Sat, 10 Jul 2010 23:04:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Scripting Languages and COBOL</title>
		<link>http://www.gennard.net/blog/2010/01/scripting-languages-and-cobol/</link>
		<comments>http://www.gennard.net/blog/2010/01/scripting-languages-and-cobol/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 10:39:04 +0000</pubDate>
		<dc:creator>spgennard</dc:creator>
				<category><![CDATA[COBOL]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javax.script]]></category>
		<category><![CDATA[JSR 223]]></category>
		<category><![CDATA[Micro Focus]]></category>
		<category><![CDATA[Net Express]]></category>
		<category><![CDATA[ScriptEngine]]></category>
		<category><![CDATA[ScriptEngineManager]]></category>
		<category><![CDATA[ScriptObject]]></category>

		<guid isPermaLink="false">http://www.gennard.net/blog/?p=462</guid>
		<description><![CDATA[The use of scripting languages with other languages has increased over the last couple of years, from a simple case of interoperability, reuse of scripting code to allowing your code to customised via the user of external scripts. All of which are real world examples I have seen customers use. Interoperability between languages is very [...]]]></description>
			<content:encoded><![CDATA[<p>The use of scripting languages with other languages has increased over the last couple of years, from a simple case of interoperability, reuse of scripting code to allowing your code to customised via the user of external scripts.   All of which are real world examples I have seen customers use.</p>
<p>Interoperability between languages is very important to COBOL environments just as much as other languages.   Some platforms such as Microsoft&#8217;s .Net with their CLR makes life much easier by allowing all languages to share a common infrastructure ie: the instruction set and the VM (MSIL and CLR) along with a base class library to get you started.</p>
<p>Environments such as Sun&#8217;s VM (JVM) provide two different approaches to interoperability with Java, the first is via JNI/JNA and the second is producing bytecode that runs as is on the VM.</p>
<p>Although the Micro Focus COBOL compiler does not support JVM bytecode or Java source generation it does have support for invoking classes/methods via the OO invoke verb.</p>
<p>This mechanism is very simple to use, you just need to let our object COBOL runtime know the class is a Java class, which can be done by placing $JAVA$ before the name of the class and ensuring the class itself can be found by the JVM itself usually by adding an extra directories or .jar files to the CLASSPATH environment variable.</p>
<p>With Java 6.0 and JSR 223 support for Java based scripting languages were provided via the package javax.script.</p>
<p>Java has a wealth of scripting languages from awk to xlst.  My favourites being <a href="http://jython.sourceforge.net/">jpython</a>, <a href="http://jruby.sourceforge.net/">jruby</a> and <a href="http://www.mozilla.org/rhino">javascript</a>.</p>
<p>The java.net website has a comprehensive list of scripting languages -<br />
<a href="https://scripting.dev.java.net/">https://scripting.dev.java.net/</a>.</p>
<p>To use the scripting packages, you first need to create a ScriptEngineManager, then use this to create a specific ScriptEngine object for your chosen scripting language and use it.</p>
<p>For example:</p>
<li>Create a ScriptEngineManager object.</li>
<li>Retrieve a ScriptEngine object from the manager.</li>
<li>Evaluate a script using the ScriptEngine object.</li>
<p>In COBOL this is quite simply: </p>
<div class="codecolorer-container cobol blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="">*&gt; ooctrl(+p) required for COM and Java classes</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; ooctrl(-f) used to preserve case of method names for Java</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #a0a0a0; font-style: italic;">$set ooctrl(+p) ooctrl(-f)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class-control</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-Script-EngineManager <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$javax.script.ScriptEngineManager&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-Script-Engine <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$javax.script.ScriptEngine&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.lang.Object&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-System <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.lang.System&quot;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-PrintStream <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.io.PrintStream&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">working-storage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-obj-sem &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-Script-EngineManager<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-javascript &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-Script-Engine<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-obj &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-<span style="color: #008000; font-weight: bold;">object</span><span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-pout &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-PrintStream<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">procedure</span> <span style="color: #008000; font-weight: bold;">division</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-Script-EngineManager <span style="color: #ff0000;">&quot;new&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-obj-sem <span style="color: #ff0000;">&quot;getEngineByName&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;JavaScript&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-javascript <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;eval&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z<span style="color: #ff0000;">&quot;print('Hello, world!')&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-obj<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-obj <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> cls-System <span style="color: #ff0000;">&quot;getout&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-pout<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-pout <span style="color: #ff0000;">&quot;println&quot;</span> &nbsp; <span style="color: #008000; font-weight: bold;">using</span> ws-obj<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-pout <span style="color: #ff0000;">&quot;finalize&quot;</span> &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-pout<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-obj <span style="color: #ff0000;">&quot;finalize&quot;</span> &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> ws-obj<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
<br />
&nbsp; &nbsp; &nbsp; $if NO-FINALIZE <span style="color: #008000; font-weight: bold;">not</span> defined<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-obj-sem <span style="color: #ff0000;">&quot;finalize&quot;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;finalize&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-javascript<br />
&nbsp; &nbsp; &nbsp; $end<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">stop</span> <span style="color: #008000; font-weight: bold;">run</span><span style="color: #000066;">.</span></div></div>
<p>The actual Javascript being execute is contained in the invoke statement, which is simply:</p>
<div class="codecolorer-container javascript blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello, world!'</span><span style="color: #009900;">&#41;</span></div></div>
<p>To use the above example, we first need to compile the code and run it.. which is done as follows:</p>
<div class="codecolorer-container text blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\jscripting\HelloWorld&gt;cobol cbljscript.cbl int(); <br />
Micro Focus Net Express V5<br />
Version 6.0.00059 &nbsp;Copyright (C) 1984-2009 Micro Focus (IP) Limited.<br />
URN AXCGG/AA0/00000 <br />
* Checking complete with no errors - starting code generation<br />
* Generating cbljscript<br />
* Data: &nbsp; &nbsp; &nbsp; &nbsp; 848 &nbsp; &nbsp; Code: &nbsp; &nbsp; &nbsp; &nbsp;1992 &nbsp; &nbsp; Literals: &nbsp; &nbsp; &nbsp; &nbsp; 904<br />
<br />
C:\jscripting\HelloWorld&gt;runm cbljscript <br />
Micro Focus Net Express V6.0.00059 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
RUN TIME ENVIRONMENT Copyright (C) 1984-2009 Micro Focus (IP) Limited. &nbsp; &nbsp; &nbsp; &nbsp; <br />
URN AXCGG/AA0/00000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
Hello, world!</div></div>
</p>
<p>This is just the start, the next thing piece that is required with interoperability to another language is the ability to pass parameters to in and out of the script.  Luckily for us the clever chaps on the jsr group have provided &#8216;put&#8217; and &#8216;get&#8217; methods that allows us to simply put a name parameter and get the resulting updated or new parameter.</p>
<p>So consider the example, where we need to setup a parameter called &#8216;message&#8217; for the script and then read a parameter called &#8216;replyMessage&#8217; after the script has been executed.   The javascript to do this is: </p>
<div class="codecolorer-container javascript blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* Do some insanity checking! */</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; message <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ERROR - 'message' has not been setup&quot;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
println<span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><br />
<br />
replyMessage <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Hello from javascript&quot;</span></div></div>
<p>To setup the message parameter, we just need todo.</p>
<div class="codecolorer-container cobol blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp;<span style="">*&gt; Put a variable in engine, so the javascript</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="">*&gt; can use it.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;put&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z<span style="color: #ff0000;">&quot;message&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z<span style="color: #ff0000;">&quot;Hello World from COBOL!&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">end-invoke</span></div></div>
<p>The after the script has executed, we just need to use the &#8216;get&#8217; method..</p>
<div class="codecolorer-container cobol blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp;<span style="">*&gt; get a variable in engine</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;get&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z<span style="color: #ff0000;">&quot;replyMessage&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">end-invoke</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="">*&gt; now display the replyMessage if it is available</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">if</span> ws-<span style="color: #008000; font-weight: bold;">message</span> <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-pout <span style="color: #ff0000;">&quot;println&quot;</span> &nbsp; <span style="color: #008000; font-weight: bold;">using</span> ws-<span style="color: #008000; font-weight: bold;">message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">display</span> <span style="color: #ff0000;">&quot;Javascript did not set a replyMessage var&quot;</span></div></div>
<p>The completed COBOL example below, uses a side file for the javascript too, the code is as follows:</p>
<div class="codecolorer-container cobol blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="">*&gt; ooctrl(+p) required for COM and Java classes</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; ooctrl(-f) used to preserve case of method names for Java</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #a0a0a0; font-style: italic;">$set ooctrl(+p) ooctrl(-f)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class-control</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-Script-EngineManager <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$javax.script.ScriptEngineManager&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-Script-Engine <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$javax.script.ScriptEngine&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.lang.Object&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-System <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.lang.System&quot;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-PrintStream <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.io.PrintStream&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-FileReader <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.io.FileReader&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">working-storage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-<span style="color: #008000; font-weight: bold;">file</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-FileReader<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-obj-sem &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-Script-EngineManager<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-javascript &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-Script-Engine<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-obj &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-<span style="color: #008000; font-weight: bold;">object</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-<span style="color: #008000; font-weight: bold;">message</span> &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-<span style="color: #008000; font-weight: bold;">object</span><span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-pout &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-PrintStream<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">procedure</span> <span style="color: #008000; font-weight: bold;">division</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; setup ws-pout to be System.out object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-System <span style="color: #ff0000;">&quot;getout&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-pout<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; Setup a FileReader object for the external helloworld.js file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-FileReader <span style="color: #ff0000;">&quot;new&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;helloworld.js&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; Create a new script manager</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-Script-EngineManager <span style="color: #ff0000;">&quot;new&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; Find the javascript engine</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-obj-sem <span style="color: #ff0000;">&quot;getEngineByName&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;JavaScript&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-javascript <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; Put a variable in engine, so the javascript</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; can use it.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;put&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;message&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;Hello World from COBOL!&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; do some javascript stuff!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;eval&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ws-<span style="color: #008000; font-weight: bold;">file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; get a variable in engine</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;get&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;replyMessage&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; now display the replyMessage if it is available</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-<span style="color: #008000; font-weight: bold;">message</span> <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-pout <span style="color: #ff0000;">&quot;println&quot;</span> &nbsp; <span style="color: #008000; font-weight: bold;">using</span> ws-<span style="color: #008000; font-weight: bold;">message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">display</span> <span style="color: #ff0000;">&quot;Javascript did not set a replyMessage var&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; cleanup code, not strickly needed for the example but</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; its good practice, to do it.</span><br />
&nbsp; &nbsp; &nbsp; $if NO-FINALIZE <span style="color: #008000; font-weight: bold;">not</span> defined<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-<span style="color: #008000; font-weight: bold;">message</span> <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-<span style="color: #008000; font-weight: bold;">message</span> <span style="color: #ff0000;">&quot;finalize&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-pout <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-pout <span style="color: #ff0000;">&quot;finalize&quot;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-pout<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-obj-sem <span style="color: #ff0000;">&quot;finalize&quot;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;finalize&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-javascript<br />
&nbsp; &nbsp; &nbsp; $end<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">stop</span> <span style="color: #008000; font-weight: bold;">run</span><span style="color: #000066;">.</span></div></div>
<div class="codecolorer-container text blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\jscripting\HelloWorld3&gt;cobol cbljscript.cbl int(); <br />
Micro Focus Net Express V5<br />
Version 6.0.00059 &nbsp;Copyright (C) 1984-2009 Micro Focus (IP) Limited.<br />
URN AXCGG/AA0/00000 <br />
* Checking complete with no errors - starting code generation<br />
* Generating cbljscript<br />
* Data: &nbsp; &nbsp; &nbsp; &nbsp; 888 &nbsp; &nbsp; Code: &nbsp; &nbsp; &nbsp; &nbsp;2528 &nbsp; &nbsp; Literals: &nbsp; &nbsp; &nbsp; &nbsp;1296<br />
<br />
C:\jscripting\HelloWorld3&gt;runm cbljscript <br />
Micro Focus Net Express V6.0.00059 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
RUN TIME ENVIRONMENT Copyright (C) 1984-2009 Micro Focus (IP) Limited. &nbsp; &nbsp; &nbsp; &nbsp; <br />
URN AXCGG/AA0/00000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
Hello World from COBOL!<br />
Hello from javascript</div></div>
<p>As you can see from the code above, setting up parameter is pretty easy todo but sometimes we just want to execute a function in the scripting language such as:</p>
<div class="codecolorer-container javascript blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> testMessage<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;testMessage : &quot;</span> <span style="color: #339933;">+</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The ScriptEngine object that we have created to use the scripting engine may implement an optional interface called javax.script.Invocable, if the scripting engine we are using does provide this interface then a method called invokeFunction(..) can be used.</p>
<p>In order to reduce the size of the COBOL code, I have coded a simple utils class in java as a simple proxy layer, the code is pretty simple but does make it easier for the COBOL to use the invokeFunction() method.</p>
<div class="codecolorer-container javascript blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Then from the COBOL side<span style="color: #339933;">,</span> we can just <span style="color: #003366; font-weight: bold;">use</span> the invokeFunction above.<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>For example<span style="color: #339933;">:&lt;/</span>p<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>code lang<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;cobol&quot;</span> width<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;800&quot;</span> lines<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;-1&quot;</span> nowrap<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">*&gt;</span> invoke a <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066; font-weight: bold;">with</span> one parameter<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invoke cls<span style="color: #339933;">-</span>utils <span style="color: #3366CC;">&quot;invokeFunction&quot;</span> using<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ws<span style="color: #339933;">-</span>javascript<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z<span style="color: #3366CC;">&quot;testMessage&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z<span style="color: #3366CC;">&quot;Hello to function testMessage from COBOL&quot;</span></div></div>
<p>Which gives us the following output when executed.</p>
<div class="codecolorer-container text blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\jscripting\InvokeFunction&gt;runm cbljscript<br />
Micro Focus Net Express V6.0.00059<br />
RUN TIME ENVIRONMENT Copyright (C) 1984-2009 Micro Focus (IP) Limited.<br />
URN AXCGG/AA0/00000<br />
<br />
testMessage : Hello to function testMessage from COBOL</div></div>
<p>The completed example is as follows:</p>
<div class="codecolorer-container cobol blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="">*&gt; ooctrl(+p) required for COM and Java classes</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; ooctrl(-f) used to preserve case of method names for Java</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #a0a0a0; font-style: italic;">$set ooctrl(+p) ooctrl(-f)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class-control</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-Script-EngineManager <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$javax.script.ScriptEngineManager&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-Script-Engine <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$javax.script.ScriptEngine&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.lang.Object&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-System <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.lang.System&quot;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-PrintStream <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.io.PrintStream&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-FileReader <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$java.io.FileReader&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cls-Utils <span style="color: #008000; font-weight: bold;">is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;$JAVA$utils&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">working-storage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-<span style="color: #008000; font-weight: bold;">file</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-FileReader<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-obj-sem &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-Script-EngineManager<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-javascript &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-Script-Engine<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-<span style="color: #008000; font-weight: bold;">message</span> &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-<span style="color: #008000; font-weight: bold;">object</span><span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;01 ws-pout &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span> cls-PrintStream<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">procedure</span> <span style="color: #008000; font-weight: bold;">division</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; setup ws-pout to be System.out object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-System <span style="color: #ff0000;">&quot;getout&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-pout<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; Setup a FileReader object for the external helloworld.js file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-FileReader <span style="color: #ff0000;">&quot;new&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;helloworld.js&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; Create a new script manager</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-Script-EngineManager <span style="color: #ff0000;">&quot;new&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; Find the javascript engine</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-obj-sem <span style="color: #ff0000;">&quot;getEngineByName&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;JavaScript&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-javascript <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; do some javascript function</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;eval&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ws-<span style="color: #008000; font-weight: bold;">file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; invoke a function with one parameter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> cls-utils <span style="color: #ff0000;">&quot;invokeFunction&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ws-javascript<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;testMessage&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;Hello to function testMessage from COBOL&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; cleanup code, not strickly needed for the example but</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt; its good practice, to do it.</span><br />
&nbsp; &nbsp; &nbsp; $if NO-FINALIZE <span style="color: #008000; font-weight: bold;">not</span> defined<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-<span style="color: #008000; font-weight: bold;">file</span> <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-<span style="color: #008000; font-weight: bold;">file</span> <span style="color: #ff0000;">&quot;finalize&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-<span style="color: #008000; font-weight: bold;">message</span> <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-<span style="color: #008000; font-weight: bold;">message</span> <span style="color: #ff0000;">&quot;finalize&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-<span style="color: #008000; font-weight: bold;">message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-pout <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-pout <span style="color: #ff0000;">&quot;finalize&quot;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-pout<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-obj-sem <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-obj-sem <span style="color: #ff0000;">&quot;finalize&quot;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> ws-obj-sem<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">if</span> ws-javascript <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> ws-javascript <span style="color: #ff0000;">&quot;finalize&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> ws-javascript<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; $end<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">stop</span> <span style="color: #008000; font-weight: bold;">run</span><span style="color: #000066;">.</span></div></div>
<p>Conclusions: Using a Java based scripting language from COBOL is quite easy, so feel free to use it.   Now which scripting language should I use&#8230;?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gennard.net/blog/2010/01/scripting-languages-and-cobol/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using sun.jvmstat.monitor to see active Java processes</title>
		<link>http://www.gennard.net/blog/2009/12/using-sun-jvmstat-monitor-to-see-active-java-processes/</link>
		<comments>http://www.gennard.net/blog/2009/12/using-sun-jvmstat-monitor-to-see-active-java-processes/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 10:56:38 +0000</pubDate>
		<dc:creator>spgennard</dc:creator>
				<category><![CDATA[JVM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jvmstat]]></category>
		<category><![CDATA[monitor]]></category>

		<guid isPermaLink="false">http://www.gennard.net/blog/?p=335</guid>
		<description><![CDATA[Monitor&#8217;ing Java processes can be achieved using the jvmstat monitor classes provided in the JVM. The documentation is a bit sketchy but with a little experimenting it can be done. Below is a little example that shows you how to get a list of active Java processes.. which of course can then be used for [...]]]></description>
			<content:encoded><![CDATA[<p>Monitor&#8217;ing Java processes can be achieved using the jvmstat monitor classes provided in the JVM.   The documentation is a bit sketchy but with a little experimenting it can be done.</p>
<p>Below is a little example that shows you how to get a list of active Java processes.. which of course can then be used for other things <img src='http://www.gennard.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here&#8217;s the code&#8230;</p>
<div class="codecolorer-container java blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URISyntaxException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">sun.jvmstat.monitor.*</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> sjps<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> MonitorException, URISyntaxException<br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; MonitoredHost monitoredHost <span style="color: #339933;">=</span> MonitoredHost.<span style="color: #006633;">getMonitoredHost</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Set<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> activeVms <span style="color: #339933;">=</span> monitoredHost.<span style="color: #006633;">activeVms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> psId <span style="color: #339933;">:</span> activeVms<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MonitoredVm monitoredVm <span style="color: #339933;">=</span> monitoredHost.<span style="color: #006633;">getMonitoredVm</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> VmIdentifier<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>psId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003399;">String</span> mainClass <span style="color: #339933;">=</span> MonitoredVmUtil.<span style="color: #006633;">mainClass</span><span style="color: #009900;">&#40;</span>monitoredVm, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003399;">String</span> vmVersion <span style="color: #339933;">=</span> MonitoredVmUtil.<span style="color: #006633;">vmVersion</span><span style="color: #009900;">&#40;</span>monitoredVm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003399;">String</span> commandLine <span style="color: #339933;">=</span> MonitoredVmUtil.<span style="color: #006633;">commandLine</span><span style="color: #009900;">&#40;</span>monitoredVm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>mainClass <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; [&quot;</span> <span style="color: #339933;">+</span> psId <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot; using : &quot;</span><span style="color: #339933;">+</span>vmVersion<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; -&gt; &quot;</span><span style="color: #339933;">+</span>commandLine<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>And to see the code running..</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ java sjps<br />
sjps [865] using : 14.1-b02-90<br />
&nbsp;-&gt; sjps<br />
JConsole [863] using : 14.1-b02-90<br />
&nbsp;-&gt; sun.tools.jconsole.JConsole</div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gennard.net/blog/2009/12/using-sun-jvmstat-monitor-to-see-active-java-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java process id via java.lang.management</title>
		<link>http://www.gennard.net/blog/2009/12/java-process-id-via-java-lang-management/</link>
		<comments>http://www.gennard.net/blog/2009/12/java-process-id-via-java-lang-management/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 23:53:13 +0000</pubDate>
		<dc:creator>spgennard</dc:creator>
				<category><![CDATA[JVM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[getpid]]></category>

		<guid isPermaLink="false">http://www.gennard.net/blog/?p=329</guid>
		<description><![CDATA[While working on a project recently I need to find out the current process of the active running Java process (for tracing/auditing), however I never found a 100% perfect solution but did come across an acceptable solution to use the management classes to query its name, which happens to have encoding in it, so here [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a project recently I need to find out the current process of the active running Java process (for tracing/auditing), however I never found a 100% perfect solution but did come across an acceptable solution to use the management classes to query its name, which happens to have encoding in it, so here is the quick solution:
</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.management.ManagementFactory</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> getpid<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Process id : &quot;</span><span style="color: #339933;">+</span>getProcessId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">long</span> getProcessId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> ManagementFactory.<span style="color: #006633;">getRuntimeMXBean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> nameBits <span style="color: #339933;">=</span> name.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> nameBits <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">-</span>1 <span style="color: #339933;">:</span> &nbsp;<span style="color: #003399;">Long</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>nameBits<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ java getpid<br />
Process id : 377</div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gennard.net/blog/2009/12/java-process-id-via-java-lang-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type Safety and COBOL</title>
		<link>http://www.gennard.net/blog/2009/11/type-safety-and-cobol/</link>
		<comments>http://www.gennard.net/blog/2009/11/type-safety-and-cobol/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 23:27:34 +0000</pubDate>
		<dc:creator>spgennard</dc:creator>
				<category><![CDATA[CLR]]></category>
		<category><![CDATA[COBOL]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[TypeSafety]]></category>

		<guid isPermaLink="false">http://www.gennard.net/blog/?p=263</guid>
		<description><![CDATA[Having read a recent bog about COBOL and type-safety, I though I would jot down some comments. ANS85 COBOL is naturally is type-unsafe due as every data item being part of one memory region (or storage area), because of this it can make is difficult to talk to type-safe language such as Java or the [...]]]></description>
			<content:encoded><![CDATA[<p>Having read a recent bog about COBOL and type-safety, I though I would jot down some comments.</p>
<p>ANS85 COBOL is naturally is type-unsafe due as every data item being part of one memory region (or storage area), because of this it can make is difficult to talk to type-safe language such as Java or the CLR.</p>
<p>However, just like any computer language it does not stay still.  Object oriented features were added to COBOL, at this point in time you we were given the verbs to create 100% type safe application.</p>
<p>The key to writing type safe code is the use OBJECT-REFERENCE will a TYPE and using them on in your methods and using the INVOKE verb.</p>
<p>For example:</p>
<div class="codecolorer-container cobol blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">IDENTIFICATION</span> <span style="color: #008000; font-weight: bold;">DIVISION</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">PROGRAM-ID</span><span style="color: #000066;">.</span> &nbsp; <span style="color: #ff0000;">&quot;TypeSafeExample&quot;</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">ENVIRONMENT</span> <span style="color: #008000; font-weight: bold;">DIVISION</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">CONFIGURATION</span> <span style="color: #000080; font-weight: bold;">SECTION</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">REPOSITORY</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">CLASS</span> SYS-<span style="color: #008000; font-weight: bold;">STRING</span> <span style="color: #008000; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;System.String&quot;</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">DATA</span> <span style="color: #008000; font-weight: bold;">DIVISION</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">WORKING-STORAGE</span> <span style="color: #000080; font-weight: bold;">SECTION</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 hello-world-<span style="color: #008000; font-weight: bold;">string</span> &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">OBJECT</span> <span style="color: #008000; font-weight: bold;">REFERENCE</span> SYS-<span style="color: #008000; font-weight: bold;">STRING</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">PROCEDURE</span> <span style="color: #008000; font-weight: bold;">DIVISION</span><span style="color: #000066;">.</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">SET</span> hello-world-<span style="color: #008000; font-weight: bold;">string</span> <span style="color: #000000; font-weight: bold;">TO</span> <span style="color: #ff0000;">&quot;Hello World&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">DISPLAY</span> hello-world-<span style="color: #008000; font-weight: bold;">string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">STOP</span> <span style="color: #008000; font-weight: bold;">RUN</span><span style="color: #000066;">.</span></div></div>
<p>The use of COBOL pictures can still be used, however for easy interop with other type-safe languages you really have to stick to the right types for your target environment when exposing your COBOL program to the other languages, this means if you are creating classes, the method to be consumed by other languages should use standard types, for .Net/CLR applications the use of <a href="http://msdn.microsoft.com/en-us/library/12a7a7h3(VS.71).aspx">CLS-COMPLIANT</a> types is the right approach.  For JVM applications, keep to the &#8220;core&#8221; java.lang types.  </p>
<p>Keep to these simple rules and COBOL will happy interop with type-safe languages. <img src='http://www.gennard.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-8227618096827072";
/* 468x15, created 11/23/09 */
google_ad_slot = "6592882116";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gennard.net/blog/2009/11/type-safety-and-cobol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fiddling with the JVM</title>
		<link>http://www.gennard.net/blog/2009/11/unsafe-java-for-the-wierd/</link>
		<comments>http://www.gennard.net/blog/2009/11/unsafe-java-for-the-wierd/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 20:10:49 +0000</pubDate>
		<dc:creator>spgennard</dc:creator>
				<category><![CDATA[JVM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[sun.misc.Unsafe]]></category>
		<category><![CDATA[unsafe]]></category>

		<guid isPermaLink="false">http://www.gennard.net/blog/?p=25</guid>
		<description><![CDATA[Java is a very safe language if used in a normal way, however just like the CLR it can be used in an unsafe manor. The main reason for using Java in a unsafe manor is performance, some unsafe operations are optimised by the JVM itself. The boot class loader grants enough permissions to access [...]]]></description>
			<content:encoded><![CDATA[<p>Java is a very safe language if used in a normal way, however just like the CLR it can be used in an unsafe manor.</p>
<p>The main reason for using Java in a unsafe manor is performance, some unsafe operations are optimised by the JVM itself.</p>
<p>The boot class loader grants enough permissions to access a key undocumented class sun.misc.Unsafe.  As I will state, this is an undocumented class and as the name implies it&#8217;s <b>unsafe</b>.</p>
<p>This class provides methods that allows you to manipulate objects and the memory of the objects directly.</p>
<p>For example, you could use it to access the object itself, lets look at an example to manipulate a String object.. not nice I hear you say&#8230;. and boy are you are so right.   The purpose of this example is to demonstrate the power of the JVM if used to the extreme but not to demonstrate how to destroy the JVM.</p>
<div class="codecolorer-container java blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">sun.misc.Unsafe</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Field</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// &nbsp;To Compile: javac BadBoy.java</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp;To Run: &nbsp; &nbsp; &nbsp; &nbsp;java -Xbootclasspath/p:. BadBoy</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BadBoy<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> &nbsp;Unsafe &nbsp;unsafe &nbsp;<span style="color: #339933;">=</span> Unsafe.<span style="color: #006633;">getUnsafe</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Find the field &quot;count&quot; inside java.lang.String</span><br />
&nbsp; &nbsp;<span style="color: #003399;">Field</span> field <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Find the memory offset within the field...</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">long</span> countOffset <span style="color: #339933;">=</span> unsafe.<span style="color: #006633;">objectFieldOffset</span><span style="color: #009900;">&#40;</span>field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;field <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;offset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">long</span> offset4Offset &nbsp;<span style="color: #339933;">=</span> unsafe.<span style="color: #006633;">objectFieldOffset</span><span style="color: #009900;">&#40;</span>field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Lets read the memory directory...</span><br />
&nbsp; &nbsp;<span style="color: #003399;">Object</span> &nbsp;object <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello World from Java, the ultra safe language... or is it..&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">int</span> length <span style="color: #339933;">=</span> unsafe.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span>object, countOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The original Length is length: &quot;</span> <span style="color: #339933;">+</span> length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1- The 'object' contains : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; -&gt; &quot;</span><span style="color: #339933;">+</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &nbsp;hashCode is : &quot;</span> <span style="color: #339933;">+</span> object.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;unsafe.<span style="color: #006633;">putInt</span><span style="color: #009900;">&#40;</span>object, offset4Offset, 17<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;unsafe.<span style="color: #006633;">putInt</span><span style="color: #009900;">&#40;</span>object, countOffset, 32<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2- The 'object' contains : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; -&gt; &quot;</span><span style="color: #339933;">+</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &nbsp;hashCode is : &quot;</span> <span style="color: #339933;">+</span> object.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Then, the output of the example on my little macbook is:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stephen-gennards-macbook:blob spg$ java -Xbootclasspath/p:. BadBoy<br />
The original Length is length: 60<br />
1- The 'object' contains : <br />
&nbsp;-&gt; Hello World from Java, the ultra safe language... or is it..<br />
&nbsp; hashCode is : 573430574<br />
2- The 'object' contains : <br />
&nbsp;-&gt; Java, the ultra safe language...<br />
&nbsp; hashCode is : 573430574</div></div>
<p>As you can see the String object is changed but the hashCode remains the same. <img src='http://www.gennard.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Even if the above example seems weird to the extreme and it is, I offer you one take home from this blog&#8230;</p>
<p>Be very careful what you place on your &#8220;bootclasspath!&#8221;<br />
]]></content:encoded>
			<wfw:commentRss>http://www.gennard.net/blog/2009/11/unsafe-java-for-the-wierd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object COBOL/Java default exception handler</title>
		<link>http://www.gennard.net/blog/2009/11/object-coboljava-default-exception-handler/</link>
		<comments>http://www.gennard.net/blog/2009/11/object-coboljava-default-exception-handler/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 22:16:54 +0000</pubDate>
		<dc:creator>spgennard</dc:creator>
				<category><![CDATA[COBOL]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Interop]]></category>

		<guid isPermaLink="false">http://www.gennard.net/blog/?p=49</guid>
		<description><![CDATA[While working on some support recently for our compiler (Micro Focus COBOL compiler that is), I became annoyed with the lack of a reasonable error messages/stack trace output from our Java/COBOL Object support. I have no idea why our default exception handler for Java exceptions just displays a such a simple message with little or [...]]]></description>
			<content:encoded><![CDATA[<p>While working on some support recently for our compiler (Micro Focus COBOL compiler that is), I became annoyed with the lack of a reasonable error messages/stack trace output from our Java/COBOL Object support.</p>
<p>I have no idea why our default exception handler for Java exceptions just displays a such a simple message with little or no information.   </p>
<p>For those who have not seen it, display something similar to:</p>
<div class="codecolorer-container text blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Exception 65537 not trapped by the class javaexceptionmanager.<br />
Description: &quot;Java exception&quot;<br />
Test error<br />
Hit T to terminate program. Hit any other key to continue.<br />
instantiated - test <br />
<br />
Exception 65537 not trapped by the class javaexceptionmanager.<br />
Description: &quot;Java exception&quot;<br />
Test error<br />
Hit T to terminate program. Hit any other key to continue.</div></div>
<p>Luckily for me and you, we do expose a mechanism for replacing the default exception handler.</p>
<p>Anyway, with very little effort I created a different &#8220;default&#8221; system exception handler that display this instead:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">instantiated - test 1<br />
java.lang.Exception: Test error<br />
&nbsp; &nbsp; &nbsp; &nbsp; at SimpleClass.TestException(SimpleClass.java:10)<br />
<br />
WARNING: JavaException: Test error<br />
instantiated - test 2<br />
java.lang.Exception: Test error<br />
&nbsp; &nbsp; &nbsp; &nbsp; at SimpleClass.TestException(SimpleClass.java:10)<br />
WARNING: JavaException: Test error</div></div>
<p>The key difference being that a Java stack trace is included, boy did this help me.</p>
<p>The program below is the code that implements the exception handler.  I am sure someone else can take this example and make it much nicer and provide more features but for this blog I will keep it simple.</p>
<div class="codecolorer-container cobol blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="color: #a0a0a0; font-style: italic;">$set ooctrl (+p-f) case</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">program-id</span><span style="color: #000066;">.</span> ExceptionCatcher<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class-control</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EntryCallback <span style="color: #008000; font-weight: bold;">is</span> <span style="color: #008000; font-weight: bold;">class</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;entrycll&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JavaExceptionManager <span style="color: #008000; font-weight: bold;">is</span> <span style="color: #008000; font-weight: bold;">class</span> &nbsp; <span style="color: #ff0000;">&quot;javaexpt&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExceptionManager <span style="color: #008000; font-weight: bold;">is</span> <span style="color: #008000; font-weight: bold;">class</span> &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;exptnmgr&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Javasup <span style="color: #008000; font-weight: bold;">is</span> <span style="color: #008000; font-weight: bold;">class</span> <span style="color: #ff0000;">&quot;javasup&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">working-storage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 wsCallback &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 wsIterator &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 theJavaException &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">local-storage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 <span style="color: #008000; font-weight: bold;">filler</span> <span style="color: #008000; font-weight: bold;">pic</span> x<span style="color: #000066;">.</span> &nbsp; <span style="">*&gt; dummy storage item to allow recursion</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">linkage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 lnkException &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 lnkErrorObject &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 lnkErrorTextCollection &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 lnkErrorNumber &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">pic</span> x<span style="color: #339933;">&#40;</span>4<span style="color: #339933;">&#41;</span> <span style="color: #008000; font-weight: bold;">comp-5</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 anElement &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">procedure</span> <span style="color: #008000; font-weight: bold;">division</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt;---Set up system level Exception handler</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> EntryCallback <span style="color: #ff0000;">&quot;new&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; z<span style="color: #ff0000;">&quot;JException&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">returning</span> wsCallback<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> ExceptionManager <span style="color: #ff0000;">&quot;register&quot;</span> <span style="color: #008000; font-weight: bold;">using</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; javaexceptionmanager<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wsCallback<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> EntryCallback <span style="color: #ff0000;">&quot;new&quot;</span> <span style="color: #008000; font-weight: bold;">using</span> z<span style="color: #ff0000;">&quot;DispError&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> wsIterator<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">goback</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">entry</span> <span style="color: #ff0000;">&quot;Jexception&quot;</span> <span style="color: #008000; font-weight: bold;">using</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lnkException <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lnkErrorNumber<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lnkErrorTextcollection<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> javasup <span style="color: #ff0000;">&quot;exceptionOccurred&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">returning</span> theJavaException<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">end-invoke</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">if</span> theJavaException <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">equal</span> <span style="color: #008000; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> theJavaException <span style="color: #ff0000;">&quot;printStackTrace&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">end-if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> lnkErrorTextCollection <span style="color: #ff0000;">&quot;do&quot;</span> <span style="color: #008000; font-weight: bold;">using</span> wsIterator<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">goback</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">entry</span> <span style="color: #ff0000;">&quot;DispError&quot;</span> <span style="color: #008000; font-weight: bold;">using</span> anElement<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">display</span> <span style="color: #ff0000;">&quot;WARNING: JavaException: &quot;</span> <span style="color: #008000; font-weight: bold;">with</span> <span style="color: #008000; font-weight: bold;">no</span> <span style="color: #008000; font-weight: bold;">advancing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-weight: bold;">invoke</span> anElement <span style="color: #ff0000;">&quot;display&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">display</span> <span style="color: #ff0000;">&quot; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">goback</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">.</span></div></div>
<p>To use the above code, you just have to first cut-paste to code into a file called ExceptionCatcher.cbl and include this in your project, and then add the directive INITCALL&#8221;ExceptionCatcher&#8221;, then away you go.</p>
<p>My test programs for the above example are:</p>
<div class="codecolorer-container cobol blackboard" style="border: 1px solid #9F9F9F;width:435px;"><div class="cobol codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp;<span style="color: #a0a0a0; font-style: italic;">$set ooctrl (+p-f) case</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">program-id</span><span style="color: #000066;">.</span> jtest<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">class-control</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SimpleClass <span style="color: #008000; font-weight: bold;">is</span> <span style="color: #008000; font-weight: bold;">class</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;$JAVA$SimpleClass&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">working-storage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 theInstance &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">object</span> <span style="color: #008000; font-weight: bold;">reference</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">local-storage</span> <span style="color: #000080; font-weight: bold;">section</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;01 <span style="color: #008000; font-weight: bold;">filler</span> <span style="color: #008000; font-weight: bold;">pic</span> x<span style="color: #000066;">.</span> &nbsp; <span style="">*&gt; dummy storage to allow the local entry</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">procedure</span> <span style="color: #008000; font-weight: bold;">division</span><span style="color: #000066;">.</span><br />
&nbsp; &nbsp; &nbsp; <span style="">*&gt;---Instantiate the class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> SimpleClass <span style="color: #ff0000;">&quot;new&quot;</span> <span style="color: #000000; font-weight: bold;">returning</span> theInstance<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">display</span> <span style="color: #ff0000;">&quot;instantiated - test 1&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> theInstance <span style="color: #ff0000;">&quot;TestException&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">display</span> <span style="color: #ff0000;">&quot;instantiated - test 2&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">invoke</span> theInstance <span style="color: #ff0000;">&quot;TestException&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000; font-weight: bold;">stop</span> <span style="color: #008000; font-weight: bold;">run</span><span style="color: #000066;">.</span></div></div>
<p>and the Java class itself:</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.*</span> <span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleClass <span style="color: #009900;">&#123;</span> <br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> SimpleClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> TestException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test error&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>References: <a href="http://supportline.microfocus.com/documentation/books/nx60/nx60indx.htm">Studio Enterprise 6.0 Document for COBOL/Java Interop</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gennard.net/blog/2009/11/object-coboljava-default-exception-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
