1 23 package com.sun.enterprise.server.logging.logviewer.backend; 24 import java.io.*; 25 import java.util.ArrayList ; 26 import java.util.Date ; 27 import java.util.List ; 28 import java.util.StringTokenizer ; 29 import java.text.ParseException ; 30 import java.util.Properties ; 31 import java.util.Iterator ; 32 import java.text.SimpleDateFormat ; 33 34 35 47 public class LogFile implements java.io.Serializable { 48 49 private static SimpleDateFormat SIMPLE_DATE_FORMAT = 50 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 51 52 55 public LogFile(String name) { 56 _recordIdx.add(new Long (0)); 57 setLogFileName(name); 58 buildLogFileIndex(); 59 } 60 61 62 68 public List getLogEntries(long startingRecord) { 69 return getLogEntries(startingRecord, getIndexSize()); 70 } 71 72 73 74 81 public List getLogEntries(long startingRecord, long maxRecords) { 82 if (startingRecord < 0) { 83 return null; 84 } 85 86 BufferedReader reader = getFilePosition(startingRecord); 88 List results = new ArrayList (); 89 try { 90 while (results.size()<maxRecords) { 91 String line = reader.readLine(); 93 if (line == null) { 94 break; 95 } 96 if (!line.startsWith(RECORD_BEGIN_MARKER)) { 97 continue; 98 } 99 100 while (!line.endsWith(RECORD_END_MARKER)) { 102 line += "\n" + reader.readLine(); 103 } 104 105 try { 107 results.add(new LogEntry(line, 108 startingRecord+results.size())); 109 } catch (IllegalArgumentException ex) { 110 ex.printStackTrace(); 111 } 112 } 113 } catch (Exception ex) { 114 throw new RuntimeException (ex); 115 } finally { 116 if (reader != null) { 117 try { 118 reader.close(); 119 } catch (IOException ex) { 120 } 121 } 122 } 123 124 return results; 126 } 127 128 129 134 protected synchronized void buildLogFileIndex() { 135 int cnt, idx; 136 long recordCount = -1; 138 char recordBeginMarker[] = RECORD_BEGIN_MARKER.toCharArray(); 139 int recordBeginMarkerLen = recordBeginMarker.length; 140 141 long charPos = ((Long )_recordIdx.get(_recordIdx.size()-1)).longValue(); 143 BufferedReader reader = getLogFileReader(charPos); 144 long localIndexSize = getIndexSize(); 145 try { 146 while (true) { 147 try { 148 cnt = reader.read(); 149 if (cnt == -1) { 150 break; 151 } 152 charPos++; 153 154 for (idx=0; idx<recordBeginMarkerLen; idx++) { 156 if (cnt != recordBeginMarker[idx]) { 157 break; 158 } 159 cnt = reader.read(); 160 charPos++; 161 } 162 if (idx == recordBeginMarkerLen) { 163 recordCount++; 165 if (recordCount == localIndexSize) { 166 recordCount = 0; 170 _recordIdx.add( 171 new Long (charPos - (recordBeginMarkerLen+1))); 172 } 173 } 174 } catch (EOFException ex) { 175 break; 176 } catch (Exception ex) { 177 ex.printStackTrace(); 178 break; 179 } 180 } 181 } catch (Exception ex) { 182 ex.printStackTrace(); 183 } finally { 184 if (reader != null) { 185 try { 186 reader.close(); 187 } catch (IOException ex) { 188 } 189 } 190 } 191 } 192 193 194 201 protected BufferedReader getFilePosition(long recordNumber) { 202 int index = (int)(recordNumber / getIndexSize()); 207 if (_recordIdx.size() <= index) { 208 buildLogFileIndex(); 210 if (_recordIdx.size() <= index) { 211 throw new IllegalArgumentException ( 213 "Attempting to access Log entries that don't exist! "); 214 } 215 } 216 return getRecordPosition(index, 217 (int) (recordNumber % getIndexSize())); 218 } 219 220 221 224 private BufferedReader getRecordPosition(int index, int recordsToAdvance) { 225 long filePosition = ((Long )_recordIdx.get(index)).longValue(); 227 BufferedReader reader = getLogFileReader(filePosition) ; 228 229 char recordBeginMarker[] = RECORD_BEGIN_MARKER.toCharArray(); 230 int recordBeginMarkerLen = recordBeginMarker.length; 231 int ch; 232 int idx; 233 while (recordsToAdvance > 0) { 234 try { 235 ch = reader.read(); 237 filePosition++; 238 239 for (idx=0; idx<recordBeginMarkerLen; idx++) { 241 if (ch != recordBeginMarker[idx]) { 242 break; 243 } 244 ch = reader.read(); 245 filePosition++; 246 } 247 if (idx == recordBeginMarkerLen) { 248 recordsToAdvance--; 250 } 251 } catch (Exception ex) { 252 throw new RuntimeException (ex); 253 } 254 } 255 256 return reader; 258 } 259 260 261 262 266 protected BufferedReader getLogFileReader(long fromFilePosition) { 267 try { 268 FileInputStream file = new FileInputStream(getLogFileName()); 269 file.skip(fromFilePosition); 270 BufferedReader reader = 271 new BufferedReader(new InputStreamReader(file)); 272 return reader; 273 } catch (Exception ex) { 274 System.err.println("Exception in openFile..." + ex); 275 ex.printStackTrace(); 276 } 277 return null; 278 } 279 280 281 284 public String getLogFileName() { 285 return _logFileName; 286 } 287 288 289 292 public void setLogFileName(String filename) { 293 if (filename.equals(getLogFileName())) { 294 return; 295 } 296 _logFileName = filename; 297 _recordIdx = new ArrayList (); 298 _recordIdx.add(new Long (0)); 299 } 300 301 302 306 public long getLastIndexNumber() { 307 buildLogFileIndex(); 309 return _recordIdx.size()-1; 310 } 311 312 313 316 public long getIndexSize() { 317 return _indexSize; 318 } 319 320 321 325 public void setIndexSize(long indexSize) { 326 _indexSize = indexSize; 327 } 328 329 330 333 public class LogEntry implements java.io.Serializable { 334 public LogEntry(String line, long recordNumber) { 335 if (!line.startsWith(RECORD_BEGIN_MARKER)) { 336 throw new IllegalArgumentException ( 337 "Log Entries must start with: '"+RECORD_BEGIN_MARKER+ 338 "': '"+line+"'."); 339 } 340 341 StringTokenizer tokenizer = 342 new StringTokenizer (line, FIELD_SEPARATOR); 343 344 if (!(tokenizer.countTokens() > 5)) { 349 throw new IllegalArgumentException ( 350 "Log Entry does not contain all required fields: '"+ 351 line+"'."); 352 } 353 354 tokenizer.nextToken(); 356 try { 357 setLoggedDateTime( 358 SIMPLE_DATE_FORMAT.parse(tokenizer.nextToken())); 359 setLoggedLevel(tokenizer.nextToken()); 360 setLoggedProduct(tokenizer.nextToken()); 361 setLoggedLoggerName(tokenizer.nextToken()); 362 setLoggedNameValuePairs(tokenizer.nextToken()); 363 String messageIdandMessage = tokenizer.nextToken(); 364 if( messageIdandMessage != null ) { 365 int index = messageIdandMessage.indexOf( ":" ); 366 if( index != -1 ) { 367 setMessageId( messageIdandMessage.substring(0,index ) ); 368 setLoggedMessage( 369 messageIdandMessage.substring(index+1)); 370 } else { 371 setLoggedMessage(messageIdandMessage); 372 } 373 } 374 setRecordNumber(recordNumber); 375 } catch( Exception e ) { 376 RuntimeException t = 377 new RuntimeException ( "Error in building Log Entry " ); 378 t.initCause( e ); 379 throw t; 380 } 381 } 382 383 384 387 public Date getLoggedDateTime() { 388 return this.loggedDateTime; 389 } 390 391 392 395 public void setLoggedDateTime(Date loggedDateTime) { 396 this.loggedDateTime = loggedDateTime; 397 } 398 399 400 403 public String getLoggedLevel() { 404 return loggedLevel; 405 } 406 407 408 411 public void setLoggedLevel(String loggedLevel) { 412 this.loggedLevel = loggedLevel; 413 } 414 415 416 419 public String getLoggedProduct() { 420 return loggedProduct; 421 } 422 423 424 427 public void setLoggedProduct(String loggedProduct) { 428 this.loggedProduct = loggedProduct; 429 } 430 431 432 435 public String getLoggedLoggerName() { 436 return loggedLoggerName; 437 } 438 439 440 443 public void setLoggedLoggerName(String loggedLoggerName) { 444 this.loggedLoggerName = loggedLoggerName; 445 } 446 447 448 451 public String getLoggedNameValuePairs() { 452 return loggedNameValuePairs; 453 } 454 455 456 459 public void setLoggedNameValuePairs(String loggedNameValuePairs) { 460 this.loggedNameValuePairs = loggedNameValuePairs; 461 } 462 463 464 465 468 public void setLoggedMessage(String message) { 469 this.loggedMessage = message; 470 } 471 472 public void appendLoggedMessage(String message) { 473 loggedMessage += message; 474 } 475 476 477 480 public String getLoggedMessage() { 481 return loggedMessage; 482 } 483 484 public String getMessageId( ) { 485 return messageId; 486 } 487 488 public void setMessageId( String messageId ) { 489 this.messageId = messageId; 490 } 491 492 495 public long getRecordNumber() { 496 return recordNumber; 497 } 498 499 500 501 502 505 public void setRecordNumber(long recordNumber) { 506 this.recordNumber = recordNumber; 507 } 508 509 public String toString() { 510 return ""+getRecordNumber(); 511 } 512 513 514 private long recordNumber = -1; 515 private Date loggedDateTime = null; 516 private String loggedLevel = null; 517 private String loggedProduct = null; 518 private String loggedLoggerName = null; 519 private String loggedNameValuePairs = null; 520 private String loggedMessage = null; 521 private String messageId = ""; 522 } 523 524 525 526 public static final String RECORD_BEGIN_MARKER = "[#|"; 527 public static final String RECORD_END_MARKER = "|#]"; 528 public static final String FIELD_SEPARATOR = "|"; 529 530 531 private long _indexSize = 10; 532 private String _logFileName = null; 533 private List _recordIdx = new ArrayList (); 534 } 535 | Popular Tags |