1 45 package org.openejb.webadmin.main; 46 47 import java.io.BufferedReader ; 48 import java.io.File ; 49 import java.io.FileReader ; 50 import java.io.FilenameFilter ; 51 import java.io.IOException ; 52 import java.io.PrintWriter ; 53 import java.text.SimpleDateFormat ; 54 import java.util.Arrays ; 55 import java.util.Date ; 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 69 public class ListLogsBean extends WebAdminBean { 70 71 private static final String REGULAR_EXPRESSION_SEARCH = "regularExpression"; 72 private static final String DISPLAY_TYPE_FILTER = "filter"; 73 private static final String DISPLAY_TYPE_HIGHLIGHT = "highlight"; 74 private static final String DISPLAY_TYPE = "displayType"; 75 76 77 private String logType; 78 79 public void ejbCreate() { 80 this.section = "ListLogs"; 81 } 82 83 88 public void postProcess(HttpRequest request, HttpResponse response) throws IOException {} 89 90 95 public void preProcess(HttpRequest request, HttpResponse response) throws IOException { 96 this.logType = request.getQueryParameter("log"); 98 } 99 100 105 public void writeBody(PrintWriter body) throws IOException { 106 String regularExpressionSearch = request.getFormParameter(REGULAR_EXPRESSION_SEARCH); 108 String displayType = request.getFormParameter(DISPLAY_TYPE); 109 if (regularExpressionSearch == null) { 110 regularExpressionSearch = ""; 111 } 112 113 File logsDir = SystemInstance.get().getBase().getDirectory("logs"); 115 String path; 116 117 File [] openejbLogs = logsDir.listFiles(new FilenameFilter () { 118 public boolean accept(File dir, String name) { 119 return (name.indexOf(".log") != -1); 120 } 121 }); 122 123 Arrays.sort(openejbLogs); 124 int printIndex = 0; 125 SimpleDateFormat dateFormat = new SimpleDateFormat ("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(" | "); 138 } 139 } 140 body.println("<br><br>"); 141 142 String 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 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 (openejbLogs[printIndex].lastModified())) + "<br>"); 168 body.println("Size: " + fileLength + "<br>"); 169 body.println("----------------------------------------------------------"); 170 body.println("</td>\n<td>"); 171 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:  "); 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 205 private void printLogFile(PrintWriter body, File logFile, String reSearch, String displayType) throws IOException { 206 BufferedReader fileReader = new BufferedReader (new FileReader (logFile)); 207 StringBuffer lineOfText; 208 RE regularExpressionSearch = null; 209 boolean filterSearch = false; 210 boolean highlightSearch = false; 211 boolean matchFound; 212 213 if (!"".equals(reSearch.trim())) { 215 try { 216 regularExpressionSearch = new RE(reSearch); 217 } catch (RESyntaxException e) { 218 throw new IOException (e.getMessage()); 219 } 220 221 if (DISPLAY_TYPE_FILTER.equals(displayType)) { 223 filterSearch = true; 224 } else if (DISPLAY_TYPE_HIGHLIGHT.equals(displayType)) { 225 highlightSearch = true; 226 } 227 } 228 229 String [][] specialChars = new String [3][2]; 231 specialChars[0][0] = "&"; 232 specialChars[0][1] = "&"; 233 specialChars[1][0] = "<"; 234 specialChars[1][1] = "<"; 235 specialChars[2][0] = ">"; 236 specialChars[2][1] = ">"; 237 int lineCounter = 0; 238 String background; 239 240 try { 241 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 String [] colorArray = new String [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 body.println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"); 259 260 String expMatch = colorArray[0]; 262 String temp; 263 while (true) { 264 temp = fileReader.readLine(); 266 if (temp == null) { 267 break; 268 } 269 270 lineCounter++; 271 lineOfText = new StringBuffer (temp); 272 273 String charToCheck; 275 for (int i = 0; i < lineOfText.length(); i++) { 276 charToCheck = String.valueOf(lineOfText.charAt(i)); 278 for (int j = 0; j < specialChars.length; j++) { 279 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 for (int i = 0; i < expArray.length; i++) { 290 if (expArray[i].match(temp)) { 291 expMatch = colorArray[i]; 292 break; 293 } 294 } 295 296 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 if ((filterSearch || highlightSearch) && (filterSearch && !matchFound)) { 308 continue; 309 } 310 311 body.println( 312 new StringBuffer (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> </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 (se.getMessage()); 330 } 331 332 fileReader.close(); 334 } 335 336 344 public void writeHtmlTitle(PrintWriter body) throws IOException { 345 body.println(HTML_TITLE); 346 } 347 348 354 public void writePageTitle(PrintWriter body) throws IOException { 355 body.println("System Log Files"); 356 } 357 358 378 public void writeSubMenuItems(PrintWriter body) throws IOException {} 379 380 } 381 | Popular Tags |