KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > CruiseControlLogParser


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import java.io.BufferedReader JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.InputStreamReader JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.net.URLConnection JavaDoc;
11 import java.util.zip.GZIPInputStream JavaDoc;
12
13 public class CruiseControlLogParser {
14
15   private static final String JavaDoc MARKER = "...]]></message>";
16
17   private static final String JavaDoc TAG = " <message priority=\"info\"><![CDATA[";
18   private static final String JavaDoc CLOSE_TAG = "]]></message>";
19
20   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
21     if (args.length != 2) {
22       usage();
23       System.exit(1);
24     }
25
26     String JavaDoc urlStr = args[0];
27     String JavaDoc testName = args[1];
28
29     String JavaDoc failTestEnd = TAG + "ERROR Test " + testName + " FAILED" + CLOSE_TAG;
30
31     int linenum = 0;
32     boolean inTest = false;
33     URL JavaDoc url = new URL JavaDoc(urlStr);
34     URLConnection JavaDoc connect = url.openConnection();
35     String JavaDoc encoding = connect.getContentEncoding();
36     InputStream JavaDoc response = connect.getInputStream();
37     if ("x-gzip".equals(encoding) || urlStr.toLowerCase().endsWith(".gz")) {
38       response = new GZIPInputStream JavaDoc(response);
39     }
40
41     try {
42       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(response));
43       String JavaDoc line;
44
45       while ((line = reader.readLine()) != null) {
46         linenum++;
47         if (inTest) {
48           if (line.endsWith("Test" + MARKER)) break; // start of another test
49

50           if (line.startsWith(TAG)) {
51             output(line);
52             if (line.startsWith(failTestEnd)) {
53               break;
54             }
55           }
56         } else if (line.endsWith(testName + MARKER)) {
57           output(line);
58           inTest = true;
59         }
60       }
61     } catch (Exception JavaDoc e) {
62       System.err.println("***************** PARSER ERROR (line: " + linenum + ") *****************");
63       e.printStackTrace();
64     }
65   }
66
67   private static void output(String JavaDoc line) {
68     boolean err = (line.indexOf("ERROR") != -1 || line.indexOf("WARN") != -1);
69     line = line.substring(TAG.length(), line.length() - CLOSE_TAG.length());
70     line = line.replaceAll("^WARN( )?", "");
71     line = line.replaceAll("^INFO( )?", "");
72     line = line.replaceAll("^ERROR( )?", "");
73     line = line.replaceAll("&lt;", "<");
74     line = line.replaceAll("&gt;", ">");
75     if (err) System.err.println(line);
76     else System.out.println(line);
77   }
78
79   private static void usage() {
80     System.err.println("usage: " + CruiseControlLogParser.class.getName() + " <log URL> <test-name>");
81     System.err.println();
82     System.err.println(" example args: http://sand/long-strange-path-to-monkeylog.xml TwelveByteIntegerTest");
83   }
84 }
Popular Tags