PreviousJava Data Types Interfacing with Microsoft Transaction ServerNext

Chapter 9: Combination ActiveX/Java Classes

This chapter describes how you can create Object COBOL classes that can be used as either Java classes or ActiveX servers.

9.1 Overview

You can create Object COBOL classes that can be used either by Java as Java classes or through OLE automation as ActiveX classes. Before reading this chapter you should familiarize yourself with the Java and OLE automation domains by reading the chapters OLE Automation and DCOM and Calling Object COBOL from Java.

The support that enables you to create combination ActiveX/Java classes is provided through an Object COBOL class called componentbase. ActiveX servers usually inherit from a class called olebase, and Java classes inherit from a class called javabase.

If you change the inheritance of one of these classes to componentbase it can be used within either the Java or OLE automation domains. The domain to which your class is available depends on how it was first loaded within a process.

9.2 Creating a Class

Net Express does not provide wizard support for creating combination ActiveX/Java classes. The simplest way of working is to use the class wizard to create your class for OLE automation, and create the Java wrapper class and methods manually as described in the section Writing a Java Class in Object COBOL of chapter Calling Object COBOL from Java.

This way the IDL, GUIDs and registry entries for your class get generated for you by the wizard. The Java code is easier to write by hand than the IDL files for OLE automation.

9.2.1 Linking the Class

In order to be usable as an ActiveX server, you must link the class into a .dll file, and link it with dllserver.obj. This does not prevent it from being loaded and called from Java. If you use the Net Express class wizard to generate the class as an OLE automation server, it sets up the correct linking for you. However, you must ensure that the .dll file is also linked with the multi-threaded run-time, as this is required for all classes to be used with Java.

9.3 Mixed Transaction Server Component/Enterprise Java Bean

This technology also enables you to create an Object COBOL class that can be used as a Microsoft Transaction Server (MTS) Object or as an Enterprise Java Bean (EJB). As in the previous section, we recommend that you use the Net Express wizard to create a Transaction Server class, and code Java wrapper and interface classes by hand. You also need to add the methods required for Enterprise Java Beans:

The objectcontext class supplied for working with MTS supports cross-domain programming for EJB. So when MTS code which uses the objectcontext class is executed in an EJB environment, it is either ignored by the objectcontext object, or it carries out the equivalent EJB operation.

However, you must make two minor changes to code generated by the method wizard in your mixed MTS/EJB class. First, change each occurrence of:

 invoke objectcontext "getObjectContext" returning theContext

to

 invoke objectcontext "getTransactionContext" using self 
                                          returning theContext

This enables the objectcontext to determine whether the object has been loaded as an EJB or as an MTS object.

Second, change the OOCTRL compiler directive at the top of your class to: :

$set ooctrl(+p-f) 

9.4 Determining the Execution Context

There are times when you need to determine programatically whether a class is executing as a Java or OLE domain object. You can do this by comparing the componentbase with the olebase and javabase classes, as shown in the example below:

 class-control.
     olebase is class "olebase"
     javabase is class "javabase" 
     componentbase is class "componentbase" 
     ...
     .
	
 procedure division.
     if componentbase = olebase
         display "Running as OLE automation server" 
     else 
         if componentbase = javabase 
             display "Running as a Java class" 
         end-if
     end-if 

You can also use a user-defined declaration in the Class-Control Section to create a different type of object depending on the execution context:

 class-control. 
     Account is class accountName
     ... 
     .

 procedure division. 
     if componentbase = olebase
*>       Create an OLE domain object     
         move "$OLE$Bank.Account" to accountName
     else
*>       Create a Java domain object 
         move "$Java$Account" to accountName
     end-if 


Copyright © 2000 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

PreviousJava Data Types Interfacing with Microsoft Transaction ServerNext