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