1 | package ise.antelope.tasks;
|
---|
2 |
|
---|
3 | import java.net.URL;
|
---|
4 | import java.text.*;
|
---|
5 | import java.util.*;
|
---|
6 | import java.util.regex.*;
|
---|
7 |
|
---|
8 | import org.apache.tools.ant.BuildException;
|
---|
9 |
|
---|
10 | import org.apache.tools.ant.Task;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Not an antelope task, this is for Cobalt, but put it in the same package
|
---|
14 | * for easier use.
|
---|
15 | *
|
---|
16 | * @version $Revision: 1.1 $
|
---|
17 | */
|
---|
18 | public class ReadLog extends Task {
|
---|
19 |
|
---|
20 | private URL logURL = null;
|
---|
21 | private String property = null;
|
---|
22 | private int minutes = 2;
|
---|
23 |
|
---|
24 | public void setUrl(URL url) {
|
---|
25 | logURL = url;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Where to put the log contents.
|
---|
30 | *
|
---|
31 | * @param name The new property value
|
---|
32 | */
|
---|
33 | public void setProperty(String name) {
|
---|
34 | property = name;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Set the number of minutes to read, so, for example, 2 would read the most
|
---|
39 | * recent 2 minutes from the log.
|
---|
40 | *
|
---|
41 | * @param m the number of minutes. Use -1 to read the complete log.
|
---|
42 | * Default setting is to read 2 minutes.
|
---|
43 | */
|
---|
44 | public void setMinutes(int m) {
|
---|
45 | minutes = m;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | public void execute() {
|
---|
50 | // check attributes
|
---|
51 | if (property == null)
|
---|
52 | throw new BuildException("Property is null.");
|
---|
53 | if (logURL == null)
|
---|
54 | throw new BuildException("URL is null.");
|
---|
55 | if (minutes < -1)
|
---|
56 | minutes = -1;
|
---|
57 |
|
---|
58 | try {
|
---|
59 | // fetch the log
|
---|
60 | PostTask post = new PostTask();
|
---|
61 | post.setProject(getProject());
|
---|
62 | post.setTo(logURL);
|
---|
63 | Date now = new Date();
|
---|
64 | String temp_property = property + now.getTime();
|
---|
65 | post.setProperty(temp_property);
|
---|
66 | post.setVerbose(false);
|
---|
67 | post.execute();
|
---|
68 | String log = getProject().getProperty(temp_property);
|
---|
69 |
|
---|
70 | if (minutes == -1) {
|
---|
71 | // save the complete log
|
---|
72 | getProject().setProperty(property, log);
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | // calculate how much of log to save into property
|
---|
76 | long end_time = now.getTime() - (minutes * 1000 * 60);
|
---|
77 |
|
---|
78 | // split up the log to parse the dates
|
---|
79 | String[] msgs = log.split("<hr>");
|
---|
80 |
|
---|
81 | int i = 1;
|
---|
82 | for (; i < msgs.length; i++) {
|
---|
83 | Grep grep = new Grep();
|
---|
84 | grep.setIn(msgs[i]);
|
---|
85 | grep.setRegex("(.*?[:].*?[:].*?)[:].*?");
|
---|
86 | grep.setGroup(1);
|
---|
87 | String start = grep.grep();
|
---|
88 | if (start == null) {
|
---|
89 | continue;
|
---|
90 | }
|
---|
91 | SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
|
---|
92 | try {
|
---|
93 | Date start_date = sdf.parse(start);
|
---|
94 | if (start_date.getTime() < end_time)
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | catch(Exception e) {
|
---|
98 | e.printStackTrace();
|
---|
99 | continue;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | // build a string out of the appropriate pieces
|
---|
104 | StringBuffer sb = new StringBuffer();
|
---|
105 | for (int j = 1; j < i; j++) {
|
---|
106 | sb.append(msgs[j]).append("\n");
|
---|
107 | }
|
---|
108 |
|
---|
109 | // set the property with the log contents
|
---|
110 | getProject().setProperty(property, sb.toString());
|
---|
111 | }
|
---|
112 |
|
---|
113 | Unset unset = new Unset();
|
---|
114 | unset.setProject(getProject());
|
---|
115 | unset.setName(temp_property);
|
---|
116 | unset.execute();
|
---|
117 | }
|
---|
118 | catch(Exception e) {
|
---|
119 | e.printStackTrace();
|
---|
120 | throw new BuildException(e.getMessage());
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|