Chapter 4 Tutorial: ActiveX Components
To create the sample application:
This section shows you how to create the server-side component. The ActiveX component (AXArithmetic.dll) is included in your Jaguar installation. If you do not need or want to build the component, you can skip to "Verify that the component is registered".
Create the server-side component:
AXArithmetic
and
place it in a directory called c:\AXArithmetic.
Select Finish. Multiply
.
In the prototype, click between the parentheses, and add this text
to define the method parameters:double m1, double m2, double *result
Note
Since the last parameter is a pointer to
double
,
Power++ defines an IDispatch interface
in which the method returns double
.
After the component is imported into Jaguar, the Jaguar Manager
definition of the multiply method will return double
.
return
statement:*result = m1 * m2;
At this point, you are ready to build the component DLL. For more information on defining ActiveX components in Power++, see the Power++ Component Creation Guide.
When you are creating ActiveX DLLs, Power++ allows you to configure the Run command to build, register, and deploy the component DLL.
The steps below configure the Run menu so that the component DLL is registered, then copied, to the Jaguar dll directory.
Build and deploy the component DLL:
Verify that AXArithmetic.dll has been built in the c:\AXArithmetic\debug directory and copied to the Jaguar dll directory.
ActiveX component DLLs must be registered in the NT Registry before applications can call the DLLs. If you configured the Run options correctly, Power++ should have automatically registered your component.
In Jaguar, the component's name in Jaguar Manager must match the component's program ID (progid) that is stored in the NT Registry. In the steps below, you will confirm the component registration.
Verify that the component is registered:
regedt32
in the window. Click
OK. Start the Jaguar server:
Start Jaguar Manager:
This section shows you how to create a package for a new component, and import a component's method definition into the package. You can either import ActiveX method definitions from a type library (.tlb) file or a dynamic link library (.dll) file into Jaguar Manager, or you can manually enter method definitions. This tutorial imports method definitions from a type library file.
For more information about importing or creating method definitions and configuring packages and component properties, see the Jaguar CTS Programmer's Guide.
Packages provide a way of organizing related components in a Jaguar application. All components created in the Jaguar tutorials are installed in the Tutorial package. Follow these steps to create the Tutorial package if it does not already exist:
Create the Tutorial package:
Tutorial
.
You must install the AXArithmetic component on the Jaguar server before you can add methods to it.
Define the Jaguar ActiveX component:
AXArithmeticCtl
.
Click Create New Component.Once you have created the package, component, and methods, you need to generate the stub code used as a basis for creating the client-side applet that invokes the server-side component methods.
Generate stubs:
%JAGUAR%\html\classes
.Tutorial
.Change to the Jaguar html\classes\Tutorial subdirectory and compile the generated files using a JDK 1.1 compiler--for example:
cd %JAGUAR%\html\classes\Tutorial
d:\j11 *.java
Create a directory called %JAGUAR%\html\classes\TutorialApps if it does not exist. In this directory, create the Java file below as AXClient.java.
A copy of AXClient.java can be found in the html/docs/tutorial subdirectory of your Jaguar installation. Here is the source for AXClient.java:
//
// This is a sample client that invokes the
// AXArithmeticCtl component created in the Jaguar
// ActiveX component tutorial.
//
package TutorialApps;
import org.omg.CORBA.*;
import org.omg.CosNaming.*; // CORBA CosNaming interfaces
import org.omg.CosNaming.NamingContextPackage.*;
// CosNaming user exceptions
import SessionManager.*;
import java.awt.*;
import Tutorial.*; // Package for component stub classes
public class AXClient extends java.applet.Applet {
// User interface controls
Button mult_button;
TextField m1_text;
TextField m2_text;
TextField result_text;
// Component's name (relative to the server's
// initial name context), of the form package/component
String _compName = "Tutorial/AXArithmeticCtl";
// Component stub instance
Tutorial.AXArithmeticCtl _comp = null;
public void init()
{
// Draw GUI controls
m1_text = new TextField(" ");
m1_text.setText("2.5");
this.add(m1_text);
Label l1 = new Label("*");
this.add(l1);
m2_text = new TextField(" ");
m2_text.setText("3.1");
this.add(m2_text);
Label l2 = new Label("=");
this.add(l2);
result_text = new TextField(" ");
result_text.setEditable(false);
this.add(result_text);
mult_button = new Button("Multiply");
this.add(mult_button);
try {
//
// Initialize the CORBA client-side ORB and
// obtain a stub for the Jaguar component instance.
//
System.out.println("... Creating Jaguar session.");
//
// Initialize the ORB. Note that the org.omg.CORBA.ORBClass
// property must be set in applet parameters.
//
java.util.Properties props = new java.util.Properties();
ORB orb = ORB.init(this, props);
NamingContext nc = null;
org.omg.CORBA.Object objRef = null;
objRef = orb.resolve_initial_references("NameService");
nc = NamingContextHelper.narrow(objRef);
//
// Create an instance of the Jaguar SessionManager::Manager
// CORBA IDL object.
//
SessionManager.Manager manager = null;
NameComponent managerName[] =
{ new NameComponent("AuthenticationService", "") };
manager = ManagerHelper.narrow(
nc.resolve(managerName));
//
// Create an authenticated session with user "Guest"
// and password "GuestPassword".
//
SessionManager.Session session =
manager.createSession("Guest", "GuestPassword");
System.out.println("... Creating component instance.");
//
// Create a stub object instance for the
// Tutorial/AXArithmeticCtl Jaguar component.
//
NameComponent compNc[] =
{ new NameComponent(_compName, "") };
Factory compFactory =
FactoryHelper.narrow ( nc.resolve(compNc) );
_comp = Tutorial.AXArithmeticCtlHelper.narrow(
compFactory.create(session) );
System.out.println("... Created component instance.");
} catch (NotFound nfe) {
// This can happen if you have installed
// the tutorial component under a different
// package or component name in Jaguar Manager.
System.out.println("Error: Component " + _compName
+ " not found. Check the package and component "
+ "name in Jaguar Manager.\n");
System.out.println("NotFound exception details:\n");
nfe.printStackTrace();
_comp = null;
} catch (org.omg.CORBA.UserException ue) {
// Check for other CosNaming exceptions
System.out.println("CORBA CosNaming exception:\n"
+ ue.toString());
ue.printStackTrace();
_comp = null;
} catch (org.omg.CORBA.SystemException se) {
System.out.println(
"Received CORBA system exception "
+"while instantiating component:\n"
+ se.toString() );
se.printStackTrace();
_comp = null;
}
} // init()
// Handle button clicks
public boolean action(Event e, java.lang.Object arg) {
if (e.target == mult_button) {
doMultiply();
return true;
}
else return false;
}
// Call the multiply method and update the displayed result
private void doMultiply() {
// Harvest user input
Double d1 = null;
Double d2 = null;
try {
d1 = new Double(m1_text.getText());
d2 = new Double(m2_text.getText());
} catch (NumberFormatException nfe)
{
this.showStatus("ERROR: Bad number format.");
result_text.setText("");
return;
}
// Call the server component method
double m1 = d1.doubleValue();
double m2 = d2.doubleValue();
try {
double result = _comp.multiply(m1, m2);
result_text.setText((new Double(result)).toString());
}
catch (org.omg.CORBA.SystemException se)
{
System.out.println(
"Exception executing multiply method: "
+ se.toString() );
se.printStackTrace();
}
} // doMultiply
}
Compile this application using a JDK 1.1 compiler:
j11 AXClient.java
In a text editor, create an HTML file that calls the applet. Name the file runaxtut.html and create it in the %JAGUAR%\html\classes\TutorialApps subdirectory.
A copy of this file is located in your Jaguar installation directory, in the html/docs/tutorial subdirectory. As an alternative to creating the file, copy this file to your %JAGUAR%\html\classes\TutorialApps subdirectory. Here is the text of runaxtut.html:
<html><body bgcolor="#FFFFFF">
<head><title>This Applet runs the Jaguar tutorial ActiveX
component.</head></title>
<hr>
<center>
<applet
codebase="/classes"
code="TutorialApps/AXClient.class"
width=600 height=400>
<h2>This would be a Cool Applet,
but you are not running a Java enabled browser...</h2>
<!-- Specify the Java ORB implementation class -->
<param name = "org.omg.CORBA.ORBClass"
value = "com.sybase.CORBA.ORB">
<!-- Specify the URL for connections to the
Jaguar CosNaming service.
"iiop://:9000/" means to use port 9000 on the
applet's download host, with no initial
name context -->
<param name = "com.sybase.CORBA.NameServiceURL"
value = "iiop://:9000/">
</applet>
</center>
<hr>
</body></html>
No changes are required to this HTML file if your server is configured to use the default IIOP port number, 9000. If you (or your administrator) changed the IIOP port number after installing Jaguar, edit the port number in the HTML file to match.
You do not need to restart the Jaguar server before running the sample.
Classpath conflicts on development workstations
If you are running the sample client on a machine where EAServer
has been installed, you must configure your browser to eliminate
conflicts between Jaguar classes that are downloaded with the applet
and classes that are loaded from the local classpath. See "Configuring browsers to
run applets on development machines" for instructions.
Run the sample:
http://host:8080/classes/TutorialApps/runaxtut.html
Debug if necessary:
Copyright © 2000 Sybase, Inc. All rights reserved. |
![]() |