KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > main > ListLogsBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ListLogsBean.java 1988 2005-07-09 08:51:00Z dblevins $
44  */

45 package org.openejb.webadmin.main;
46
47 import java.io.BufferedReader JavaDoc;
48 import java.io.File JavaDoc;
49 import java.io.FileReader JavaDoc;
50 import java.io.FilenameFilter JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.PrintWriter JavaDoc;
53 import java.text.SimpleDateFormat JavaDoc;
54 import java.util.Arrays JavaDoc;
55 import java.util.Date JavaDoc;
56
57 import org.apache.regexp.RE;
58 import org.apache.regexp.RESyntaxException;
59 import org.openejb.webadmin.HttpRequest;
60 import org.openejb.webadmin.HttpResponse;
61 import org.openejb.webadmin.WebAdminBean;
62 import org.openejb.util.FileUtils;
63 import org.openejb.loader.SystemInstance;
64
65 /** This bean lists the openejb.log and transaction.log files
66  *
67  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
68  */

69 public class ListLogsBean extends WebAdminBean {
70     /** the form field used for a regular expression search */
71     private static final String JavaDoc REGULAR_EXPRESSION_SEARCH = "regularExpression";
72     private static final String JavaDoc DISPLAY_TYPE_FILTER = "filter";
73     private static final String JavaDoc DISPLAY_TYPE_HIGHLIGHT = "highlight";
74     private static final String JavaDoc DISPLAY_TYPE = "displayType";
75
76     /** the type of log we're using */
77     private String JavaDoc logType;
78     /** called with the bean is created */
79     public void ejbCreate() {
80         this.section = "ListLogs";
81     }
82
83     /** after the processing is completed
84      * @param request the http request
85      * @param response the http response
86      * @throws IOException if an exception is thrown
87      */

88     public void postProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc {}
89
90     /** before the processing is done
91      * @param request the http request
92      * @param response the http response
93      * @throws IOException if an exception is thrown
94      */

95     public void preProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc {
96         //get the log type
97
this.logType = request.getQueryParameter("log");
98     }
99
100     /** Write the main content
101      *
102      * @param body the output to write to
103      * @exception IOException if an exception is thrown
104      */

105     public void writeBody(PrintWriter JavaDoc body) throws IOException JavaDoc {
106         //string for re search
107
String JavaDoc regularExpressionSearch = request.getFormParameter(REGULAR_EXPRESSION_SEARCH);
108         String JavaDoc displayType = request.getFormParameter(DISPLAY_TYPE);
109         if (regularExpressionSearch == null) {
110             regularExpressionSearch = "";
111         }
112
113         // Get the logs directory
114
File JavaDoc logsDir = SystemInstance.get().getBase().getDirectory("logs");
115         String JavaDoc path;
116
117         File JavaDoc[] openejbLogs = logsDir.listFiles(new FilenameFilter JavaDoc() {
118             public boolean accept(File JavaDoc dir, String JavaDoc name) {
119                 return (name.indexOf(".log") != -1);
120             }
121         });
122
123         Arrays.sort(openejbLogs);
124         int printIndex = 0;
125         SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("MMM dd yyyy HH:mm:ss");
126
127         for (int i = 0; i < openejbLogs.length; i++) {
128             if ((this.logType == null && i == 0)
129                 || (this.logType != null && this.logType.equals(openejbLogs[i].getName()))) {
130                 body.print(openejbLogs[i].getName());
131                 printIndex = i;
132             } else {
133                 body.print("<a HREF=\"ListLogs?log=" + openejbLogs[i].getName() + "\">" + openejbLogs[i].getName() + "</a>");
134             }
135
136             if (i < openejbLogs.length - 1) {
137                 body.print("&nbsp;|&nbsp;");
138             }
139         }
140         body.println("<br><br>");
141
142         //calculate the size of the file in kb or bytes
143
String JavaDoc fileLength = "0 bytes";
144         long longFileLength = 0;
145         if (openejbLogs[printIndex].length() > 0) {
146             if (openejbLogs[printIndex].length() > 1000) {
147                 longFileLength = openejbLogs[printIndex].length() / 1000;
148                 fileLength = String.valueOf(longFileLength) + " kb";
149             } else {
150                 fileLength = String.valueOf(openejbLogs[printIndex].length()) + " bytes";
151             }
152         }
153
154         //set the path for the form action
155
if (request.getURI().getQuery() == null || "".equals(request.getURI().getQuery())) {
156             path = request.getURI().getPath();
157         } else {
158             path = request.getURI().getPath() + "?" + request.getURI().getQuery();
159         }
160
161         body.print("<form action=\"");
162         body.print(path.substring(1));
163         body.println("\" method=\"post\">");
164         body.println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"525\">");
165         body.println("<tr>\n<td valign=\"top\">");
166         body.println("----------------------------------------------------------<br>");
167         body.println("Last Modified: " + dateFormat.format(new Date JavaDoc(openejbLogs[printIndex].lastModified())) + "<br>");
168         body.println("Size: " + fileLength + "<br>");
169         body.println("----------------------------------------------------------");
170         body.println("</td>\n<td>");
171         //the form for regular expresions goes here
172
body.println(
173             "You may do a text search by using regular<br>expressions. Enter "
174                 + "your regular expression<br>below. If you are not familiar with regular<br>"
175                 + "expresions see <a HREF=\"http://jakarta.apache.org/regexp/apidocs/org/apache/"
176                 + "regexp/RE.html\" target=\"_blank\">Apache Regexp</a> for more<br>information.<br>");
177         body.print("Regular Expression:&nbsp&nbsp");
178         body.print("<input type=\"text\" name=\"");
179         body.print(REGULAR_EXPRESSION_SEARCH);
180         body.print("\" value=\"");
181         body.print(regularExpressionSearch);
182         body.println("\"><br>\nHightlight <input type=\"radio\" name=\"");
183         body.print(DISPLAY_TYPE);
184         body.print("\" value=\"");
185         body.print(DISPLAY_TYPE_HIGHLIGHT);
186         body.print("\" checked>\nFilter <input type=\"radio\" name=\"");
187         body.print(DISPLAY_TYPE);
188         body.print("\" value=\"");
189         body.print(DISPLAY_TYPE_FILTER);
190         body.println("\"><br><br>\n<input type=\"submit\" value=\"Search\" name=\"regExpSubmit\">");
191         body.println("</td>\n</tr>\n</table>\n</form>\n<br>");
192
193         if (!"".equals(regularExpressionSearch)) {
194             body.println("The results of your search are highlighted below.<br><br>");
195         }
196
197         this.printLogFile(body, openejbLogs[printIndex], regularExpressionSearch, displayType);
198     }
199
200     /** gets the openejb.log file
201      * @param body the output to send the data to
202      * @param logFile the logfile that we're printing
203      * @throws IOException if an exception is thrown
204      */

205     private void printLogFile(PrintWriter JavaDoc body, File JavaDoc logFile, String JavaDoc reSearch, String JavaDoc displayType) throws IOException JavaDoc {
206         BufferedReader JavaDoc fileReader = new BufferedReader JavaDoc(new FileReader JavaDoc(logFile));
207         StringBuffer JavaDoc lineOfText;
208         RE regularExpressionSearch = null;
209         boolean filterSearch = false;
210         boolean highlightSearch = false;
211         boolean matchFound;
212
213         //create a search reg expression
214
if (!"".equals(reSearch.trim())) {
215             try {
216                 regularExpressionSearch = new RE(reSearch);
217             } catch (RESyntaxException e) {
218                 throw new IOException JavaDoc(e.getMessage());
219             }
220
221             //set these only if there is a search
222
if (DISPLAY_TYPE_FILTER.equals(displayType)) {
223                 filterSearch = true;
224             } else if (DISPLAY_TYPE_HIGHLIGHT.equals(displayType)) {
225                 highlightSearch = true;
226             }
227         }
228
229         //create a list of special characters
230
String JavaDoc[][] specialChars = new String JavaDoc[3][2];
231         specialChars[0][0] = "&";
232         specialChars[0][1] = "&amp;";
233         specialChars[1][0] = "<";
234         specialChars[1][1] = "&lt;";
235         specialChars[2][0] = ">";
236         specialChars[2][1] = "&gt;";
237         int lineCounter = 0;
238         String JavaDoc background;
239
240         try {
241             //create an array of regular expressions
242
RE[] expArray = new RE[5];
243             expArray[0] = new RE("^INFO :");
244             expArray[1] = new RE("^DEBUG:");
245             expArray[2] = new RE("^WARN :");
246             expArray[3] = new RE("^ERROR:");
247             expArray[4] = new RE("^FATAL:");
248
249             //create an array of colors
250
String JavaDoc[] colorArray = new String JavaDoc[5];
251             colorArray[0] = "log4j-info";
252             colorArray[1] = "log4j-debug";
253             colorArray[2] = "log4j-warn";
254             colorArray[3] = "log4j-error";
255             colorArray[4] = "log4j-fatal";
256
257             //create a table to write the file to
258
body.println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
259
260             //read the file line by line
261
String JavaDoc expMatch = colorArray[0];
262             String JavaDoc temp;
263             while (true) {
264                 //check for null and break
265
temp = fileReader.readLine();
266                 if (temp == null) {
267                     break;
268                 }
269
270                 lineCounter++;
271                 lineOfText = new StringBuffer JavaDoc(temp);
272
273                 //check for and replace special characters
274
String JavaDoc charToCheck;
275                 for (int i = 0; i < lineOfText.length(); i++) {
276                     //pick out the current character
277
charToCheck = String.valueOf(lineOfText.charAt(i));
278                     for (int j = 0; j < specialChars.length; j++) {
279                         //do the check for equals
280
if (charToCheck.equals(specialChars[j][0])) {
281                             lineOfText.replace(i, i + 1, specialChars[j][1]);
282                             break;
283                         }
284                     }
285                 }
286
287                 temp = lineOfText.toString();
288                 //loop through the array of expressions to find a match
289
for (int i = 0; i < expArray.length; i++) {
290                     if (expArray[i].match(temp)) {
291                         expMatch = colorArray[i];
292                         break;
293                     }
294                 }
295
296                 //check for an re search
297
matchFound = false;
298                 background = "";
299                 if (regularExpressionSearch != null && regularExpressionSearch.match(temp)) {
300                     matchFound = true;
301                     if (highlightSearch) {
302                         background = " bgcolor=\"#00ffff\"";
303                     }
304                 }
305
306                 //print line of text to the page
307
if ((filterSearch || highlightSearch) && (filterSearch && !matchFound)) {
308                     continue;
309                 }
310
311                 body.println(
312                     new StringBuffer JavaDoc(100)
313                         .append("<tr")
314                         .append(background)
315                         .append("><td align=\"right\" valign=\"top\" class=\"")
316                         .append(colorArray[0])
317                         .append("\">")
318                         .append(lineCounter)
319                         .append("</td><td>&nbsp;</td><td class=\"")
320                         .append(expMatch)
321                         .append("\">")
322                         .append(temp)
323                         .append("</td></tr>")
324                         .toString());
325             }
326
327             body.println("</table>");
328         } catch (RESyntaxException se) {
329             throw new IOException JavaDoc(se.getMessage());
330         }
331
332         //close the file
333
fileReader.close();
334     }
335
336     /** Write the TITLE of the HTML document. This is the part
337       * that goes into the <code>&lt;head&gt;&lt;title&gt;
338       * &lt;/title&gt;&lt;/head&gt;</code> tags
339       *
340       * @param body the output to write to
341       * @exception IOException of an exception is thrown
342       *
343       */

344     public void writeHtmlTitle(PrintWriter JavaDoc body) throws IOException JavaDoc {
345         body.println(HTML_TITLE);
346     }
347
348     /** Write the title of the page. This is displayed right
349      * above the main block of content.
350      *
351      * @param body the output to write to
352      * @exception IOException if an exception is thrown
353      */

354     public void writePageTitle(PrintWriter JavaDoc body) throws IOException JavaDoc {
355         body.println("System Log Files");
356     }
357
358     /** Write the sub items for this bean in the left navigation bar of
359      * the page. This should look somthing like the one below:
360      *
361      * <code>
362      * &lt;tr&gt;
363      * &lt;td valign="top" align="left"&gt;
364      * &lt;a HREF="system?show=deployments"&gt;&lt;span class="subMenuOff"&gt;
365      * &nbsp;&nbsp;&nbsp;Deployments
366      * &lt;/span&gt;
367      * &lt;/a&gt;&lt;/td&gt;
368      * &lt;/tr&gt;
369      * </code>
370      *
371      * Alternately, the bean can use the method formatSubMenuItem(..) which
372      * will create HTML like the one above
373      *
374      * @param body the output to write to
375      * @exception IOException if an exception is thrown
376      *
377      */

378     public void writeSubMenuItems(PrintWriter JavaDoc body) throws IOException JavaDoc {}
379
380 }
381
Popular Tags