View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo;
24  
25  import java.io.PrintStream;
26  import java.io.PrintWriter;
27  
28  import lotus.domino.AgentBase;
29  import lotus.domino.Session;
30  import de.bea.domingo.exception.ExceptionUtil;
31  import de.bea.domingo.i18n.ResourceManager;
32  import de.bea.domingo.i18n.Resources;
33  import de.bea.domingo.monitor.AbstractDefaultMonitor;
34  import de.bea.domingo.monitor.AbstractMonitor;
35  
36  
37  /***
38   * Domingo agents must extend DAgentBase and use run() as the entry point for their
39   * functional code. Use {link #getDSession() getDSession()} to get a DSession
40   * object. For output to browsers as well as Notes clients (the Java debug
41   * console), create a PrintWriter object with
42   * {@link #getAgentOutput() getAgentOutput()}.
43   *
44   * @deprecated use new class {@link de.bea.domingo.notes.DAgentBase} instead
45   *
46   * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
47   */
48  public abstract class DAgentBase extends AgentBase {
49  
50      /*** Reference to the Notes Session. */
51      private DSession session;
52  
53      /*** Reference to a Domingo Monitor. */
54      private NotesAgentMonitor monitor;
55  
56      /*** Whether access to the Notes session is allowed or not. */
57      private boolean isSessionProtected = false;
58  
59      /*** Internationalized resources. */
60      private static final Resources RESOURCES = ResourceManager.getPackageResources(DAgentBase.class);
61  
62      /***
63       * Constructor.
64       */
65      public DAgentBase() {
66          super();
67      }
68  
69      /***
70       * main method to be implemented by concrete agents.
71       */
72      public abstract void main();
73  
74      /***
75       * @see lotus.domino.AgentBase#NotesMain()
76       */
77      public final void NotesMain() {
78          DNotesFactory factory = null;
79          try {
80              monitor = new NotesAgentMonitor(AbstractMonitor.WARN);
81              factory = DNotesFactory.getInstance("de.bea.domingo.proxy.NotesProxyFactory", monitor);
82              session = factory.getSession(super.getSession());
83          } catch (Exception e) {
84              getMonitor().fatalError(RESOURCES.getString("agent.init.failed"), e);
85          }
86          try {
87              isSessionProtected = true;
88              main();
89          } catch (DNotesRuntimeException  e) {
90              getMonitor().fatalError(RESOURCES.getString("agent.run.failed"), e);
91          } finally {
92              isSessionProtected = false;
93          }
94          try {
95              factory.disposeInstance(true);
96          } catch (Exception e) {
97              getMonitor().fatalError(RESOURCES.getString("agent.dispose.failed"), e);
98          }
99      }
100 
101     /***
102      * Returns the Domingo session of the agent.
103      *
104      * @return Domingo session
105      */
106     protected final DSession getDSession() {
107         return session;
108     }
109 
110     /***
111      * This method should not be called in Domingo agents. and always returns a runtime
112      * exception if a Domingo agent tries to get the internal Notes session.
113      *
114      * @return the underlying Notes session.
115      * @deprecated use method getDSession()
116      */
117     public final Session getSession() {
118         if (isSessionProtected) {
119             throw new DNotesRuntimeException(RESOURCES.getString("agent.illegal.access"));
120         } else {
121             return super.getSession();
122         }
123     }
124 
125     /***
126      * Returns the monitor of the current Domingo agent.
127      *
128      * @return monitor of current Domingo agent
129      */
130     public final DNotesMonitor getMonitor() {
131         return monitor;
132     }
133 
134     /***
135      * Adapter from a {@link de.bea.domingo.DNotesMonitor DNotesMonitor} to
136      * the debug system of Lotus Notes.
137      *
138      * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
139      */
140     private class NotesAgentMonitor extends AbstractDefaultMonitor {
141 
142         /***
143          * Constructor.
144          *
145          * @param theLevel the level of the new monitor, can be one of
146          * {@link de.bea.domingo.monitor.AbstractMonitor#DEBUG DEBUG},
147          * {@link de.bea.domingo.monitor.AbstractMonitor#INFO INFO},
148          * {@link de.bea.domingo.monitor.AbstractMonitor#WARN WARN},
149          * {@link de.bea.domingo.monitor.AbstractMonitor#ERROR ERROR} or
150          * {@link de.bea.domingo.monitor.AbstractMonitor#FATAL FATAL}
151          */
152         public NotesAgentMonitor(final int theLevel) {
153             super(theLevel);
154         }
155 
156         /***
157          * Constructor.
158          *
159          * @param base the calling Notes agent
160          * @param theLevel the level of the new monitor
161          */
162         public NotesAgentMonitor(final DAgentBase base, final int theLevel) {
163             super(theLevel);
164         }
165 
166         /***
167          * @see de.bea.domingo.monitor.AbstractDefaultMonitor#monitor(java.lang.String)
168          */
169         protected void monitor(final String message) {
170             dbgMsg(message);
171         }
172 
173         /***
174          * @see de.bea.domingo.monitor.AbstractDefaultMonitor#monitor(java.lang.Throwable)
175          */
176         protected void monitor(final Throwable throwable) {
177             if (throwable != null) {
178                 dbgMsg(ExceptionUtil.getStackTrace(throwable));
179             }
180         }
181     }
182 
183     /***
184      * If debugging is enabled, send a debug message to the specified stream.
185      *
186      * @param message the message
187      * @param out the output stream
188      * @see #setDebug(boolean)
189      */
190     public final void dbgMsg(final String message, final PrintStream out) {
191         super.dbgMsg(message, out);
192     }
193 
194     /***
195      * If debugging is enabled, send a debug message to the specified writer.
196      *
197      * @param message the message
198      * @param out the output writer
199      * @see #setDebug(boolean)
200      */
201     public final void dbgMsg(final String message, final PrintWriter out) {
202         super.dbgMsg(message, out);
203     }
204 
205     /***
206      * If debugging is enabled, send a debug message to <tt>System.out</tt>.
207      *
208      * @param message the message
209      * @see #setDebug(boolean)
210      */
211     public final void dbgMsg(final String message) {
212         super.dbgMsg(message);
213     }
214 
215     /***
216      * Returns the output writer of the current agent.
217      *
218      * @return output writer of the current agent
219      */
220     public final PrintWriter getAgentOutput() {
221         return super.getAgentOutput();
222     }
223 
224     /***
225      * Checks if the current agent is a restricted agent or not.
226      *
227      * @return <tt>true</tt> if the current agent is restricted, else <tt>false</tt>
228      */
229     public final boolean isRestricted() {
230         return super.isRestricted();
231     }
232 
233     /***
234      * Sets whether debugging is enabled or not for the current agent.
235      *
236      * @param b <tt>true</tt> if debugging should be enabled, else <tt>false</tt>
237      */
238     public final void setDebug(final boolean b) {
239         super.setDebug(b);
240     }
241 
242     /***
243      * Activates or deactivates tracing method calls of the JVM.
244      *
245      * @see java.lang.Runtime#traceMethodCalls(boolean)
246      *
247      * @param b <code>true</code> to enable instruction tracing;
248      *            <code>false</code> to disable this feature.
249      */
250     public final void setTrace(final boolean b) {
251         super.setTrace(b);
252         monitor.setLevel(AbstractMonitor.DEBUG);
253     }
254 }