KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > qfs > apps > qflog > RemoteLogLevelCallbackAdapter


1 // {{{ copyright
2

3 /********************************************************************
4  *
5  * $Id: RemoteLogLevelCallbackAdapter.java,v 1.1 2000/07/04 13:47:31 gs Exp $
6  *
7  * The contents of this file are subject to the Mozilla Public
8  * License Version 1.1 (the "License"); you may not use this file
9  * except in compliance with the License. You may obtain a copy of
10  * the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  * implied. See the License for the specific language governing
15  * rights and limitations under the License.
16  *
17  * The Original Code is qfs.de code.
18  *
19  * The Initial Developer of the Original Code is Gregor Schmid.
20  * Portions created by Gregor Schmid are
21  * Copyright (C) 2000 Quality First Software, Gregor Schmid.
22  * All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  *******************************************************************/

27
28 // }}}
29

30 package de.qfs.apps.qflog;
31
32 // {{{ imports
33

34 import java.rmi.RemoteException JavaDoc;
35
36 import de.qfs.lib.log.Log;
37 import de.qfs.lib.log.LogLevelCallback;
38 import de.qfs.lib.log.LogLevelEvent;
39 import de.qfs.lib.log.LogLevelListener;
40 import de.qfs.lib.logrmi.RemoteLogLevelCallback;
41 import de.qfs.lib.logrmi.RemoteLogLevelListenerImplBase;
42
43 // }}}
44

45 /**
46  *
47  *
48  * @author Gregor Schmid
49  * @version $Revision: 1.1 $
50  */

51 public class RemoteLogLevelCallbackAdapter
52     extends RemoteLogLevelListenerImplBase
53     implements LogLevelCallback
54 {
55     // {{{ variables
56

57     /**
58      * Maximum number of failures.
59      */

60     private final static int MAX_FAILURES = 3;
61
62     /**
63      * The LogLevelListener to forward LogLevelEvents to.
64      */

65     LogLevelListener listener;
66
67     /**
68      * The RemoteLogLevelCallback to forward callbacks to.
69      */

70     RemoteLogLevelCallback callback;
71
72     /**
73      * Number of failures when calling the remote callback. When this reaches
74      * MAX_FAILURES, the callback is disconnected.
75      */

76     private int failures;
77
78     // }}}
79

80     // {{{ RemoteLogLevelCallbackAdapter
81

82     /**
83      * Create a new RemoteLogLevelAdapter.
84      *
85      * @throws RemoteException
86      */

87     public RemoteLogLevelCallbackAdapter()
88         throws RemoteException JavaDoc
89     {
90     }
91
92     /**
93      * Create a new RemoteLogLevelAdapter.
94      *
95      * @param listener The LogLevelListener to forward LogLevelEvents to.
96      *
97      * @throws RemoteException
98      */

99     public RemoteLogLevelCallbackAdapter(LogLevelListener listener)
100         throws RemoteException JavaDoc
101     {
102         this.listener = listener;
103     }
104
105     // }}}
106

107     // {{{ getListener
108

109     /**
110      * Get the LogLevelListener of the RemoteLogLevelCallbackAdapter.
111      *
112      * @return The listener.
113      */

114     public LogLevelListener getListener()
115     {
116         return listener;
117     }
118
119     // }}}
120

121     //----------------------------------------------------------------------
122
// The LogLevelCallback interface
123
//----------------------------------------------------------------------
124
// {{{ getLogLevels
125

126     /**
127      * Get the current log levels from the callback. This method is needed to
128      * synchronize the LogLevelListener and the LogLevelCallback after the
129      * connection has been established.
130      *
131      * @return An object array that contains an alternating sequence of
132      * class/package names and log levels. The levels are Integers
133      * that may be null for classes for which a level has been
134      * requested but not explicitly set.
135      */

136     public Object JavaDoc[] getLogLevels()
137     {
138         try {
139             if (callback != null) {
140                 return callback.getLogLevels();
141             }
142         } catch (RemoteException JavaDoc ex) {
143             failed();
144         }
145         return null;
146     }
147
148     // }}}
149
// {{{ setLogLevel
150

151     /**
152      * Callback method for a LogLevelListener to change the log level for
153      * a class or package.
154      *
155      * @param source The listener that causes the change. It will not be
156      * notified to avoid recursion.
157      * @param name The name of the affected class or package.
158      * @param level The new log level.
159      */

160     public void setLogLevel(String JavaDoc name, int level)
161     {
162         try {
163             if (callback != null) {
164                 callback.setLogLevel(name, level);
165             }
166         } catch (RemoteException JavaDoc ex) {
167             failed();
168         }
169     }
170
171     // }}}
172
// {{{ removeLogLevel
173

174     /**
175      * Callback method for a LogLevelListener to remove the log level for
176      * a class or package.
177      *
178      * @param source The listener that causes the change. It will not be
179      * notified to avoid recursion.
180      * @param name The name of the affected class or package.
181      */

182     public void removeLogLevel(String JavaDoc name)
183     {
184         try {
185             if (callback != null) {
186                 callback.removeLogLevel(name);
187             }
188         } catch (RemoteException JavaDoc ex) {
189             failed();
190         }
191     }
192
193     // }}}
194
// {{{ getOutputLevel
195

196     /**
197      * Get the current output log level from the callback's application.
198      *
199      * @return The current output log level.
200      */

201     public int getOutputLevel()
202     {
203         try {
204             if (callback != null) {
205                 return callback.getOutputLevel();
206             }
207         } catch (RemoteException JavaDoc ex) {
208             failed();
209         }
210         return Log.DEFAULT_OUTPUT_LEVEL;
211     }
212
213     // }}}
214
// {{{ setOutputLevel
215

216     /**
217      * Set the output log level for the callback's application.
218      *
219      * @param level Output log level to set.
220      */

221     public void setOutputLevel(int level)
222     {
223         try {
224             if (callback != null) {
225                 callback.setOutputLevel(level);
226             }
227         } catch (RemoteException JavaDoc ex) {
228             failed();
229         }
230     }
231
232     // }}}
233
// {{{ getPreQueueLevel
234

235     /**
236      * Return the current pre-queue level from the callback's application.
237      *
238      * @return The current pre-queue level.
239      */

240     public int getPreQueueLevel()
241     {
242         try {
243             if (callback != null) {
244                 return callback.getPreQueueLevel();
245             }
246         } catch (RemoteException JavaDoc ex) {
247             failed();
248         }
249         return Log.DEFAULT_PRE_QUEUE_LEVEL;
250     }
251
252     // }}}
253
// {{{ setPreQueueLevel
254

255     /**
256      * Set the pre-queue level for the callback's application.
257      *
258      * @param level The pre-queue level to set.
259      */

260     public void setPreQueueLevel(int level)
261     {
262         try {
263             if (callback != null) {
264                 callback.setPreQueueLevel(level);
265             }
266         } catch (RemoteException JavaDoc ex) {
267             failed();
268         }
269     }
270
271     // }}}
272
// {{{ isQueueing
273

274     /**
275      * Check whether the callback's application is using a log queue.
276      *
277      * @return Whether the callback's application is queueing log entries.
278      *
279      * @see Log#isQueueing
280      */

281     public boolean isQueueing()
282     {
283         try {
284             if (callback != null) {
285                 return callback.isQueueing();
286             }
287         } catch (RemoteException JavaDoc ex) {
288             failed();
289         }
290         return true;
291     }
292
293     // }}}
294
// {{{ setQueueing
295

296     /**
297      * Set whether the callback's application should use a log queue.
298      *
299      * @param queue Whether to queue log entries.
300      *
301      * @see Log#setQueueing
302      */

303     public void setQueueing(boolean queue)
304     {
305         try {
306             if (callback != null) {
307                 callback.setQueueing(queue);
308             }
309         } catch (RemoteException JavaDoc ex) {
310             failed();
311         }
312     }
313
314     // }}}
315
// {{{ getQueueSize
316

317     /**
318      * Get the size of log queue of the callback's application.
319      *
320      * @return The size of the log queue.
321      *
322      * @see Log#getQueueSize
323      */

324     public int getQueueSize()
325     {
326         try {
327             if (callback != null) {
328                 return callback.getQueueSize();
329             }
330         } catch (RemoteException JavaDoc ex) {
331             failed();
332         }
333         return Log.DEFAULT_QUEUE_SIZE;
334     }
335
336     // }}}
337
// {{{ setQueueSize
338

339     /**
340      * Set the size of log queue of the callback's application.
341      *
342      * @param size The size of the log queue.
343      *
344      * @see Log#setQueueSize
345      */

346     public void setQueueSize(int size)
347     {
348         try {
349             if (callback != null) {
350                 callback.setQueueSize(size);
351             }
352         } catch (RemoteException JavaDoc ex) {
353             failed();
354         }
355     }
356
357     // }}}
358
// {{{ isDropOnOverflow
359

360     /**
361      * Check whether the callback's application is dropping entries when
362      * its log queue overflows.
363      *
364      * @return Whether entries are dropped on overflow.
365      *
366      * @see Log#isDropOnOverflow
367      */

368     public boolean isDropOnOverflow()
369     {
370         try {
371             if (callback != null) {
372                 return callback.isDropOnOverflow();
373             }
374         } catch (RemoteException JavaDoc ex) {
375             failed();
376         }
377         return false;
378     }
379
380     // }}}
381
// {{{ setDropOnOverflow
382

383     /**
384      * Set whether the callback's application should drop entries when
385      * its log queue overflows.
386      *
387      * @param drop Whether to drop entries on overflow.
388      *
389      * @see Log#setDropOnOverflow
390      */

391     public void setDropOnOverflow(boolean drop)
392     {
393         try {
394             if (callback != null) {
395                 callback.setDropOnOverflow(drop);
396             }
397         } catch (RemoteException JavaDoc ex) {
398             failed();
399         }
400     }
401
402     // }}}
403
// {{{ getFlushBufferSize
404

405     /**
406      * Get the size of the flush buffer of the callback's application.
407      *
408      * @return The flush buffer size or 0 to indicate no flush buffer.
409      */

410     public int getFlushBufferSize()
411     {
412         try {
413             if (callback != null) {
414                 return callback.getFlushBufferSize();
415             }
416         } catch (RemoteException JavaDoc ex) {
417             failed();
418         }
419         return Log.DEFAULT_FLUSH_BUFFER_SIZE;
420     }
421
422     // }}}
423
// {{{ setFlushBufferSize
424

425     /**
426      * Set the size of the flush buffer of the callback's application. Setting
427      * it to 0 will turn the flush buffer off.
428      *
429      * @param size The size of the flush buffer.
430      */

431     public void setFlushBufferSize(int size)
432     {
433         try {
434             if (callback != null) {
435                 callback.setFlushBufferSize(size);
436             }
437         } catch (RemoteException JavaDoc ex) {
438             failed();
439         }
440     }
441
442     // }}}
443
// {{{ getFlushTriggerLevel
444

445     /**
446      * Get the level that will trigger a flush of the messages saved in the
447      * flush buffer of the callback's application.
448      *
449      * @return The flush buffer's trigger level.
450      */

451     public int getFlushTriggerLevel()
452     {
453         try {
454             if (callback != null) {
455                 return callback.getFlushTriggerLevel();
456             }
457         } catch (RemoteException JavaDoc ex) {
458             failed();
459         }
460         return Log.DEFAULT_FLUSH_TRIGGER_LEVEL;
461     }
462
463     // }}}
464
// {{{ setFlushTriggerLevel
465

466     /**
467      * Set the level that will trigger a flush of the messages saved in the
468      * flush buffer of the callback's application.
469      *
470      * @param level The trigger level to set.
471      */

472     public void setFlushTriggerLevel(int level)
473     {
474         try {
475             if (callback != null) {
476                 callback.setFlushTriggerLevel(level);
477             }
478         } catch (RemoteException JavaDoc ex) {
479             failed();
480         }
481     }
482
483     // }}}
484
// {{{ getPostFlushSize
485

486     /**
487      * Get the number of messages to pass unfiltered through the pre-queue
488      * stage after a flush happened in the callback's application.
489      *
490      * @return The number of messages to pass.
491      */

492     public int getPostFlushSize()
493     {
494         try {
495             if (callback != null) {
496                 return callback.getPostFlushSize();
497             }
498         } catch (RemoteException JavaDoc ex) {
499             failed();
500         }
501         return Log.DEFAULT_POST_FLUSH_SIZE;
502     }
503
504     // }}}
505
// {{{ setPostFlushSize
506

507     /**
508      * Set the number of messages to pass unfiltered through the pre-queue
509      * stage after a flush happened in the callback's application.
510      *
511      * @param size The number of messages to pass.
512      */

513     public void setPostFlushSize(int size)
514     {
515         try {
516             if (callback != null) {
517                 callback.setPostFlushSize(size);
518             }
519         } catch (RemoteException JavaDoc ex) {
520             failed();
521         }
522     }
523
524     // }}}
525
// {{{ disconnect
526

527     /**
528      * End the connection with the RemoteLogLevelListener.
529      *
530      * @throws RemoteException If something RMI specific goes wrong.
531      */

532     public void disconnect()
533          throws RemoteException JavaDoc
534     {
535     }
536
537     // }}}
538

539     //----------------------------------------------------------------------
540
// The LogLevelListener interface
541
//----------------------------------------------------------------------
542
// {{{ classAdded
543

544     /**
545      * Notify the listener that a Logger for a formerly unknown class has
546      * requested its log level, adding the class to the log level tree.
547      *
548      * @param event The LogLevelEvent holding the details.
549      */

550     public void classAdded(LogLevelEvent event)
551     {
552         if (listener != null) {
553             listener.classAdded(event);
554         }
555     }
556
557     // }}}
558
// {{{ levelChanged
559

560     /**
561      * Notify the listener that a log level has been changed.
562      *
563      * @param event The LogLevelEvent holding the details.
564      */

565     public void levelChanged(LogLevelEvent event)
566     {
567         if (listener != null) {
568             listener.levelChanged(event);
569         }
570     }
571
572     // }}}
573
// {{{ levelRemoved
574

575     /**
576      * Notify the listener that a log level has been unset.
577      *
578      * @param event The LogLevelEvent holding the details.
579      */

580     public void levelRemoved(LogLevelEvent event)
581     {
582         if (listener != null) {
583             listener.levelRemoved(event);
584         }
585     }
586
587     // }}}
588
// {{{ setLogLevelCallback
589

590     /**
591      * Set the listener to forward LogLevelEvents to.
592      *
593      * @param callback The RemoteLogLevelCallback to forward callbacks to.
594      */

595     public void setLogLevelCallback(RemoteLogLevelCallback callback)
596     {
597         this.callback = callback;
598         if (listener != null && callback != null) {
599             listener.setLogLevelCallback(this);
600         }
601     }
602
603     // }}}
604

605     //----------------------------------------------------------------------
606
// Helper methods
607
//----------------------------------------------------------------------
608
// {{{ failed
609

610     /**
611      * Take care of RMI failures.
612      */

613     private void failed()
614     {
615         if (++failures >= MAX_FAILURES) {
616             // it doesn't make any sense to try to disconnect from the callback
617
callback = null;
618         }
619     }
620
621     // }}}
622
}
623
Popular Tags