Monday, August 20, 2007

Extending Domino with Java - Part 4 - Integration

I was inspired by Stephen Wissels JMeter post to finally finishing up the Extending Domino with Java posts. If you want to skip to the finish line, then you can download the finished code examples in a Domino database from openntf code bin.

So you have the HTTPFacade class working nicely in eclipse - how do you get that running in Domino. We could create an agent and attach, the and supporting jar files - but if you created another agent to do the same and attach all of the file again. So I decided to create a reusable Java Library in Domino. If you have never done this before, don't worry
its just as simple as the LS libraries.

I created a library called HTTPLibrary and pasted the HTTPFacade java code from eclipse, of course this won't compile just now. In eclipse I referenced the apache commons HTTPClient and dependant jar files - we need to do the same in Domino.

Click 'Edit Project' and add/replace the jar files from your local disk into the java library. It should look something like;

That's great but lets get a simple example working in Domino. Start with a Java agent. Create a new one, which has all the stub code. How do we reference the shared library ? simple. Just click 'Edit Project' the in the 'Browse' dropdown, then choose 'Shared Java Libraries'. You should see 'HTTPLibrary' as an option. Add that library to the project.

You can then start using the HTTPFacade class in the agent (see the JavaLibraryExample agent in the sample database).

dhttp = new HTTPFacade("");
dhttp.setWebsiteCredentials("username", "password");
String url = "";
returnCode = dhttp.executeMethod(url);

When you save the agent you should get a 'Successful compile." message if the library contains the correct jar files and is referenced.

Now try and run the Agent. What happens ? nothing ? The output is sent to the standard system.out stream. In the Notes client you need to open that stream by choosing 'File > Tools > Show Java Debug Console'. This will open the Java console and you should see the HTML output from you agent.

That's good - but what if I want to use this outside the context of an agent, in a button and in LotusScript. You can invoke Java from LotusScript using LS2J. We'll use this for the final example of integrating into a form. What we would like is that when we click on a button, we'll display the return code in a dialog and then populate a field with the HTML.

In order to use LS2J you need to add the following line into the button's option script.
While you are there add the HTTPLibrary too. The options should look like

Uselsx "*javacon"
Use "HTTPLibrary"

You will need to create a JavaSession, JavaClass, JavaError and a JavaObject variables, plus a few others (there is example in the Designer help). The below code snippet show the examples and how to create a reference to a Java object, set properties and execute a method - all without leaving lotus script.

Set mySession = New JavaSession()
Set myClass = mySession.GetClass("")
Set dhttp = myClass.CreateObject()

Call dhttp.setHost("")

sXML = dhttp.getXML()
ret=Messagebox(Cstr(iReturn), 0,Cstr(iReturn))

Call uiws.CurrentDocument.FieldSetText("xml",sXML)
Call dhttp.releaseConnection()

That's it, a simple interface to an already existing 'robust HTTP class library' that you can easily reuse in LotusScript or Java in Domino. You can also use the, and java.util packages to create your own HTTPClient as in Julians urlFetcher database.....or maybe just use his database.

I guess what you can take away from this series of posts is that there is a large volume of work in the java world that as a Domino developer you can take advantage of. I hope that from the examples you can see that it's not that hard. With the new version of Notes 8 coming and the composite application model, there is only going to be more that is available to us to produce innovative client and web based applications.