
Suppose we have this new AgentBase implementation – what we really like is for it to revert back to the lotus.domino.AgentBase functionality whenever the agent is run inside Notes/Domino and only use our new AgentBase look-a-like whenever we run inside Eclipse. Part 2 discussed how to detect where the code is running so by now we have a member variable called pIsNotes that is true if inside Notes/Domino and false otherwise.
To “fool” the agent when running inside Eclipse we need to override the getSession() and the getPrintWriter() methods of the AgentBase class with an implementation that works in Eclise. If we don’t the agent will fail with a NullPointerException whenever the agent is run.
Using our pIsNotes member variable it is almost trivial to create a java.io.PrintWriter that maps to System.out and create a lotus.domino.Session using lotus.domino.NotesFactory whenever we are outside Notes/Domino. When inside Notes/Domino we simply call the super-implementation that is the code of the AgentBase class supplied by IBM:
public abstract class EclipseAgentBase extends AgentBase {
// declarations
private boolean pIsNotes = false;
private Session pSession = null;
{
try {
Class.forName("lotus.domino.servlet.DominoSessionContext");
this.pIsNotes = true;
} catch (ClassNotFoundException e) {
// ignore - we're not in Notes/Domino
}
}
public abstract void NotesMain();
protected boolean isNotes() {
return this.pIsNotes;
}
public PrintWriter getAgentOutput() {
try {
if (this.pIsNotes) {
return super.getAgentOutput();
} else {
return new PrintWriter(new OutputStreamWriter(System.out, "ISO-8859-1"));
}
} catch (Exception e) {
throw new RuntimeException("Unable to convert System.out to PrintWriter", e);
}
}
public Session getSession() {
try {
if (this.pIsNotes) {
return super.getSession();
} else {
if (null == this.pSession) {
this.pSession = NotesFactory.createSession();
}
return this.pSession;
}
} catch (NotesException e) {
throw new RuntimeException("Unable to create session", e);
}
}
...
...
}
In the next part we’ll dive a little deeper by looking at a crucial part we are missing – the AgentContext returned by the Session object. At present it simply returns null which won’t work when testing agents.
Stay tuned.