'FilterLog.java'에 해당되는 글 1건

  1. 2010.09.27 FilterLog.java 1
01.JAVA/Java2010. 9. 27. 15:48
반응형
/*
* CDDL HEADER START
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* http://www.sun.com/cddl/cddl.html and legal/CDDLv1.0.txt
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Copyright 2007 Sun Microsystems Inc. All Rights Reserved
* CDDL HEADER END
*/


package com.sun.portal.search.robot;

import java.lang.*;
import java.io.*;
import java.util.*;
import java.text.*;



public class FilterLog
{
    public static DateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.US);
    public static DateFormat listdf = new SimpleDateFormat("dd/MMM -  HH:mm:ss");
    protected ArrayList runList = new ArrayList();
    protected PrintWriter debug = null;
    private String dateStr = null;
    private String reasonStr = null;
    private int reasonIndex = -1;
    private Hashtable ExReasons = new Hashtable();
    private ArrayList ExReasonsArray = new ArrayList();
    private ArrayList FilteredList = new ArrayList();
    private int total = 0;

    static public void main(String[] args) {
FilterLog filterLog = null;
        if (args.length == 1) {
            filterLog= new FilterLog(args[0]);
        }
        else if (args.length == 2) {
            filterLog = new FilterLog(args[0], args[1]);
        }
        else if (args.length == 3) {
            filterLog = new FilterLog(args[0], args[1], args[2]);
        }
        else {
   System.out.println("Usage:FilterLog filterLogPath [selectedDate [selectedReason]]");
   return;
        }

        filterLog.printResult(System.out);
    }

    public FilterLog(String file, PrintWriter out)
    {
        debug=out;
        out.println("FilterLog debug enabled");
        parserFilterLog(file);
    }
    public FilterLog(String file, String date, PrintWriter out)
    {
        debug=out;
        dateStr = date;
        out.println("FilterLog debug enabled");
        parserFilterLog(file);
    }

    public FilterLog(String file, String date, String reason, PrintWriter out)
    {
        debug=out;
        dateStr = date;
        reasonStr = reason;
        out.println("FilterLog debug enabled");
        parserFilterLog(file);
    }

    public FilterLog(String file)
    {
        parserFilterLog(file);
    }
    public FilterLog(String file, String date)
    {
        dateStr = date;
        parserFilterLog(file);
    }
    public FilterLog(String file, String date, String reason)
    {
        dateStr = date;
        reasonStr = reason;
        parserFilterLog(file);
    }
    public FilterLog(String file, String date, int reason)
    {
        dateStr = date;
        reasonIndex = reason;
        parserFilterLog(file);
    }


    public void parserFilterLog(String filterlog_path)
    {
BufferedReader in = null;
String oldline = null;
String line = null;
        String preLine = null;
        Date date = null;
        boolean inSelectedDate = false;
        boolean inSelectedReason = false;
        int lineNum = 0;    // line number starts with 1
try {
   in = new BufferedReader(new InputStreamReader(new FileInputStream(filterlog_path), "ISO-8859-1"));
   while ((oldline=in.readLine()) != null) {
                lineNum ++;
try {
   line = new String(oldline.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
   continue;
}
                // expecting
                //[22/Jan/2002:10:48:19]  9883@001: Filter log started
                if ( line.startsWith("[") /*|| line.startsWith("Time:")*/) {
                    int start = line.indexOf('[');
                    int end = line.indexOf(']');
                    if (start >=0 && end > start) {
                        String datestr = line.substring(start+1, end);
                        try {
                            date = df.parse(datestr);
                            if (debug != null) {
                                debug.println("add date : " + df.format(date));
                            }
                            runList.add(date);
                            if (dateStr != null && datestr.compareTo(dateStr)==0) {
                                inSelectedDate = true;
                            }
                            else {
                                inSelectedDate = false;
                            }
                        } catch (ParseException e) {
                            if (debug != null) {
                                debug.println("ParseException at position(" + e.getErrorOffset() + ")");
                                debug.println(line);
                            } else {
                                System.out.println("ParseException at position(" + e.getErrorOffset() + ")");
                                System.out.println(line);
                            }
                        }
                    }
                }
                else  if (inSelectedDate && line.startsWith("Hint:")) {
                    String hint = line.substring(5).trim();
                    if (hint.startsWith("File Not Found.")) {
                        hint = "File Not Found.";
                    }
                    if (preLine != null) {
                        int c = 1;
                        if (ExReasons.containsKey(hint)) {
                            Integer count =(Integer)ExReasons.get(hint);
                            c = count.intValue();
                            c++;
   ExReasons.put(hint, new Integer(c));
                        }
else {
   ExReasons.put(hint, new Integer(c));
   ExReasonsArray.add(hint);
}
                        total++;
                        if ((reasonStr != null && reasonStr.compareToIgnoreCase(hint) == 0) || ((reasonIndex >= 0 && ExReasonsArray.size() > reasonIndex && ((String)ExReasonsArray.get(reasonIndex)).compareToIgnoreCase(hint) == 0))) {
                            FilteredList.add(preLine);
                        }
                        preLine = null;
                    }
                }
                else if (inSelectedDate) {
                    preLine = line;
                }
   }
}
catch(IOException e) {
            if (debug != null) {
                debug.println("IOException:" + e.getMessage()  + " at line# " + lineNum);
            }
            else {
                System.out.println("IOException:" + e.getMessage() + " at line# " + lineNum);
            }
}
    }
/*    public void printRunList(PrintWriter out)
    {
        for (int i=0; i < runList.size(); i++) {
            out.println(i + " run - " + listdf.format((Date)runList.get(i)) );
        }
    }
*/

    public void printResult(PrintStream out)
    {
        for (int i=0; i < runList.size(); i++) {
            out.println(i + " run - " + listdf.format((Date)runList.get(i)) );
        }
        if (dateStr != null) {
            Enumeration e = ExReasons.keys();
            while(e.hasMoreElements()) {
                String reason = (String)e.nextElement();
                Integer count = (Integer) ExReasons.get(reason);
                out.println(reason + ":" + count);
            }
            if (reasonStr != null) {
                for (int i=0; i < FilteredList.size(); i++) {
                    out.println((String)FilteredList.get(i));
                }
            }
        }

    }

    public Object[] getRunArray()
    {
        return runList.toArray();
    }

    public ArrayList getRunArrayList()
    {
        return runList;
    }

    public void setDebug(PrintWriter out)
    {
        debug=out;
    }

    public String[] getReasons() {
return (String[]) ExReasonsArray.toArray(new String[0]);
    }

    public int getReasonSubTotal(String reason) {
        if (ExReasons != null) {
            Integer count = (Integer)ExReasons.get(reason);
            return count.intValue();
        }
        return 0;
    }

    public  ArrayList getFilteredURL() {
        return FilteredList;
    }

    public String getReason() {
        return reasonStr;
    }
    public String getReasonByIndex(int index) {
try {
   return (String) ExReasonsArray.get(index);
} catch (Exception e) {
   return null;
}
    }

    public int getTotal() {
        return total;
    }
}
Posted by 1010