comparison src/com/go/trove/log/LogEventParsingOutputStream.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 /* ====================================================================
2 * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3 * ====================================================================
4 * The Tea Software License, Version 1.1
5 *
6 * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by the
23 * Walt Disney Internet Group (http://opensource.go.com/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
26 *
27 * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28 * not be used to endorse or promote products derived from this
29 * software without prior written permission. For written
30 * permission, please contact opensource@dig.com.
31 *
32 * 5. Products derived from this software may not be called "Tea",
33 * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34 * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35 * written permission of the Walt Disney Internet Group.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * For more information about Tea, please see http://opensource.go.com/.
51 */
52
53 package com.go.trove.log;
54
55 import java.io.*;
56 import java.util.*;
57
58 /******************************************************************************
59 * LogEventParsingOutputStream parses the data written to it and converts it
60 * to LogEvent objects. Add a LogListener to intercept LogEvents. Events are
61 * parsed based on newline characters (LF, CRLF or CR) or a switch to a
62 * different thread.
63 *
64 * @author Brian S O'Neill
65 * @version
66 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 9/07/00 <!-- $-->
67 */
68 public class LogEventParsingOutputStream extends OutputStream {
69 private Vector mListeners;
70 private Log mSource;
71 private int mType;
72
73 private byte[] mOneByte = new byte[1];
74
75 private ByteArrayOutputStream mMessageBuffer;
76 private String mEncoding;
77 private Thread mMessageThread;
78
79 private Date mTimestamp;
80
81 // Set to true upon reading a CR so that if the next character is a LF,
82 // it is consumed. This is how CRLF patterns are discovered.
83 private boolean mTrackLF;
84
85 /**
86 * @param source Source object to create events with.
87 * @param type Type of events to create.
88 */
89 public LogEventParsingOutputStream(Log source, int type) {
90 mListeners = new Vector(2);
91 mSource = source;
92 mType = type;
93
94 mMessageBuffer = new ByteArrayOutputStream();
95 }
96
97 /**
98 * @param source Source object to create events with.
99 * @param type Type of events to create.
100 * @param encoding Character encoding of bytes.
101 */
102 public LogEventParsingOutputStream(Log source, int type,
103 String encoding) {
104 mListeners = new Vector(2);
105 mSource = source;
106 mType = type;
107
108 mMessageBuffer = new ByteArrayOutputStream();
109 mEncoding = encoding;
110 }
111
112 public void addLogListener(LogListener listener) {
113 mListeners.addElement(listener);
114 }
115
116 public void removeLogListener(LogListener listener) {
117 mListeners.removeElement(listener);
118 }
119
120 private synchronized void flushLogEvent()
121 throws UnsupportedEncodingException {
122
123 if (mMessageThread == null) {
124 return;
125 }
126
127 String message;
128 if (mEncoding == null) {
129 message = mMessageBuffer.toString();
130 }
131 else {
132 message = mMessageBuffer.toString(mEncoding);
133 }
134
135 if (mMessageBuffer.size() > 10000) {
136 mMessageBuffer = new ByteArrayOutputStream();
137 }
138 else {
139 mMessageBuffer.reset();
140 }
141
142 LogEvent e;
143
144 if (mTimestamp == null) {
145 e = new LogEvent(mSource, mType, message, mMessageThread);
146 }
147 else {
148 e = new LogEvent(mSource, mType, message, mMessageThread,
149 mTimestamp);
150 mTimestamp = null;
151 }
152
153 synchronized (mListeners) {
154 Enumeration group = mListeners.elements();
155 while (group.hasMoreElements()) {
156 ((LogListener)group.nextElement()).logMessage(e);
157 }
158 }
159 }
160
161 public synchronized void write(int b) throws IOException {
162 mOneByte[0] = (byte)b;
163 write(mOneByte, 0, 1);
164 }
165
166 public synchronized void write(byte[] array, int off, int len)
167 throws IOException {
168
169 if (!isEnabled()) {
170 if (mMessageBuffer.size() > 0) {
171 flushLogEvent();
172 }
173 return;
174 }
175
176 Thread current = Thread.currentThread();
177 if (current != mMessageThread) {
178 if (mMessageBuffer.size() > 0) {
179 flushLogEvent();
180 }
181 mMessageThread = current;
182 }
183
184 int writtenLength = 0;
185
186 int i = 0;
187 for (i=0; i<len; i++) {
188 byte b = array[i + off];
189 if (b == (byte)'\r') {
190 mTrackLF = true;
191 writeToBuffer(array, writtenLength + off, i - writtenLength);
192 // Add one more than i to skip the CR.
193 writtenLength = i + 1;
194 flushLogEvent();
195 }
196 else if (b == (byte)'\n') {
197 if (mTrackLF) {
198 // Consume the LF of CRLF.
199 mTrackLF = false;
200 writtenLength++;
201 }
202 else {
203 writeToBuffer(array, writtenLength + off,
204 i - writtenLength);
205 // Add one more than i to skip the LF.
206 writtenLength = i + 1;
207 flushLogEvent();
208 }
209 }
210 else {
211 mTrackLF = false;
212 }
213 }
214
215 writeToBuffer(array, writtenLength + off, i - writtenLength);
216 }
217
218 public synchronized void close() throws IOException {
219 mMessageBuffer.close();
220 }
221
222 /**
223 * Returning false discards written data, and events are not generated.
224 * Default implementation always returns true.
225 */
226 public boolean isEnabled() {
227 return true;
228 }
229
230 private void writeToBuffer(byte[] array, int off, int len) {
231 if (mMessageBuffer.size() == 0) {
232 mTimestamp = new Date();
233 }
234 mMessageBuffer.write(array, off, len);
235 }
236 }