BEA Home | Events | Solutions | Partners | Products | Services | Download | Developer Center | WebSUPPORT
| WebLogic Server Doc Home | WebLogic jCOM Reference Guide | Previous Topic | Next Topic | Contents | View as PDF

Using COM Objects from Java

In this section we describe how you can create an instance of a COM object, and use it from Java using WebLogic jCOM. We also explain how to deal with new references to COM objects that are returned from method calls to COM objects:

- Java classes generated from COM classes by com2java

- Java interfaces & classes generated from COM interfaces by com2java

You may wish to read through the documentation on the com2java tool prior to reading this section of the documentation.

Java classes generated from COM classes by com2java

For each COM Class that the com2java tool finds in a type library, it generates a Java class which can be used to access the COM Class. These generated Java classes have several constructors:

- The default constructor, which creates an instance of the COM Class on the local host, with no authentication

- A second constructor, which creates an instance of the COM Class on a specific host, with no authentication

- A third constructor, which creates an instance of the COM Class on the local host, with specific authentication

- A fourth constructor, which creates an instance of the COM Class on a specified host, with specific authentication

- A final constructor, which can be used to wrap a returned object reference which is known to reference an instance of the COM Class

The default constructor

The default constructor can be used to create an instance of a COM object on the local machine using the default authentication if it has been set, or using no authentication if no default has been defined.

The second constructor (String host)

The second constructor can be used to create an instance of a COM object on a specific host using the default authentication if it has been set, or using no authentication if no default has been defined.

The third constructor (AuthInfo authInfo)

The third constructor can be used to create an instance of a COM object on the local host using the specified authentication.

The fourth constructor (String host, AuthInfo authInfo)

The fourth constructor can be used to create an instance of a COM object on the specified host using the specified authentication.

The final constructor (Object objRef)

This final constructor does not actually create a new instance of the COM Class. Instead, it can be used to access a reference to the COM class that was returned from another COM Class (from a method call, or via a property or event).

If a method or property returns a reference to a COM Class, then the com2java tool will automatically generate a method signature which returns the appropriate Java Class, if the returned COM Class is defined in the same type library.

Java interfaces & classes generated from COM interfaces by com2java

A method in a COM interface may return a reference to an object through a specific interface.

For example the Excel type library (Excel8.olb) defines the _Application COM Interface, with the method Add which is defined like this in COM IDL:

>     [id(0x0000023c), propget, helpcontext(0x0001023c)]
>     HRESULT Workbooks([out, retval] Workbooks** RHS);

The method returns a reference to an object that implements the Workbooks COM interface. Because the Workbooks interface is defined in the same type library as the _Application interface, the com2java tool generates the following method in the _Application Java interface it creates:

>     /**
>     * getWorkbooks.
>     *
>     * @return return value. An reference to a Workbooks
>     * @exception java.io.IOException If there are communications
>     problems.
>     * @exception com.bea.jcom.AutomationException If the remote server
>     throws an exception.
>     */
>     public Workbooks getWorkbooks () throws java.io.IOException,
>     com.bea.jcom.AutomationException;

It is revealing to look at the implementation of the method in the generated _ApplicationProxy Java class:

>     /**
>     * getWorkbooks.
>     *
>     * @return return value. An reference to a Workbooks
>     * @exception java.io.IOException If there are communications
>     problems.
>     * @exception com.bea.jcom.AutomationException If the remote
>     server throws an exception.
>     */
>     public Workbooks getWorkbooks () throws java.io.IOException,
>     com.bea.jcom.AutomationException{ com.bea.jcom.MarshalStream
>     marshalStream = newMarshalStream("getWorkbooks");
>     marshalStream = invoke("getWorkbooks", 52, marshalStream);
>     Object res = marshalStream.readDISPATCH("return value");
>     Workbooks returnValue = res == null ? null : new
>     WorkbooksProxy(res);
>     checkException(marshalStream,
>     marshalStream.readERROR("HRESULT"));
>     return returnValue;
>     }

As you can see, the method internally makes use of the generated WorkbooksProxy Java class. As mentioned above, the com2java tool generates the method with the Workbooks return type because the Workbooks interface is defined in the same type library as _Application.

If the Workbooks interface were defined in a different type library, WebLogic jCOM would have generated the following code:

>     /**
>     * getWorkbooks.
>     *
>     * @return return value. An reference to a Workbooks
>     * @exception java.io.IOException If there are communications
>     problems.
>     * @exception com.bea.jcom.AutomationException If the remote server
>     throws an exception.
>     */
>     public Object getWorkbooks () throws java.io.IOException,
>     com.bea.jcom.AutomationException;

In this case, you would have to explicitly use the generated proxy class to access the returned Workbooks:

>     Object wbksObj = app.getWorkbooks();
>     Workbooks workbooks = new WorkbooksProxy(wbObj);

Copyright © 2001 BEA Systems, Inc. All rights reserved. Required browser: Netscape 4.0 or higher, or Microsoft Internet Explorer 4.0 or higher.