FileDocCategorySizeDatePackage
LogFile.javaAPI DocGlassfish v2 API14277Fri May 04 22:35:46 BST 2007com.sun.enterprise.server.logging.logviewer.backend

LogFile

public class LogFile extends Object implements Serializable

This class encapsulates the log file so that its details are not exposed. "getLongEntries" returns an unfiltered List of LogEntry objects from the requested record number. It will always search forward. getIndexSize() returns the number of records between each index. getLastIndexNumber returns the last index.

AUTHOR:
Hemanth Puttaswamy and Ken Paulsen

This class also contains an inner class for storing LogEntry objects.

Fields Summary
private static SimpleDateFormat
SIMPLE_DATE_FORMAT
public static final String
RECORD_BEGIN_MARKER
public static final String
RECORD_END_MARKER
public static final String
FIELD_SEPARATOR
private long
_indexSize
private String
_logFileName
private List
_recordIdx
Constructors Summary
public LogFile(String name)
Constructor


    	     
       
	_recordIdx.add(new Long(0));
	setLogFileName(name);
	buildLogFileIndex();
    
Methods Summary
protected synchronized voidbuildLogFileIndex()
This method builds the file index in the beginning. The index is for the beginning of every record after the size specified by '_indexSize' variable.

	int cnt, idx;
        // NOTE: This should be -1 for indexing at the right intervals
	long recordCount = -1;
	char recordBeginMarker[] = RECORD_BEGIN_MARKER.toCharArray();
	int recordBeginMarkerLen = recordBeginMarker.length;

	// Open the file and skip to the where we left off
        long charPos = ((Long)_recordIdx.get(_recordIdx.size()-1)).longValue();
	BufferedReader reader = getLogFileReader(charPos);
        long localIndexSize = getIndexSize(); 
	try {
	    while (true) {
		try {
		    cnt = reader.read();
		    if (cnt == -1) {
			break;
		    }
		    charPos++;

		    // Compare to RECORD_BEGIN_MARKER
		    for (idx=0; idx<recordBeginMarkerLen; idx++) {
			if (cnt != recordBeginMarker[idx]) {
			    break;
			}
			cnt = reader.read();
			charPos++;
		    }
		    if (idx == recordBeginMarkerLen) {
			// Begining of a new record
			recordCount++;
			if (recordCount == localIndexSize) {
			    // Now we have traversed the records equal
			    // to index size. Time to add a new entry
			    // into the index.
			    recordCount = 0;
			    _recordIdx.add(
				new Long(charPos - (recordBeginMarkerLen+1)));
			}
		    }
		} catch (EOFException ex) {
		    break;
		} catch (Exception ex) {
		    ex.printStackTrace();
                    break;
		}
	    }
	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    if (reader != null) {
		try {
		    reader.close();
		} catch (IOException ex) {
		}
	    }
	}
    
protected java.io.BufferedReadergetFilePosition(long recordNumber)
This method returns the file position given the record number.

param
recordNumber The Record Number
return
The file position.

	// The index is stored from the second slot. i.e., if there
	// are 100 records and the index will be on 20, 40, 60, 80, 100
	// if the _indexSize is 20. We don't store '0' hence we subtract
	// from 1 to get the right index
	int index = (int)(recordNumber / getIndexSize());
	if (_recordIdx.size() <= index) {
	    // We have not indexed enough
	    buildLogFileIndex();
	    if (_recordIdx.size() <= index) {
		// Hmm... something's not right
		throw new IllegalArgumentException(
		    "Attempting to access Log entries that don't exist! ");
	    }
	}
        return getRecordPosition(index,
            (int) (recordNumber % getIndexSize()));
    
public longgetIndexSize()

	return _indexSize;
    
public longgetLastIndexNumber()
The log records are indexed, this method returns the last index. It will ensure that the indexes are up-to-date.

    	// Ensure the file is fully indexed for this call
	buildLogFileIndex();
	return _recordIdx.size()-1;
    
public java.util.ListgetLogEntries(long startingRecord)
This method returns up to _indexSize records starting with the given record number.

param
startingRecord The starting point to search for LogEntries

	return getLogEntries(startingRecord, getIndexSize());
    
public java.util.ListgetLogEntries(long startingRecord, long maxRecords)
This method returns up to _indexSize records starting with the given record number. It will return up to "maxRecords" records.

param
startingRecord The starting point to search for LogEntries
param
masRecords The maximum number of records to return

	if (startingRecord < 0) {
	    return null;
	}

    	// Open the file at the desired starting Record
	BufferedReader reader = getFilePosition(startingRecord);
	List results = new ArrayList();
	try {
	    while (results.size()<maxRecords) {
	    	// Get a line from the log file
		String line = reader.readLine();
		if (line == null) {
		    break;
		}
		if (!line.startsWith(RECORD_BEGIN_MARKER)) {
		    continue;
		}

		// Read the whole record
		while (!line.endsWith(RECORD_END_MARKER)) {
		    line += "\n" + reader.readLine();
		}

		// Read the LogEntry
		try {
		    results.add(new LogEntry(line, 
                        startingRecord+results.size()));
		} catch (IllegalArgumentException ex) {
		    ex.printStackTrace();
		}
	    }
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	} finally {
	    if (reader != null) {
		try {
		    reader.close();
		} catch (IOException ex) {
		}
	    }
	}

	// Return the results
	return results;
    
public java.lang.StringgetLogFileName()

	return _logFileName;
    
protected java.io.BufferedReadergetLogFileReader(long fromFilePosition)
This method opens the server.log file and moves the stream to the specified filePosition.

	try {
	    FileInputStream file = new FileInputStream(getLogFileName());
	    file.skip(fromFilePosition);
	    BufferedReader reader =
		new BufferedReader(new InputStreamReader(file));
	    return reader;
	} catch (Exception ex) {
	    System.err.println("Exception in openFile..." + ex);
	    ex.printStackTrace();
	}
	return null;
    
private java.io.BufferedReadergetRecordPosition(int index, int recordsToAdvance)
Gets the precise record position from the specified FilePosition.

	// Get the indexed file position
        long filePosition = ((Long)_recordIdx.get(index)).longValue(); 
        BufferedReader reader = getLogFileReader(filePosition) ;

	char recordBeginMarker[] = RECORD_BEGIN_MARKER.toCharArray();
	int recordBeginMarkerLen = recordBeginMarker.length;
        int ch;
        int idx;
        while (recordsToAdvance > 0) {
	    try {
		// If we read past the end, throw exception
		ch = reader.read();
		filePosition++;

		// Compare to RECORD_BEGIN_MARKER
		for (idx=0; idx<recordBeginMarkerLen; idx++) {
		    if (ch != recordBeginMarker[idx]) {
			break;
		    }
		    ch = reader.read();
		    filePosition++;
		}
		if (idx == recordBeginMarkerLen) {
		    // Begining of a new record
		    recordsToAdvance--;
		}
	    } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }

	// Return the reader
        return reader;
    
public voidsetIndexSize(long indexSize)
The number of records between indexes. This is also used as the max number of records returned from getLogEntries(long).

	_indexSize = indexSize;
    
public voidsetLogFileName(java.lang.String filename)

	if (filename.equals(getLogFileName())) {
	    return;
	}
	_logFileName = filename;
	_recordIdx = new ArrayList();
	_recordIdx.add(new Long(0));