KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > util > LogNameFilter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.util;
24
25 import java.io.File JavaDoc;
26
27 import java.io.FilenameFilter JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.BufferedReader JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.text.SimpleDateFormat JavaDoc;
35 import java.text.ParseException JavaDoc;
36
37 import com.sun.enterprise.diagnostics.Constants;
38 import com.sun.enterprise.diagnostics.DiagnosticException;
39 /**
40  * Log file name is assumed to be in server.log_YYYY-MM-DDTHH-MM-SS format
41  * eg.server.log_2005-03-04T12-21-53 format. Files are sorted in ascending order.
42  * @author Manisha Umbarje
43  */

44 public class LogNameFilter implements FilenameFilter JavaDoc {
45
46     private Date JavaDoc startDate;
47     private Date JavaDoc endDate;
48     private String JavaDoc fileNamePrefix;
49     
50     private static final SimpleDateFormat JavaDoc dateFormat =
51     new SimpleDateFormat JavaDoc(Constants.DATE_PATTERN);
52
53     public LogNameFilter (String JavaDoc fileName, Date JavaDoc startDate, Date JavaDoc endDate) {
54         this.startDate = startDate;
55         this.endDate = endDate;
56         fileNamePrefix = fileName;
57     }
58
59     public boolean accept (File JavaDoc aDir, String JavaDoc 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         // fileDate indicates date of last entry in the file
69
Date JavaDoc 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 JavaDoc e) {
80         return false;
81         }
82     } else {
83         try
84         {
85         // server.log, check if date of first entry is before
86
// the endDate, assuption here is every new log file start
87
// with new logEntry and not partial entry
88
return endDate.after(getDateOfFirstLogEntry(aDir,fileName));
89         } catch (DiagnosticException de) {
90             return false;
91         }
92     }
93     }
94
95     private Date JavaDoc getDateOfFirstLogEntry(File JavaDoc aDir, String JavaDoc fileName)
96                 throws DiagnosticException {
97     if (aDir == null || fileName == null)
98         return null;
99
100     try {
101         File JavaDoc file = new File JavaDoc(aDir, fileName);
102         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
103         String JavaDoc firstEntry = reader.readLine();
104
105         return dateFormat.parse(firstEntry.substring
106         (Constants.ENTRY_DATE_BEGIN_INDEX,Constants.ENTRY_DATE_LENGTH));
107     } catch (FileNotFoundException JavaDoc fnf) {
108         throw new DiagnosticException(fnf.getMessage());
109     } catch (IOException JavaDoc io) {
110         throw new DiagnosticException(io.getMessage());
111     } catch (ParseException JavaDoc pe) {
112         throw new DiagnosticException(pe.getMessage());
113     }
114     }
115 }
116
Popular Tags