1 23 24 package com.sun.enterprise.util; 25 26 import java.io.*; 27 import java.util.ArrayList ; 28 import java.util.StringTokenizer ; 29 30 65 66 70 class ThreadedReader extends Thread 71 { 72 BufferedReader _reader = null; 73 String _messageKey = null; 74 ArrayList _result = null; 75 76 public ThreadedReader(InputStream is, ArrayList result, String messageKey) { 77 _reader = new BufferedReader(new InputStreamReader(is)); 78 _result = result; 79 _messageKey = messageKey; 80 } 81 82 public void run() { 83 try { 84 String line = null; 85 while (true) { 86 line = _reader.readLine(); 87 if (line == null) { 88 break; 89 } 90 _result.add(line); 91 } 92 } catch (Exception ex) { 93 System.err.println("ThreadedReader " + _messageKey + " exception"); 94 ex.printStackTrace(); 95 } 96 } 97 } 98 99 100 public class BuildLogMsgDoc { 101 102 public static void main(String [] args) { 103 if (args.length != 1) { 104 System.err.println("usage: BuildLogMsgDoc <resource-file-name>"); 105 System.exit(1); 106 } 107 String inFile = args[0]; 108 String outFile = args[0] + ".html"; 109 try { 110 createHtml(inFile, outFile); 111 } catch (Exception ex) { 112 ex.printStackTrace(); 113 } 114 } 115 116 protected static void createHtml(String in, String out) 117 throws FileNotFoundException, IOException 118 { 119 BufferedReader reader = new BufferedReader(new FileReader(in)); 120 BufferedWriter writer = new BufferedWriter(new FileWriter(out, false)); 121 try { 122 writer.write(tableHeader(in)); 124 writer.newLine(); 125 writer.write(tableRow("Message Id", "Severity", "Message Text", "Description")); 126 writer.newLine(); 127 String line = null; 128 String newLine = null; 129 while (true) { 130 line = reader.readLine(); 131 if (line == null) { 132 break; 133 } 134 parsePropertyEntry(writer, line); 135 } 136 } finally { 137 writer.write(tableFooter()); 139 try { 140 reader.close(); 141 } catch (Exception ex) {} 142 try { 143 writer.close(); 144 } catch (Exception ex) {} 145 } 146 } 147 148 protected static void parsePropertyEntry(BufferedWriter writer, String line) 149 { 150 String result = line.trim(); 151 if (result.length() == 0) { 153 return; 154 } 155 if (result.startsWith("#")) { 157 return; 158 } 159 String key = null; 161 String messageId = null; 162 String message = null; 163 int pos = -2; 164 int pos2 = -2; 165 int pos3 = -2; 166 pos = result.indexOf("="); 167 if (pos > 0) { 168 key = result.substring(0, pos); 173 result = result.substring(pos + 1); 174 pos2 = result.indexOf(" "); 175 pos3 = result.indexOf(":"); 176 if ((pos3 < 0 || pos2 < pos3) && pos2 > 0) { 177 messageId = result.substring(0, pos2); 178 message = result.substring(pos2 + 1).trim(); 179 formatLine(writer, key, messageId, message); 180 return; 181 182 } else if (pos3 > 0) { 183 messageId = result.substring(0, pos3); 184 message = result.substring(pos3 + 1).trim(); 185 formatLine(writer, key, messageId, message); 186 return; 187 } 188 } 189 System.err.println("Failed to parse: " + line + " pos=" + pos + 191 " pos2=" + pos2 + " pos3=" + pos3); 192 } 193 194 protected static void formatLine(BufferedWriter writer, String key, 195 String messageId, String message) 196 { 197 ArrayList files = findSourceFiles (key); 199 if (files == null) { 200 try { 202 System.err.println("message " + key + "is not found in any files"); 203 writer.write(tableRow(messageId, "UNKNOWN", message, 204 "key=" + key + " found in NO FILES")); 205 writer.newLine(); 206 } catch (Exception ex) { 207 System.err.println("formatLine id " + 208 key + " exception "); 209 ex.printStackTrace(); 210 } 211 } else { 212 for (int i = 0; i < files.size(); i++) { 216 findSourceOccurrence(writer, key, messageId, message, 218 (String )files.get(i)); 219 } 220 } 221 } 222 223 protected static String findLevel (String line) { 226 String [] keywords = {"INFO", "WARNING", "SEVERE", "CONFIG", "ALERT", "FATAL", 227 "FINE", "FINER", "FINEST"}; 228 line = line.toUpperCase(); 229 for (int i = 0; i < keywords.length; i++) { 230 int pos = line.indexOf(keywords[i]); 231 if (pos >= 0) { 232 return (keywords[i]); 233 } 234 } 235 return null; 236 } 237 238 protected static void findSourceOccurrence(BufferedWriter writer, 242 String messageKey, String messageId, String message, 243 String in) 244 { 245 BufferedReader reader = null; 246 try { 247 reader = new BufferedReader(new FileReader(in)); 248 int lineno = 1; 249 int foundOn = -1; 250 String line = null; 251 while (true) { 254 line = reader.readLine(); 255 if (line == null) { 256 if (foundOn > 0) { 259 System.err.println("found " + messageKey + " but no level in " + 260 in); 261 writer.write(tableRow(messageId, "UNKNOWN", message, 262 "key=" + messageKey + " found in " + in + 263 " line " + foundOn)); 264 writer.newLine(); 265 } 266 break; 267 } 268 int pos = line.indexOf(messageKey); 269 if (pos >= 0) { 270 if (foundOn > 0) { 273 System.err.println("found next " + messageKey + 276 " before finding previous level in " + in); 277 writer.write(tableRow(messageId, "UNKNOWN", message, 278 "key=" + messageKey + " found in " + in + 279 " line " + foundOn)); 280 writer.newLine(); 281 } 282 foundOn = lineno; 283 } 286 if (foundOn >= lineno) { 287 String level = findLevel(line); 290 if (level != null) { 291 if (foundOn < lineno + 5) { 298 writer.write(tableRow(messageId, level, message, 299 "key=" + messageKey + " found in " + in + 300 " line " + foundOn + " logged in line " + lineno)); 301 } else { 302 writer.write(tableRow(messageId, "**" + level, message, 303 "key=" + messageKey + " found in " + in + 304 " line " + foundOn + " logged in line " + lineno)); 305 } 306 writer.newLine(); 307 foundOn = -1; 308 } 309 } 310 lineno++; 311 } 312 } catch (Exception ex) { 313 System.err.println("findSourceOccurrence id " + 314 messageKey + " in " + in + " exception"); 315 ex.printStackTrace(); 316 } finally { 317 try { 318 if (reader != null) { 319 reader.close(); 320 } 321 } catch (Exception ex) {} 322 } 323 } 324 325 protected static ArrayList findSourceFiles (String messageKey) 326 { 327 try { 328 ArrayList stdoutList = new ArrayList (); 329 ArrayList stderrList = new ArrayList (); 330 Process p = Runtime.getRuntime().exec("/bin/find /ias/tip/iplanet/ias/server/src/java -name \"*.java\" -exec grep -l " + messageKey + " {} ;"); 334 ThreadedReader inReader = new ThreadedReader(p.getInputStream(), stdoutList, messageKey); 335 ThreadedReader errReader = new ThreadedReader(p.getErrorStream(), stderrList, messageKey); 336 inReader.start(); 337 errReader.start(); 338 inReader.join(); 339 errReader.join(); 340 p.waitFor(); 341 p.exitValue(); 342 if (!stderrList.isEmpty()) { 343 System.err.println("findSourceFiles " + messageKey + " stderr"); 344 for (int i = 0; i < stderrList.size(); i++) { 345 System.err.println(" " + (String )stderrList.get(i)); 346 } 347 } 348 if (!stdoutList.isEmpty()) { 349 return stdoutList; 350 } 351 return null; 352 } catch (Exception ex) { 353 System.err.println("findSourceFiles " + messageKey + " exception"); 354 ex.printStackTrace(); 355 return null; 356 } 357 } 358 359 protected static String tableHeader(String inFile) { 361 String r = "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">"; 362 r += "\n" + "<html>"; 363 r += "\n" + "<head>"; 364 r += "\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"; 365 r += "\n" + "<meta name=\"Author\" content=\"Ken Ebbs\">"; 366 r += "\n" + "<meta name=\"GENERATOR\" content=\"Mozilla/4.79 [en] (Windows NT 5.0; U) [Netscape]\">"; 367 r += "\n" + "<title>Logging Message Reference</title>"; 368 r += "\n" + "</head>"; 369 r += "\n" + "<body>"; 370 r += "\n" + " "; 371 r += "\n" + "<table BORDER WIDTH=\"100%\" >"; 372 r += "\n" + "<caption>Log Messages For " + inFile + "</caption>"; 373 return r; 374 } 375 376 protected static String tableFooter() { 378 String r = "</table>"; 379 r += "\n" + "</body>"; 380 r += "\n" + "</html>"; 381 return r; 382 } 383 384 protected static String tableRow(String messageId, String severity, String messageText, String description) { 386 String r = "<tr>"; 387 r += "\n" + "<td>" + messageId + "</td>"; 388 r += "\n" + "<td>" + severity + "</td>"; 389 r += "\n" + "<td>" + messageText + "</td>"; 390 r += "\n" + "<td>" + description + "</td>"; 391 r += "\n" + "</tr>"; 392 return r; 393 } 394 } 395 | Popular Tags |