Notes/XPages - Debugando um Java Agent
Autor: Fernando Camargo | Publicado: 2011-02-28T11:16:00-03:00

Existe uma forma de debugar o Java Agent no Designer, mas é totalmente instável, o Designer trava e tem que ser fechado o tempo todo.
Então vamos usar o Eclipse para debugar nosso Agent Java. É recomendável que se deixe um eclipse apenas para isso, pois vamos mudar a JRE do Eclipse para usar a JRE do Notes. Caso use o mesmo Eclipse para outras aplicações Java, terá que ficar alterando o JRE padrão o tempo todo.

Primeiramente, crie um projeto java no eclipse como o nome que preferir. Crie a classe JavaAgent. Cole o seguinte código:
import lotus.domino.*;

public class JavaAgent extends AgentBase {

&nbsp;&nbsp;&nbsp; private Database database;
&nbsp;&nbsp;&nbsp; private Session session;

&nbsp;&nbsp;&nbsp; public JavaAgent() {

&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; public JavaAgent(Session s, Database d) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.session = s;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.database = d;
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; //
&nbsp;&nbsp;&nbsp; public static void main(String[] args) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Session sess=null; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Database db=null;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; JavaAgent ag;

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Initialize Notes
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NotesThread.sinitThread(); 

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sess = NotesFactory.createSession();&nbsp; // start a Notes session
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; db = sess.getDatabase("", 

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "nomeDaDatabase.nsf");&nbsp; // simulate home database for agent
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ag = new JavaAgent(sess, db);&nbsp; /* simulate agent object&nbsp; with session and database*/
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ag.NotesMain();&nbsp;&nbsp; // call main routine of agent
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; catch(NotesException ne) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(ne.id + " " + ne.text);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; catch (Exception e)&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; finally {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try { 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (db != null) db.recycle(); 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (sess != null) sess.recycle(); 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; catch (Exception x) {}
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NotesThread.stermThread();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; public void NotesMain() {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Session session = null;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AgentContext agentContext = null;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (this.session != null) // Already have an agent context.&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; session = this.session;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else {&nbsp; // Need to get agent context.&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; session = this.getSession();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (session==null) throw new 

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NotesException(NotesError.NOTES_ERR_ERROR,&nbsp; "Could not get current session at start of agent.");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; agentContext = session.getAgentContext();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (agentContext==null) throw new 

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NotesException(NotesError.NOTES_ERR_ERROR,&nbsp; "Could not get agent context at start of agent.");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.database = agentContext.getCurrentDatabase();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (this.database==null) throw new 

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NotesException(NotesError.NOTES_ERR_ERROR,&nbsp; "Could not get current database at start of agent.");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; catch(NotesException ne) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(ne.id + " " + ne.text);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; catch (Exception e)&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e.printStackTrace();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Your code goes here!!!
&nbsp;&nbsp;&nbsp; }
} &nbsp;Aparecerá erros em todos os objetos do Notes, pois ainda não colocamos a JVM/JRE do Notes.

Vamos mudar o JRE do Eclipse. Vá no menu: Window &gt; Preferences.



Vá em Java &gt; Installed JREs.



Clique em Add, selecione Standard VM, clique em Next. Em JRE home, clique em Directory e selecione o diretório da JVM do Notes. Exemplo: C:\Program Files\IBM\Lotus\Notes\jvm

Coloque um nome de sua preferência e clique em Finish. Agora marque a JVM do Notes como padrão.

Apenas dê OK.
Agora vamos para a segunda parte. Quando você estiver programando o Java Agent no Eclipse, ele já vai usar a JVM do Notes, estando disponíveis todos comandos vindos do Notes para Java Agent. Mas também precisamos usar essa JVM na hora de executar o programa e debugar.
Vá em Run &gt; Run Configurations.

Dê um clique duplo em  Java Application para criar uma nova configuração. Coloque o nome que quiser (usei JavaAgent), selecione seu projeto e a Main Class.

Vá na aba Arguments e, caso o Working Directory não esteja já definido para o diretório de seu Notes, clique em Other e o selecione.



Certifique-se de que em JRE esteja selecionado em Runtime JRE: Project JRE (&lt;nome que você colocou na jvm&gt;).

Clique em Apply e Close. Agora você pode programa e debugar seu Java Agent no Eclipse.
Agora apenas modifique a linha:
db = sess.getDatabase("", "nomeDaDatabase.nsf");colocando o nome/caminho de sua database.
Programe seu Java Agent apartir da linha:
//Your code goes here!!!

-------------------------------------------------------------
Fonte: http://www.bobzblog.com/tuxedoguy.nsf/dx/the-2-headed-beast-debugging-domino-java-agents-with-eclipse
Adaptação: Fernando Camargo