Step 1 - Install Bridge2Java
This is available for download at http://www.alphaworks.ibm.com/aw.nsf/techmain/bridge2java
It's free to play with, but if you want to use it commercially there is a $1000 dollar license fee.
Unzip the file and you are ready to go.
Step 2 - generating Java COM wrappers
In your Bridge2Java directory, edit proxyall.bat to point to the COM libraries you want to use.
I am going to use ADO and Microsoft Exchange 2000, so I add the following lines to the batch file:
rmdir /q /s cdoex
proxygenBridge2Java C:winntsystem32cdoex.dll %BRIDGE2JAVA% cdoex
rmdir /q /s ado
proxygenBridge2Java "C:program filescommon filessystemADOmsado15.dll" %BRIDGE2JAVA% ado
A few things to note: cdoex.dll is the Exchange server 2000 COM object library, msado15.dll is the ADO object library.
The above lines tell bridge2Java that I want to create a package called ado based on the ado library, and a package called
cdoex based on the Exchange library.
Save the batch file and edit BASEENV.bat to point to your bridge2Java directory and Java directory. Mine looks like this:
set BRIDGE2JAVA=C:javaBRIDGE2JAVA
set JAVAJDK=C:j2sdk1.4.0
@ECHO ON
set path=%path%;%BRIDGE2JAVA%
set CLASSPATH=%JAVAJDK%libclasses;%BRIDGE2JAVA%;.
Now open a command prompt, cd into your bridge2Java directory and run baseenv.bat followed by proxyall.bat - This will create a your ado and cdoex directories, full of java source files.
You are now ready to start coding.
Coding
You will need to have your ado and cdoex directories in your source tree, you will also need to include the com.ibm.bridge2java classes on your classpath
(You will find these classes under your bridge2java directory). You will also need to do the following:
Tell the JVM where it can find the bridge2java dll - you can do this by setting a system property via the java -D flag, or in code such as
System.setProperty("java.library.path","c:javaBridge2java");
The last thing you need to do is to remember to startup and shutdown the com bridge - see the main method in the code example.
Some hints:
Check out the following code:
Fields fields = new Fields(config.get_Fields());
config.getFields returns an object, if you do this in VB, the object returned is of type Field, so the natural thing to do in java would be:
Fields fields = (Fields)config.get_Fields());
If you do this you get a class cast exception - the object returned by the getFields method is actually an INTEGER!
The rule of thumb is that if a call to a COM object returns an Object, don't try to cast it to the target type, instead create a new Object of the target type and pass the returned Object into the constructor.
If you get an error complaining about link exceptions, Java can't find bridge2Java.dll - make sure the system property is set.
If you get ole errors, make sure you
have remembered to start the bridge.
Example
The following example is a simple mail client, It demonstrates the use of ADO and CDOEX to send a mail message through an Exchange server.
/**
* Demonstrates the use of IBM Bridge2Java with ADO and CDOEX
*/
public class Demo1
{
public void sendMail(String[] args)
{
Message message = new Message();
Configuration config = new Configuration();
// note that casting does not work - Instead,
// create a new object of the target type based on the returned object
// (Returned object is actually an Integer!)
Fields fields = new Fields(config.get_Fields());
Field field1 = fields.get_Item(new Integer(1));
// 3 means that you are asking mail to be sent using Exchange Server.
fields.get_Item("http://schemas.microsoft.com/cdo/configuration/sendusing").set_Value(new Integer(3));
fields.get_Item("http://schemas.microsoft.com/cdo/configuration/user").set_Value(args[0]);
fields.get_Item("http://schemas.microsoft.com/cdo/configuration/password").set_Value(args[1]);
fields.Update();
message.set_From(args[2]);
message.set_To(args[3]);
message.set_Subject(args[4]);
message.set_TextBody(args[5]);
message.Send();
}
public static void main(String[] args)
{
if(args.length!=6)
{
System.out.println("Usage java Demo1 mailbox_username mailbox_password from to subject message");
System.exit(0);
}
// need to tell the JVM where to find the bridge2Java.dll
// we also need to have the com.ibm.bridge2java classes on the classpath
System.setProperty("java.library.path","c:javaBridge2java");
//initialise the bridge
com.ibm.bridge2java.OleEnvironment.Initialize();
Demo1 d = new Demo1();
d.sendMail(args);
//shut down the bridge
com.ibm.bridge2java.OleEnvironment.UnInitialize();
}
}
This article was originally written by pertinax |