|[ Team LiB ]|
Both COM and Java technologies support the notion of an event—something occurring dynamically at run time that a listening thread can take action on. It should come as no surprise that the mechanisms each uses to accomplish this are sufficiently incompatible. Here we'll again look to jCOM to come to the rescue and provide a transparent way of conversing between the two.
A COM class defines an event through the use of a interface—more specifically, a source interface. COM classes that implement the interface use it to subscribe to the events defined therein.
Java events are defined by an interface derived from java.util.EventListener. Java event handlers are used to respond to events. You register the events you want to have handled with a specific event handler.
Although perhaps an oversimplification, for our purposes what is important to know is that jCOM allows COM objects to subscribe to Java events as though they were COM events. In turn, Java objects can subscribe to COM events using standard Java mechanisms.
The com2java tool is used to hide COM event implementations from view. It does this by reading the associated type library and rendering Java equivalents for each source interface it finds. When com2java finds an interface, at a minimum, there must be one COM class that uses the interface as a source interface. It then adds two methods to the generated Java class. It inserts a standard Java addAppEventsListener and removeAppEventsListener method in a manner familiar to Java programmers when subscribing to events in Java. A corresponding java.util.EventListener interface is then generated for the class.
It is a requirement of the Java event-driven model that each method in an interface have a single parameter that is an instance of a class derived from the java.util.EventObject class. To comply with this, java2com generates an additional class for each method in an event interface to hold the parameters.
The last Java convention that java2com must contend with is the use of an adapter class. In Java, the adapter class provides empty default implementations for each method in the interface. This enables a class to be created from the interface without having to implement all the methods in the interface, which with some interfaces can become quite lengthy. java2com therefore generates an adapter class for each event interface it finds.
For COM clients accessing event-driven Java objects, the java2com tool generates source interfaces for all Java events found in a Java class. These will be made available to the COM client through the generated type library, which can be used to subscribe to events in the standard COM way. Event-driven programs are thus supported.
|[ Team LiB ]|