The technique for building a better Notes Java API
This is a followup post to yesterdays post titled Want to join me in building a better Notes Java API? and here I'll show just how easy it was to build the wrapper API.
Below is the code from the implementation of the getView(String) method of the lotus.domino.Database interface. As you can see the central element is to get the current thread the method is called on and the thread used for Notes data access. If we're not on the Notes thread execute a blocking request and return the result. If we're on the Notes thread simply go ahead and do the operation. Simple right?
The most difficult part of all this was that I had to write my own Notes thread handler (equivalent of NotesPlatform) as there isn't a method for doing a blocking request on the Notes thread in NotesPlatform class supplied by Lotus.
public View getView(final String name) throws NotesException {
// get threads
Thread curThread = Thread.currentThread();
Thread notesThread = NotesPlatform.getInstance().getThread();
// decide if we're in the Notes thread or not
if (curThread != notesThread) {
// we're not on the Notes thread so wrap
final Holder h = new Holder();
NotesPlatform.getInstance().syncExec(new Runnable() {
public void run() {
try {
h.value = DatabaseWrapper.this.db.getView(name);
} catch (NotesException e) {
h.throwable = e;
}
}
});
if (null != h.throwable) {
throw (NotesException)h.throwable;
} else {
return new ViewWrapper((View)h.value);
}
} else {
// we're on the thread so do request
return new ViewWrapper(DatabaseWrapper.this.db.getView(name));
}
}
Re: The technique for building a better Notes Java API
Re: The technique for building a better Notes Java API
The best solution is to create a new session for every plugin, and for long-running plugins also for every Notes data access and dispose it afterwards. That way it's also easier to handle users switching the location/Notes ID, because your code is not full of stored/cached Notes objects that belong to a specific user context (I regret that I did not think more about this a few years ago ;-) ).
Always creating a new Notes session correctly handles switched Notes IDs and is best for memory management.





