1 23 package com.sun.enterprise.diagnostics.util; 24 25 import java.io.File ; 26 27 import java.io.FilenameFilter ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.BufferedReader ; 31 import java.io.FileNotFoundException ; 32 import java.io.IOException ; 33 import java.util.Date ; 34 import java.text.SimpleDateFormat ; 35 import java.text.ParseException ; 36 37 import com.sun.enterprise.diagnostics.Constants; 38 import com.sun.enterprise.diagnostics.DiagnosticException; 39 44 public class LogNameFilter implements FilenameFilter { 45 46 private Date startDate; 47 private Date endDate; 48 private String fileNamePrefix; 49 50 private static final SimpleDateFormat dateFormat = 51 new SimpleDateFormat (Constants.DATE_PATTERN); 52 53 public LogNameFilter (String fileName, Date startDate, Date endDate) { 54 this.startDate = startDate; 55 this.endDate = endDate; 56 fileNamePrefix = fileName; 57 } 58 59 public boolean accept (File aDir, String fileName) { 60 if (aDir == null || fileName == null) 61 return false; 62 if (fileName.indexOf(fileNamePrefix)<0) 63 return false; 64 int datePatternIndex = fileName.indexOf(Constants.FILENAME_DATE_SEPARATOR); 65 66 if (datePatternIndex > 0) { 67 try { 68 Date fileDate = dateFormat.parse 70 (fileName.substring(datePatternIndex+1,datePatternIndex+11)); 71 72 if (fileDate.compareTo(startDate) >=0 && 73 fileDate.compareTo(endDate) <=0) 74 { 75 return true; 76 } 77 else 78 return false; 79 } catch(Exception e) { 80 return false; 81 } 82 } else { 83 try 84 { 85 return endDate.after(getDateOfFirstLogEntry(aDir,fileName)); 89 } catch (DiagnosticException de) { 90 return false; 91 } 92 } 93 } 94 95 private Date getDateOfFirstLogEntry(File aDir, String fileName) 96 throws DiagnosticException { 97 if (aDir == null || fileName == null) 98 return null; 99 100 try { 101 File file = new File (aDir, fileName); 102 BufferedReader reader = new BufferedReader (new FileReader (file)); 103 String firstEntry = reader.readLine(); 104 105 return dateFormat.parse(firstEntry.substring 106 (Constants.ENTRY_DATE_BEGIN_INDEX,Constants.ENTRY_DATE_LENGTH)); 107 } catch (FileNotFoundException fnf) { 108 throw new DiagnosticException(fnf.getMessage()); 109 } catch (IOException io) { 110 throw new DiagnosticException(io.getMessage()); 111 } catch (ParseException pe) { 112 throw new DiagnosticException(pe.getMessage()); 113 } 114 } 115 } 116 | Popular Tags |