001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.webadmin.main;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.FileReader;
021: import java.io.FilenameFilter;
022: import java.io.IOException;
023: import java.io.PrintWriter;
024: import java.text.SimpleDateFormat;
025: import java.util.Arrays;
026: import java.util.Date;
027:
028: import org.apache.regexp.RE;
029: import org.apache.regexp.RESyntaxException;
030: import org.apache.openejb.webadmin.HttpRequest;
031: import org.apache.openejb.webadmin.HttpResponse;
032: import org.apache.openejb.webadmin.WebAdminBean;
033: import org.apache.openejb.webadmin.HttpHome;
034: import org.apache.openejb.loader.SystemInstance;
035:
036: import javax.ejb.Stateless;
037: import javax.ejb.RemoteHome;
038:
039: /** This bean lists the openejb.log and transaction.log files
040: *
041: * @author <a href="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
042: */
043: @Stateless(name="Webadmin/ListLogs")
044: @RemoteHome(HttpHome.class)
045: public class ListLogsBean extends WebAdminBean {
046: /** the form field used for a regular expression search */
047: private static final String REGULAR_EXPRESSION_SEARCH = "regularExpression";
048: private static final String DISPLAY_TYPE_FILTER = "filter";
049: private static final String DISPLAY_TYPE_HIGHLIGHT = "highlight";
050: private static final String DISPLAY_TYPE = "displayType";
051:
052: /** the type of log we're using */
053: private String logType;
054:
055: /** called with the bean is created */
056: public void ejbCreate() {
057: this .section = "ListLogs";
058: }
059:
060: /** after the processing is completed
061: * @param request the http request
062: * @param response the http response
063: * @throws IOException if an exception is thrown
064: */
065: public void postProcess(HttpRequest request, HttpResponse response)
066: throws IOException {
067: }
068:
069: /** before the processing is done
070: * @param request the http request
071: * @param response the http response
072: * @throws IOException if an exception is thrown
073: */
074: public void preProcess(HttpRequest request, HttpResponse response)
075: throws IOException {
076: //get the log type
077: this .logType = request.getQueryParameter("log");
078: }
079:
080: /** Write the main content
081: *
082: * @param body the output to write to
083: * @exception IOException if an exception is thrown
084: */
085: public void writeBody(PrintWriter body) throws IOException {
086: //string for re search
087: String regularExpressionSearch = request
088: .getFormParameter(REGULAR_EXPRESSION_SEARCH);
089: String displayType = request.getFormParameter(DISPLAY_TYPE);
090: if (regularExpressionSearch == null) {
091: regularExpressionSearch = "";
092: }
093:
094: // Get the logs directory
095: File logsDir = SystemInstance.get().getBase().getDirectory(
096: "logs");
097: String path;
098:
099: File[] openejbLogs = logsDir.listFiles(new FilenameFilter() {
100: public boolean accept(File dir, String name) {
101: return (name.indexOf(".log") != -1);
102: }
103: });
104:
105: Arrays.sort(openejbLogs);
106: int printIndex = 0;
107: SimpleDateFormat dateFormat = new SimpleDateFormat(
108: "MMM dd yyyy HH:mm:ss");
109:
110: for (int i = 0; i < openejbLogs.length; i++) {
111: if ((this .logType == null && i == 0)
112: || (this .logType != null && this .logType
113: .equals(openejbLogs[i].getName()))) {
114: body.print(openejbLogs[i].getName());
115: printIndex = i;
116: } else {
117: body.print("<a href=\"ListLogs?log="
118: + openejbLogs[i].getName() + "\">"
119: + openejbLogs[i].getName() + "</a>");
120: }
121:
122: if (i < openejbLogs.length - 1) {
123: body.print(" | ");
124: }
125: }
126: body.println("<br><br>");
127:
128: //calculate the size of the file in kb or bytes
129: String fileLength = "0 bytes";
130: long longFileLength = 0;
131: if (openejbLogs[printIndex].length() > 0) {
132: if (openejbLogs[printIndex].length() > 1000) {
133: longFileLength = openejbLogs[printIndex].length() / 1000;
134: fileLength = String.valueOf(longFileLength) + " kb";
135: } else {
136: fileLength = String.valueOf(openejbLogs[printIndex]
137: .length())
138: + " bytes";
139: }
140: }
141:
142: //set the path for the form action
143: if (request.getURI().getQuery() == null
144: || "".equals(request.getURI().getQuery())) {
145: path = request.getURI().getPath();
146: } else {
147: path = request.getURI().getPath() + "?"
148: + request.getURI().getQuery();
149: }
150:
151: body.print("<form action=\"");
152: body.print(path.substring(1));
153: body.println("\" method=\"post\">");
154: body
155: .println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"525\">");
156: body.println("<tr>\n<td valign=\"top\">");
157: body
158: .println("----------------------------------------------------------<br>");
159: body.println("Last Modified: "
160: + dateFormat.format(new Date(openejbLogs[printIndex]
161: .lastModified())) + "<br>");
162: body.println("Size: " + fileLength + "<br>");
163: body
164: .println("----------------------------------------------------------");
165: body.println("</td>\n<td>");
166: //the form for regular expresions goes here
167: body
168: .println("You may do a text search by using regular<br>expressions. Enter "
169: + "your regular expression<br>below. If you are not familiar with regular<br>"
170: + "expresions see <a href=\"http://jakarta.apache.org/regexp/apidocs/org/apache/"
171: + "regexp/RE.html\" target=\"_blank\">Apache Regexp</a> for more<br>information.<br>");
172: body.print("Regular Expression:  ");
173: body.print("<input type=\"text\" name=\"");
174: body.print(REGULAR_EXPRESSION_SEARCH);
175: body.print("\" value=\"");
176: body.print(regularExpressionSearch);
177: body
178: .println("\"><br>\nHightlight <input type=\"radio\" name=\"");
179: body.print(DISPLAY_TYPE);
180: body.print("\" value=\"");
181: body.print(DISPLAY_TYPE_HIGHLIGHT);
182: body.print("\" checked>\nFilter <input type=\"radio\" name=\"");
183: body.print(DISPLAY_TYPE);
184: body.print("\" value=\"");
185: body.print(DISPLAY_TYPE_FILTER);
186: body
187: .println("\"><br><br>\n<input type=\"submit\" value=\"Search\" name=\"regExpSubmit\">");
188: body.println("</td>\n</tr>\n</table>\n</form>\n<br>");
189:
190: if (!"".equals(regularExpressionSearch)) {
191: body
192: .println("The results of your search are highlighted below.<br><br>");
193: }
194:
195: this .printLogFile(body, openejbLogs[printIndex],
196: regularExpressionSearch, displayType);
197: }
198:
199: /** gets the openejb.log file
200: * @param body the output to send the data to
201: * @param logFile the logfile that we're printing
202: * @throws IOException if an exception is thrown
203: */
204: private void printLogFile(PrintWriter body, File logFile,
205: String reSearch, String displayType) throws IOException {
206: BufferedReader fileReader = new BufferedReader(new FileReader(
207: logFile));
208: StringBuffer lineOfText;
209: RE regularExpressionSearch = null;
210: boolean filterSearch = false;
211: boolean highlightSearch = false;
212: boolean matchFound;
213:
214: //create a search reg expression
215: if (!"".equals(reSearch.trim())) {
216: try {
217: regularExpressionSearch = new RE(reSearch);
218: } catch (RESyntaxException e) {
219: throw new IOException(e.getMessage());
220: }
221:
222: //set these only if there is a search
223: if (DISPLAY_TYPE_FILTER.equals(displayType)) {
224: filterSearch = true;
225: } else if (DISPLAY_TYPE_HIGHLIGHT.equals(displayType)) {
226: highlightSearch = true;
227: }
228: }
229:
230: //create a list of special characters
231: String[][] specialChars = new String[3][2];
232: specialChars[0][0] = "&";
233: specialChars[0][1] = "&";
234: specialChars[1][0] = "<";
235: specialChars[1][1] = "<";
236: specialChars[2][0] = ">";
237: specialChars[2][1] = ">";
238: int lineCounter = 0;
239: String background;
240:
241: try {
242: //create an array of regular expressions
243: RE[] expArray = new RE[5];
244: expArray[0] = new RE("^INFO :");
245: expArray[1] = new RE("^DEBUG:");
246: expArray[2] = new RE("^WARN :");
247: expArray[3] = new RE("^ERROR:");
248: expArray[4] = new RE("^FATAL:");
249:
250: //create an array of colors
251: String[] colorArray = new String[5];
252: colorArray[0] = "log4j-info";
253: colorArray[1] = "log4j-debug";
254: colorArray[2] = "log4j-warn";
255: colorArray[3] = "log4j-error";
256: colorArray[4] = "log4j-fatal";
257:
258: //create a table to write the file to
259: body
260: .println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
261:
262: //read the file line by line
263: String expMatch = colorArray[0];
264: String temp;
265: while (true) {
266: //check for null and break
267: temp = fileReader.readLine();
268: if (temp == null) {
269: break;
270: }
271:
272: lineCounter++;
273: lineOfText = new StringBuffer(temp);
274:
275: //check for and replace special characters
276: String charToCheck;
277: for (int i = 0; i < lineOfText.length(); i++) {
278: //pick out the current character
279: charToCheck = String.valueOf(lineOfText.charAt(i));
280: for (int j = 0; j < specialChars.length; j++) {
281: //do the check for equals
282: if (charToCheck.equals(specialChars[j][0])) {
283: lineOfText.replace(i, i + 1,
284: specialChars[j][1]);
285: break;
286: }
287: }
288: }
289:
290: temp = lineOfText.toString();
291: //loop through the array of expressions to find a match
292: for (int i = 0; i < expArray.length; i++) {
293: if (expArray[i].match(temp)) {
294: expMatch = colorArray[i];
295: break;
296: }
297: }
298:
299: //check for an re search
300: matchFound = false;
301: background = "";
302: if (regularExpressionSearch != null
303: && regularExpressionSearch.match(temp)) {
304: matchFound = true;
305: if (highlightSearch) {
306: background = " bgcolor=\"#00ffff\"";
307: }
308: }
309:
310: //print line of text to the page
311: if ((filterSearch || highlightSearch)
312: && (filterSearch && !matchFound)) {
313: continue;
314: }
315:
316: body
317: .println(new StringBuffer(100)
318: .append("<tr")
319: .append(background)
320: .append(
321: "><td align=\"right\" valign=\"top\" class=\"")
322: .append(colorArray[0])
323: .append("\">")
324: .append(lineCounter)
325: .append(
326: "</td><td> </td><td class=\"")
327: .append(expMatch).append("\">").append(
328: temp).append("</td></tr>")
329: .toString());
330: }
331:
332: body.println("</table>");
333: } catch (RESyntaxException se) {
334: throw new IOException(se.getMessage());
335: }
336:
337: //close the file
338: fileReader.close();
339: }
340:
341: /** Write the TITLE of the HTML document. This is the part
342: * that goes into the <code><head><title>
343: * </title></head></code> tags
344: *
345: * @param body the output to write to
346: * @exception IOException of an exception is thrown
347: *
348: */
349: public void writeHtmlTitle(PrintWriter body) throws IOException {
350: body.println(HTML_TITLE);
351: }
352:
353: /** Write the title of the page. This is displayed right
354: * above the main block of content.
355: *
356: * @param body the output to write to
357: * @exception IOException if an exception is thrown
358: */
359: public void writePageTitle(PrintWriter body) throws IOException {
360: body.println("System Log Files");
361: }
362:
363: /** Write the sub items for this bean in the left navigation bar of
364: * the page. This should look somthing like the one below:
365: *
366: * <code>
367: * <tr>
368: * <td valign="top" align="left">
369: * <a href="system?show=deployments"><span class="subMenuOff">
370: * Deployments
371: * </span>
372: * </a></td>
373: * </tr>
374: * </code>
375: *
376: * Alternately, the bean can use the method formatSubMenuItem(..) which
377: * will create HTML like the one above
378: *
379: * @param body the output to write to
380: * @exception IOException if an exception is thrown
381: *
382: */
383: public void writeSubMenuItems(PrintWriter body) throws IOException {
384: }
385:
386: }
|