1 19 20 package org.netbeans.modules.j2ee.sun.ide.j2ee; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.WeakHashMap ; 30 import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport; 31 import org.openide.ErrorManager; 32 import org.openide.windows.InputOutput; 33 34 35 36 37 public final class LogViewerSupport extends Thread { 38 39 40 private InputOutput io; 41 42 private String url; 43 private static final HashMap logsMap = new HashMap (); 44 49 private long sampleInterval = 10000; 50 51 54 private File logfile; 55 56 60 private boolean startAtBeginning = false; 61 62 65 private boolean working = false; 66 private LogHyperLinkSupport.AppServerLogSupport logSupport; 67 private BufferedReader reader = null; 68 69 private boolean initRingerDone=false; 70 72 static private int OLD_LINES = 600; 73 static private int MAX_LINES = 25000; 74 static private int LINES = 2000; 75 76 85 86 public static LogViewerSupport getLogViewerSupport(final File file, final String url, final long sampleInterval, final boolean startAtBeginning ) { 87 synchronized (logsMap) { 88 LogViewerSupport logViewer = (LogViewerSupport)logsMap.get(url); 89 if (logViewer==null) { 90 logViewer = new LogViewerSupport( file, url, sampleInterval, startAtBeginning ); 91 logsMap.put(url,logViewer); 92 } 93 94 logViewer.sampleInterval = sampleInterval; 95 logViewer.startAtBeginning = startAtBeginning; 96 return logViewer; 97 } 98 } 99 100 106 public static void removeLogViewerSupport(final String url) { 107 synchronized (logsMap) { 108 LogViewerSupport logViewer = (LogViewerSupport)logsMap.get(url); 109 if (logViewer!=null) { 110 logsMap.remove(url); 111 logViewer.working =false; 112 } 113 } 114 } 115 116 private LogViewerSupport(final File file, final String url, final long sampleInterval, final boolean startAtBeginning) { 117 this.logfile = file; 118 this.url = url; 119 this.sampleInterval = sampleInterval; 120 this.startAtBeginning = startAtBeginning; 121 io = UISupport.getServerIO(url); 122 123 logSupport = new LogHyperLinkSupport.AppServerLogSupport("", "/"); 124 setDaemon(true); 126 start(); 127 } 128 129 130 131 protected void printLine( String line ) { 132 String s = filterLine(line); 133 if ((!s.equals(""))&&((!s.equals(" ")))){ 135 LogHyperLinkSupport.AppServerLogSupport.LineInfo lineInfo = logSupport.analyzeLine(s); 136 if (lineInfo.isError()) { 137 if (lineInfo.isAccessible()) { 138 try { 139 io.getOut().println(s, logSupport.getLink(lineInfo.message(), lineInfo.path(), lineInfo.line())); 140 } catch (IOException ex) { 141 ErrorManager.getDefault().notify(ex); 142 } 143 } else { 144 io.getOut().println(s); 145 } 146 } else { 147 io.getOut().println(s); 148 } 149 150 } 151 152 } 153 154 public void stopTailing() { 155 this.working = false; 156 } 157 158 159 private void initRingBuffer(Ring ring){ 160 161 try { 162 if (reader!=null){ 164 try { 165 reader.close(); 166 } catch (IOException ex) { 167 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 168 } 169 reader = null; 170 } 171 172 if(logfile.exists()) { 173 reader = new BufferedReader (new FileReader (logfile)); 174 int c; 175 String line; 176 177 try { 180 while ((line = reader.readLine()) != null) { 181 ring.add(line); 182 } 183 } catch (IOException e) { 184 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 185 } 186 if (startAtBeginning){ 188 ring.output(); 189 190 } 191 ring.setMaxCount(LINES); 192 } 193 } catch (Exception e){ 194 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 195 } 196 197 } 198 199 public void run() { 200 long currentIndex = 0; 202 long currentNbofFileLogs = logfile.getParentFile().list().length; 203 boolean needTosleep=true; 204 boolean needToRotate=false; 205 boolean alreadyRotated=false; 206 working = true; 207 String line; 208 int lines =0; 209 210 Ring ring = new Ring(OLD_LINES); 211 currentIndex = logfile.length(); 212 try{ 213 while( working ) { 214 needTosleep=true; 215 try { 216 if (initRingerDone==false){ 217 initRingBuffer(ring); 219 currentIndex = logfile.length(); 220 initRingerDone=true; 221 } 222 if (lines >= MAX_LINES) { 223 io.getOut().reset(); 224 lines = ring.output(); 225 } 226 long fileLength = logfile.length(); 228 long newNbofFileLogs = logfile.getParentFile().list().length; 229 230 if( fileLength < currentIndex ) { 231 needToRotate =true; 232 } 233 234 if( currentNbofFileLogs < newNbofFileLogs ) { 235 needToRotate =true; 236 currentNbofFileLogs = newNbofFileLogs; 237 } 238 239 if (needToRotate ){ 240 243 try { 245 line = (reader != null) ? reader.readLine() : null; 246 if(line != null) { 247 alreadyRotated = false; 248 } 249 while( line != null ) { 250 printLine( line ); 251 ring.add(line); 252 253 line = reader.readLine(); 254 lines++; 255 } 256 } catch (IOException ex) { 257 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 258 needToRotate =true; 259 } 260 261 if(!alreadyRotated) { 263 printLine("----Log File Rotated---"); 265 alreadyRotated = true; 266 } 267 268 try { 269 if(reader != null) { 270 reader.close(); 271 } 272 } catch (IOException ex) { 273 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 274 } 275 276 if(logfile.exists()) { 277 reader = new BufferedReader (new FileReader (logfile)); 278 currentIndex = 0; 279 needToRotate = false; 280 } else { 281 reader = null; 282 } 283 } 284 285 286 try { 287 line = (reader != null) ? reader.readLine() : null; 288 if(line != null) { 289 alreadyRotated = false; 290 } 291 while( line != null ) { 292 printLine( line ); 293 ring.add(line); 294 line = reader.readLine(); 295 lines++; 296 } 297 } catch (IOException ex) { 298 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 299 needTosleep=false; needToRotate =true; 301 } 302 currentIndex = logfile.length(); 304 currentNbofFileLogs = logfile.getParentFile().list().length; 305 306 307 } catch(Exception ex) { 308 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 309 working = true; } finally{ 311 try { 312 synchronized(this) { 313 if (needTosleep){ 314 wait(100); 315 } 316 } 317 } catch(InterruptedException ex) { 318 } 320 } 321 } 322 } catch( Exception e ) { 323 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 324 working =false; } finally{ 326 if (reader!=null) { 328 try { 329 reader.close(); 330 } catch (IOException ex) { 331 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 332 } 333 } 334 } 335 } 336 337 342 private String filterLine(String line){ 343 if (!line.startsWith("[#")){ if (line.endsWith("|#]")){ line = line.substring(0,line.length()-3); } 347 return line; 348 } 349 350 String s[] = line.split("\\|"); if (s==null){ 352 return line; 353 } 354 355 356 if (s.length<=6){ 357 return ""; } 359 360 return s[6]; 361 } 362 363 369 public InputOutput showLogViewer(boolean forced) throws IOException { 370 io = UISupport.getServerIO(url); 371 372 373 375 working = true; 376 if (forced &&(io.isClosed())){ 377 initRingerDone=false; 378 io.getOut().reset(); 379 } 380 381 io.select(); 382 383 return io; 384 385 } 386 387 private class Ring { 388 private int maxCount; 389 private int count; 390 private LinkedList anchor; 391 392 public Ring(int max) { 393 maxCount = max; 394 count = 0; 395 anchor = new LinkedList (); 396 } 397 398 public String add(String line) { 399 if (line == null || line.equals("")) { return null; 401 } 402 403 while (count >= maxCount) { 404 anchor.removeFirst(); 405 count--; 406 } 407 408 anchor.addLast(line); 409 count++; 410 411 return line; 412 } 413 414 public void setMaxCount(int newMax) { 415 maxCount = newMax; 416 } 417 418 public int output() { 419 int i = 0; 420 Iterator it = anchor.iterator(); 421 422 while (it.hasNext()) { 423 printLine((String )it.next()); 424 i++; 425 } 426 return i; 427 } 428 429 public void reset() { 430 anchor = new LinkedList (); 431 } 432 } 433 434 } 435 | Popular Tags |