This section describes the development process for ActiveX controls, automation servers, or any other server that complies with COM specifications. The following example implements a simple Automation server that performs integer addition. We use the Setaddend () method to set the Addend value. Each time the sum () method is invoked, the Addend is added to the current result. We use GetResult () to get the result value and reset the value with clear (). The Java class used to implement this behavior is very simple:
public class Adder {
private int addend;
private int result;
public void Setaddend (int a) {addend = A;}
public int getaddend () {return addend;}
public int GetResult () {return result;}
public void sum () {result + = Addend; }
public void Clear () {result
= 0;
Addend = 0;
}
}
To use this Java class as a COM object, we apply the JavaReg tool to the compiled Adder.class file. This tool provides a range of options; In this case, we specify the Java class filename ("Adder"), the ProgID ("Javaadder.adder.1") that is placed in the registry for this server, and the name that you want to specify for the type library that is about to be generated (" Javaadder.tlb "). Because the CLSID has not been given, JavaReg will automatically generate one. If we call JavaReg on the same server again, we will use the ready-made CLSID directly.
Javareg/register
/class:adder/progid:javaadder.adder.1
/typelib:javaadder.tlb
JavaReg also registers the new server with the Windows registry. At this point, we must remember to copy the Adder.class to the Windows\java\trustlib directory. For security reasons, especially if the program calls COM services, these servers are activated only if the COM server is already installed to the Trustlib directory.
Now we have a new Automation server installed on our system. For testing purposes, we need a automation controller, and the Automation controller is visual Basic (VB). In the following, you will see a few lines of VB code. In VB format, I set up a text box to use it to receive the added value from the user. The results are displayed with a label, and the sum () and clear () methods are called separately with two push buttons. In the beginning, we declared an object variable named Adder. In Form_Load thread (loaded when the form is first displayed), a new instance of the Adder automatic server is invoked and the text field of the form is initialized. Once the user presses the "Sum" or "clear" button, the corresponding method in the server is invoked.
Dim Adder as Object
Private Sub form_load ()
Set Adder = CreateObject ("Javaadder.adder.1")
Addend.text = Adder.getaddend
result.caption = Adder.getresult End
Sub
Private Sub sumbtn_click ()
adder.setaddend (Addend.text)
Adder.sum
result.caption = Adder.getresult End
Sub
Private Sub clearbtn_click ()
adder.clear
Addend.text = Adder.getaddend
Result.caption = Adder.getresult End
Sub
Note that this code does not know that the server is implemented in Java.
After you run this program and call the CreateObject () function, you will search for the specified ProgID in the Windows registry. The most important of the information related to ProgID is the name of the Java class file. As a response, the Java Virtual machine is started and an instance of the Java object is called inside the JVM. From that point on, the JVM automatically takes over the communication between the client and the server code.