Accessing COM Components from the NetDynamics Application Server |
This example demonstrates how to access COM Components from the NetDynamics Application Server. J-Integra® for COM is a Java interoperability component that bridges Java objects hosted on NetDynamics and Microsoft COM components. It provides bi-directional access of Java objects and COM components.
You may read this example simply to get a feel for how easy it is to use J-Integra® to access COM components from the NetDynamics Application server, however we urge you to try out this example. Although it appears that there are a lot of steps, most of them are trivial -- we have included such a lot of detail in order that no matter your experience, you will be able to follow the example.
The component you will create in this example is an Visual C++ ATL COM component in an Executable server.
Since J-Integra® can be used to access COM components from any JVM running on any platform, the NetDynamics application you create should execute in a NetDynamics Application Server running on any platform, including Solaris.
Prior to doing this example, please read the J-Integra® Excel example, since we introduce in that example some basic concepts that are assumed here.
If you do wish to try out this example, you should first download and install the Java Developers Kit.
You should download and install J-Integra®.
You must also have installed the NetDynamics kit. This example assumes you are using NetDynamics version 4.1.2
This example requires access to a machine that is running Windows NT and has Visual C++ installed on it.
This example assumes you are using Visual Studio 97.
This example assumes that you have installed the JDK under D:\JDK1.3.1, J-Integra® under D:\jintegra, and NetDynamics under D:\NetDynamics.
Start up Visual C++, and create a new project.
Create a project of type ATL COM AppWizard, enter D:\pure as the the Location, and enter inventory as the Project name -- the Location will be automatically updated to include the project name:
Click OK.
In Step 1, set the Server Type to Executable:
Click on Finish, and then click OK to create the new project
To create a new ATL Object, click Insert>New ATL Object:
Click the Simple Object icon:
Click Next.
Enter Stock as the Short Name:
Click OK.
Click inventory classes. Right-click on IStock, and select Add Method to add a new method to the interface:
Enter quantity as the Method Name, and enter [in] BSTR item, [out] long* amount as the Parameters:
Click OK
Click IStock to expand the tree. Double click quantity(BSTR item, long * amount), to bring up the corresponding code:
Update the implementation of the quantity method.
As you may have guessed, we are creating a simple business-logic type component, representing an inventory system. The quantity method can be used to determine how many instances of a specific item in stock. In this example the inventory is somewhat depleted, and indeed the only item that still appears to be in stock is aardvarks:
// Stock.cpp : Implementation of CStock |
To build your server, click the Build menu, then click Build inventory.exe:
The status window will indicate a succesful build
and registration of your server:
Read the Excel Example for a full description of what is happening here.
Use the com2java tool to generate Java proxies which can be used to access the COM Component you just created, from any standard JVM:
Open D:\pure\inventory\inventory.tlb as the type library -- this file was automatically created by Visual C++ when you built your project:
Enter D:\pure as the output directory
Click the Generate Proxies button
You may wish to examine the three files that were generated:
IStock.java is a Java interface corresponding to the IStock COM interface
IStockProxy.java is a Java class which can be used to access remote COM objects that implement the IStock COM interface
Please read Configuring DCOM for Remote Access for details on configuring DCOM.
You should change the properties
on your COM Component, which will be listed under Stock
Class:
Click the Start button, then click Settings>Control Panel. Double click the System icon. Click the Environment tab, and select NETDYN_CLASSPATH from the System Variables:
Append ;d:\jintegra\lib\jintegra.jar;d:\pure to the definition of the variable, and click on Set and then Apply:
Reboot your machine in order for the changes to take effect.
Start the NetDynamics studio, and create a new project:
Set the Project Name to nd2activex
Click Create.
Start with a blank page
Click Next and Finish.
Change the Name of the new page in its properties page to NetDynamics2COM:
Click the HTML tab to use the HTML tools:
Create a new Text Box:
Use the new text box's property page to set its Label and Name to Item:
Create a new Button:
The button's action will be a Built-in Action:
Click on Next.
Select the Execute Built-in Action:
Click Next.
Set the Format String to Inventory:
Click Next.
Set the Name to Inventory:
Click Finish.
Click the new button's Event tab, and double-click on OnWebEvent to bring up the code that is invoked when the user submits the form by clicking on the button:
Modify the code as follows. The lines in bold are lines to be added. Don't forget to change the domain, user and password to be used to access the component.
In real life, you would not hard code the domain, user and password. In addition, you might create and store the reference to the Inventory COM component at the User Session or Global level, so that it is not created and released with every request.
// This file has been generated by NetDynamics
Studio |
Build the project by clicking Build>Compile All:
Run the example by clicking Build>Test Run:
Your Web Browser should start up. You can now query the inventory system:
Any Wombats?
No!
How about Aardvarks?
Yes!