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

LogNameComparator

public class LogNameComparator extends Object implements Comparator
Sorts server.log files in the ascending order by date.
author
Manisha Umbarje

Fields Summary
private static final SimpleDateFormat
dateFormat
Constructors Summary
public LogNameComparator()

    
      
    
Methods Summary
public intcompare(java.lang.Object obj1, java.lang.Object obj2)

	String name1 = ((File)obj1).getName();
	String name2 = ((File)obj2).getName();

	if (name1 == null || name2 == null)
	    return 0;

	//Log Files are in the format server.log_yyyy-mm-ddThh-mm-ss
	int name1DateBeginIndex = name1.indexOf
				(Constants.FILENAME_DATE_SEPARATOR)  + 1;
	int name2DateBeginIndex = name2.indexOf
				(Constants.FILENAME_DATE_SEPARATOR) + 1;

	// obj1 represents server.log i.e latest file
	if (name1DateBeginIndex <=  0) {
	    return 1;
	}

	// obj2 represents server.log i.e latest file
	if(name2DateBeginIndex <= 0) {
	    return -1;
	}

	try {
	    Date name1Date = dateFormat.parse
		(name1.substring
		(name1DateBeginIndex,name1DateBeginIndex + 
		Constants.ENTRY_DATE_LENGTH));
	    
	    Date name2Date = dateFormat.parse
		(name2.substring
		(name2DateBeginIndex,name1DateBeginIndex + 
		Constants.ENTRY_DATE_LENGTH));

	    if (name1Date.after(name2Date))
		return 1;
	    else
		return -1;
	} catch (ParseException exc) {
	    return 0;
	}