KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > StaticLogger


1 /*
2  * Copyright 1999, 2000 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import java.util.*;
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24
25
26 /**
27  * Logger that uses a static message buffer to facilitate intra-web-app
28  * recording and retrieval of log messages.
29  *
30  * @author Craig R. McClanahan
31  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
32  */

33
34 public class StaticLogger {
35
36
37     // ----------------------------------------------------------- Constructors
38

39
40     // ------------------------------------------------------- Static Variables
41

42
43     /**
44      * The set of messages that have been logged.
45      */

46     protected static ArrayList messages = new ArrayList();
47
48
49     /**
50      * The index of the next message that will be retrieved by a read() call.
51      */

52     protected static int position = 0;
53
54
55     // --------------------------------------------------------- Public Methods
56

57
58     /**
59      * Return the next message that has been logged, or <code>null</code>
60      * if there are no more messages.
61      */

62     public static String JavaDoc read() {
63
64         synchronized (messages) {
65             if (position < messages.size())
66                 return ((String JavaDoc) messages.get(position++));
67             else
68                 return (null);
69         }
70
71     }
72
73
74     /**
75      * Reset the messages buffer and position.
76      */

77     public static void reset() {
78
79         synchronized (messages) {
80             messages.clear();
81             position = 0;
82         }
83
84     }
85
86
87     /**
88      * Write a new message to the end of the messages buffer.
89      *
90      * @param message The message to be added
91      */

92     public static void write(String JavaDoc message) {
93
94         synchronized (messages) {
95             messages.add(message);
96         }
97
98     }
99
100
101 }
102
Popular Tags