001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.robot;
007:
008: import java.lang.*;
009: import java.io.*;
010: import java.util.*;
011: import java.text.*;
012:
013: public class FilterLog {
014: public static DateFormat df = new SimpleDateFormat(
015: "dd/MMM/yyyy:HH:mm:ss", Locale.US);
016: public static DateFormat listdf = new SimpleDateFormat(
017: "dd/MMM - HH:mm:ss");
018: protected ArrayList runList = new ArrayList();
019: protected PrintWriter debug = null;
020: private String dateStr = null;
021: private String reasonStr = null;
022: private int reasonIndex = -1;
023: private Hashtable ExReasons = new Hashtable();
024: private ArrayList ExReasonsArray = new ArrayList();
025: private ArrayList FilteredList = new ArrayList();
026: private int total = 0;
027:
028: static public void main(String[] args) {
029: FilterLog filterLog = null;
030: if (args.length == 1) {
031: filterLog = new FilterLog(args[0]);
032: } else if (args.length == 2) {
033: filterLog = new FilterLog(args[0], args[1]);
034: } else if (args.length == 3) {
035: filterLog = new FilterLog(args[0], args[1], args[2]);
036: } else {
037: System.out
038: .println("Usage:FilterLog filterLogPath [selectedDate [selectedReason]]");
039: return;
040: }
041:
042: filterLog.printResult(System.out);
043: }
044:
045: public FilterLog(String file, PrintWriter out) {
046: debug = out;
047: out.println("FilterLog debug enabled");
048: parserFilterLog(file);
049: }
050:
051: public FilterLog(String file, String date, PrintWriter out) {
052: debug = out;
053: dateStr = date;
054: out.println("FilterLog debug enabled");
055: parserFilterLog(file);
056: }
057:
058: public FilterLog(String file, String date, String reason,
059: PrintWriter out) {
060: debug = out;
061: dateStr = date;
062: reasonStr = reason;
063: out.println("FilterLog debug enabled");
064: parserFilterLog(file);
065: }
066:
067: public FilterLog(String file) {
068: parserFilterLog(file);
069: }
070:
071: public FilterLog(String file, String date) {
072: dateStr = date;
073: parserFilterLog(file);
074: }
075:
076: public FilterLog(String file, String date, String reason) {
077: dateStr = date;
078: reasonStr = reason;
079: parserFilterLog(file);
080: }
081:
082: public FilterLog(String file, String date, int reason) {
083: dateStr = date;
084: reasonIndex = reason;
085: parserFilterLog(file);
086: }
087:
088: public void parserFilterLog(String filterlog_path) {
089: BufferedReader in = null;
090: String oldline = null;
091: String line = null;
092: String preLine = null;
093: Date date = null;
094: boolean inSelectedDate = false;
095: boolean inSelectedReason = false;
096: int lineNum = 0; // line number starts with 1
097: try {
098: in = new BufferedReader(new InputStreamReader(
099: new FileInputStream(filterlog_path), "ISO-8859-1"));
100: while ((oldline = in.readLine()) != null) {
101: lineNum++;
102: try {
103: line = new String(oldline.getBytes("ISO-8859-1"),
104: "UTF-8");
105: } catch (Exception e) {
106: continue;
107: }
108: // expecting
109: //[22/Jan/2002:10:48:19] 9883@001: Filter log started
110: if (line.startsWith("[") /*|| line.startsWith("Time:")*/) {
111: int start = line.indexOf('[');
112: int end = line.indexOf(']');
113: if (start >= 0 && end > start) {
114: String datestr = line.substring(start + 1, end);
115: try {
116: date = df.parse(datestr);
117: if (debug != null) {
118: debug.println("add date : "
119: + df.format(date));
120: }
121: runList.add(date);
122: if (dateStr != null
123: && datestr.compareTo(dateStr) == 0) {
124: inSelectedDate = true;
125: } else {
126: inSelectedDate = false;
127: }
128: } catch (ParseException e) {
129: if (debug != null) {
130: debug
131: .println("ParseException at position("
132: + e.getErrorOffset()
133: + ")");
134: debug.println(line);
135: } else {
136: System.out
137: .println("ParseException at position("
138: + e.getErrorOffset()
139: + ")");
140: System.out.println(line);
141: }
142: }
143: }
144: } else if (inSelectedDate && line.startsWith("Hint:")) {
145: String hint = line.substring(5).trim();
146: if (hint.startsWith("File Not Found.")) {
147: hint = "File Not Found.";
148: }
149: if (preLine != null) {
150: int c = 1;
151: if (ExReasons.containsKey(hint)) {
152: Integer count = (Integer) ExReasons
153: .get(hint);
154: c = count.intValue();
155: c++;
156: ExReasons.put(hint, new Integer(c));
157: } else {
158: ExReasons.put(hint, new Integer(c));
159: ExReasonsArray.add(hint);
160: }
161: total++;
162: if ((reasonStr != null && reasonStr
163: .compareToIgnoreCase(hint) == 0)
164: || ((reasonIndex >= 0
165: && ExReasonsArray.size() > reasonIndex && ((String) ExReasonsArray
166: .get(reasonIndex))
167: .compareToIgnoreCase(hint) == 0))) {
168: FilteredList.add(preLine);
169: }
170: preLine = null;
171: }
172: } else if (inSelectedDate) {
173: preLine = line;
174: }
175: }
176: } catch (IOException e) {
177: if (debug != null) {
178: debug.println("IOException:" + e.getMessage()
179: + " at line# " + lineNum);
180: } else {
181: System.out.println("IOException:" + e.getMessage()
182: + " at line# " + lineNum);
183: }
184: }
185: }
186:
187: /* public void printRunList(PrintWriter out)
188: {
189: for (int i=0; i < runList.size(); i++) {
190: out.println(i + " run - " + listdf.format((Date)runList.get(i)) );
191: }
192: }
193: */
194: public void printResult(PrintStream out) {
195: for (int i = 0; i < runList.size(); i++) {
196: out.println(i + " run - "
197: + listdf.format((Date) runList.get(i)));
198: }
199: if (dateStr != null) {
200: Enumeration e = ExReasons.keys();
201: while (e.hasMoreElements()) {
202: String reason = (String) e.nextElement();
203: Integer count = (Integer) ExReasons.get(reason);
204: out.println(reason + ":" + count);
205: }
206: if (reasonStr != null) {
207: for (int i = 0; i < FilteredList.size(); i++) {
208: out.println((String) FilteredList.get(i));
209: }
210: }
211: }
212:
213: }
214:
215: public Object[] getRunArray() {
216: return runList.toArray();
217: }
218:
219: public ArrayList getRunArrayList() {
220: return runList;
221: }
222:
223: public void setDebug(PrintWriter out) {
224: debug = out;
225: }
226:
227: public String[] getReasons() {
228: return (String[]) ExReasonsArray.toArray(new String[0]);
229: }
230:
231: public int getReasonSubTotal(String reason) {
232: if (ExReasons != null) {
233: Integer count = (Integer) ExReasons.get(reason);
234: return count.intValue();
235: }
236: return 0;
237: }
238:
239: public ArrayList getFilteredURL() {
240: return FilteredList;
241: }
242:
243: public String getReason() {
244: return reasonStr;
245: }
246:
247: public String getReasonByIndex(int index) {
248: try {
249: return (String) ExReasonsArray.get(index);
250: } catch (Exception e) {
251: return null;
252: }
253: }
254:
255: public int getTotal() {
256: return total;
257: }
258: }
|