FileDocCategorySizeDatePackage
LogNameFilter.javaAPI DocGlassfish v2 API6367Fri May 04 22:34:50 BST 2007com.sun.enterprise.diagnostics.util

LogNameFilter

public class LogNameFilter extends Object implements FilenameFilter
Log file name is assumed to be in server.log_YYYY-MM-DDTHH-MM-SS format eg.server.log_2005-03-04T12-21-53 format. Files are sorted in ascending order.
author
Manisha Umbarje, Jagadish Ramu

Fields Summary
private Date
startDate
private Date
endDate
private String
fileNamePrefix
private static final SimpleDateFormat
dateFormat
Constructors Summary
public LogNameFilter(String fileName, Date startDate, Date endDate)


            
	    this.startDate = startDate;
	    this.endDate = endDate;
	    fileNamePrefix = fileName;
    
Methods Summary
public booleanaccept(java.io.File aDir, java.lang.String fileName)

        if (aDir == null || fileName == null)
            return false;
        if (fileName.indexOf(fileNamePrefix) < 0)
            return false;
        int datePatternIndex = fileName.indexOf(Constants.FILENAME_DATE_SEPARATOR);

        if (datePatternIndex > 0) {
            try {
                // fileDate indicates date of last entry in the file
                Date fileDate = dateFormat.parse
                        (fileName.substring(datePatternIndex + 1, datePatternIndex + 11));

                if (isValidEntry(fileDate, startDate, endDate)) {
                    return true;
                }
                //handle the case where date_of_last entry is beyond endDate and
                // it may contain entries within start & end date.
                Date firstLogEntry = getDateOfFirstLogEntry(aDir, fileName);
                return  isValidEntry(firstLogEntry, startDate, endDate) ;
            } catch (Exception e) {
                return false;
            }
        } else {
            try {
                // server.log, check if date of first entry is before
                // the endDate, assuption here is every new log file start
                // with new logEntry and not partial entry
                Date date = getDateOfFirstLogEntry(aDir, fileName);

                return isValidEntry(date, startDate, endDate);
            } catch (DiagnosticException de) {
                return false;
            }
        }
    
private java.util.DategetDateOfFirstLogEntry(java.io.File aDir, java.lang.String fileName)

        if (aDir == null || fileName == null)
            return null;
            BufferedReader reader = null;
        try {
            File file = new File(aDir, fileName);
            reader = new BufferedReader(new FileReader(file));
            String firstEntry = null;
            while ((firstEntry = reader.readLine()) != null) {
                try {
                    return dateFormat.parse(firstEntry.substring
                            (Constants.ENTRY_DATE_BEGIN_INDEX, Constants.ENTRY_DATE_BEGIN_INDEX+Constants.ENTRY_DATE_LENGTH));
                } catch (ParseException pe) {
                    //ignore, it may be startup / shutdown log
                } catch(IndexOutOfBoundsException ie){
                    //ignore.
                }
                //continue with next log entry
                continue;
            }
        } catch (FileNotFoundException fnf) {
            throw new DiagnosticException(fnf.getMessage());
        } catch (IOException io) {
            throw new DiagnosticException(io.getMessage());
        }finally{
            try{
                if(reader!=null){
                    reader.close();
                }
            }catch(IOException ie){
                //ignore
            }
        }
        return null;
    
private booleanisValidEntry(java.util.Date firstLogEntry, java.util.Date startDate, java.util.Date endDate)

        if(firstLogEntry == null){
            return false;
        }
        //assuming that startDate is not null.
        if( endDate != null )
            return firstLogEntry.compareTo(startDate) >=0 && firstLogEntry.compareTo(endDate) <=0 ;
        else
            return firstLogEntry.compareTo(startDate) >=0 ;