KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.text.ParseException JavaDoc;
29
30 import com.sun.enterprise.diagnostics.Constants;
31
32 /**
33  * Sorts server.log files in the ascending order by date.
34  * @author Manisha Umbarje
35  */

36 public class LogNameComparator implements Comparator JavaDoc {
37
38     private static final SimpleDateFormat JavaDoc dateFormat =
39     new SimpleDateFormat JavaDoc(Constants.DATE_PATTERN);
40     
41     public LogNameComparator() {
42     }
43
44     public int compare(Object JavaDoc obj1 , Object JavaDoc obj2) {
45     String JavaDoc name1 = ((File JavaDoc)obj1).getName();
46     String JavaDoc name2 = ((File JavaDoc)obj2).getName();
47
48     if (name1 == null || name2 == null)
49         return 0;
50
51     //Log Files are in the format server.log_yyyy-mm-ddThh-mm-ss
52
int name1DateBeginIndex = name1.indexOf
53                 (Constants.FILENAME_DATE_SEPARATOR) + 1;
54     int name2DateBeginIndex = name2.indexOf
55                 (Constants.FILENAME_DATE_SEPARATOR) + 1;
56
57     // obj1 represents server.log i.e latest file
58
if (name1DateBeginIndex <= 0) {
59         return 1;
60     }
61
62     // obj2 represents server.log i.e latest file
63
if(name2DateBeginIndex <= 0) {
64         return -1;
65     }
66
67     try {
68         Date JavaDoc name1Date = dateFormat.parse
69         (name1.substring
70         (name1DateBeginIndex,name1DateBeginIndex +
71         Constants.ENTRY_DATE_LENGTH));
72         
73         Date JavaDoc name2Date = dateFormat.parse
74         (name2.substring
75         (name2DateBeginIndex,name1DateBeginIndex +
76         Constants.ENTRY_DATE_LENGTH));
77
78         if (name1Date.after(name2Date))
79         return 1;
80         else
81         return -1;
82     } catch (ParseException JavaDoc exc) {
83         return 0;
84     }
85     }
86 }
87
Popular Tags