1 31 32 package org.opencms.util; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.main.CmsIllegalArgumentException; 36 import org.opencms.main.CmsLog; 37 import org.opencms.main.CmsRuntimeException; 38 import org.opencms.main.OpenCms; 39 40 import java.io.BufferedReader ; 41 import java.io.ByteArrayOutputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.FileNotFoundException ; 45 import java.io.IOException ; 46 import java.io.InputStreamReader ; 47 import java.io.LineNumberReader ; 48 import java.io.OutputStreamWriter ; 49 import java.nio.charset.Charset ; 50 import java.nio.charset.IllegalCharsetNameException ; 51 import java.nio.charset.UnsupportedCharsetException ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import java.util.Stack ; 55 56 import org.apache.commons.logging.Log; 57 58 73 public class CmsRfsFileViewer implements Cloneable { 74 75 76 protected static final Log LOG = CmsLog.getLog(CmsRfsFileViewer.class); 77 78 79 private boolean m_enabled; 80 81 82 private Charset m_fileEncoding; 83 84 85 protected Map m_fileName2lineIndex; 86 87 88 protected String m_filePath; 89 90 97 private boolean m_frozen; 98 99 103 private boolean m_isLogfile; 104 105 106 protected int m_windowPos; 107 108 109 protected int m_windowSize; 110 111 118 public CmsRfsFileViewer() { 119 120 m_isLogfile = true; 121 m_fileName2lineIndex = new HashMap (); 122 m_fileEncoding = Charset.forName(new OutputStreamWriter (new ByteArrayOutputStream ()).getEncoding()); 124 m_enabled = true; 125 m_windowSize = 200; 126 127 } 128 129 136 private void checkFrozen() throws CmsRuntimeException { 137 138 if (m_frozen) { 139 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_FILE_VIEW_SETTINGS_FROZEN_0)); 140 } 141 } 142 143 153 public Object clone() { 154 155 if (m_filePath == null) { 157 if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 159 m_filePath = OpenCms.getSystemInfo().getLogFileRfsPath(); 160 } 161 } 162 CmsRfsFileViewer clone = new CmsRfsFileViewer(); 163 try { 164 clone.setFilePath(m_filePath); 166 } catch (CmsRfsException e) { 167 } catch (CmsRuntimeException e) { 169 } 171 clone.m_fileEncoding = m_fileEncoding; 172 clone.m_isLogfile = m_isLogfile; 173 clone.m_enabled = m_enabled; 174 clone.setWindowSize(m_windowSize); 176 clone.m_fileName2lineIndex = m_fileName2lineIndex; 177 clone.m_frozen = false; 179 return clone; 180 } 181 182 194 public String getFileEncoding() { 195 196 return m_fileEncoding.name(); 197 } 198 199 204 public String getFilePath() { 205 206 return m_filePath; 207 } 208 209 214 public boolean getIsLogfile() { 215 216 return m_isLogfile; 218 } 219 220 229 public int getWindowPos() { 230 231 return m_windowPos; 232 } 233 234 240 public int getWindowSize() { 241 242 return m_windowSize; 243 } 244 245 253 public boolean isEnabled() { 254 255 return m_enabled; 256 } 257 258 266 public String readFilePortion() throws CmsRfsException { 267 268 if (m_enabled) { 269 int lines = -1; 272 int startLine; 273 if (m_isLogfile) { 274 lines = scrollToFileEnd(); 275 startLine = lines - m_windowSize; 280 } else { 281 m_windowPos = 0; 282 startLine = m_windowPos * m_windowSize; 283 } 284 LineNumberReader reader = null; 285 try { 286 reader = new LineNumberReader (new BufferedReader (new InputStreamReader ( 289 new FileInputStream (m_filePath), 290 m_fileEncoding)), (int)m_windowSize * 200); 291 int currentLine = 0; 292 while (startLine > currentLine) { 294 reader.readLine(); 295 currentLine++; 296 } 297 StringBuffer result = new StringBuffer (); 298 String read = reader.readLine(); 299 300 if (m_isLogfile) { 303 304 Stack inverter = new Stack (); 306 for (int i = m_windowSize; i > 0 && read != null; i--) { 307 inverter.push(read); 308 read = reader.readLine(); 309 } 310 while (!inverter.isEmpty()) { 312 result.append(inverter.pop()); 313 result.append('\n'); 314 } 315 316 } else { 317 318 for (int i = m_windowSize; i > 0 && read != null; i--) { 319 result.append(read); 320 result.append('\n'); 321 read = reader.readLine(); 322 } 323 } 324 return CmsEncoder.escapeXml(result.toString()); 325 } catch (IOException ioex) { 326 CmsRfsException ex = new CmsRfsException(Messages.get().container( 327 Messages.ERR_FILE_ARG_ACCESS_1, 328 m_filePath), ioex); 329 throw ex; 330 } finally { 331 if (reader != null) { 332 try { 333 reader.close(); 334 } catch (IOException e) { 335 LOG.error(e); 336 } 337 338 } 339 } 340 } else { 341 return Messages.get().getBundle().key(Messages.GUI_FILE_VIEW_NO_PREVIEW_0); 342 } 343 344 } 345 346 358 private int scrollToFileEnd() { 359 360 int lines = 0; 361 if (OpenCms.getRunLevel() < OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 362 } else { 364 LineNumberReader reader = null; 365 try { 368 reader = new LineNumberReader ( 369 new BufferedReader (new InputStreamReader (new FileInputStream (m_filePath)))); 370 while (reader.readLine() != null) { 371 lines++; 372 } 373 reader.close(); 374 int availWindows = (int)Math.ceil(lines / m_windowSize); 376 m_windowPos = availWindows - 1; 378 } catch (IOException ioex) { 379 LOG.error("Unable to scroll file " + m_filePath + " to end. Ensure that it exists. "); 380 } finally { 381 if (reader != null) { 382 try { 383 reader.close(); 384 } catch (Throwable f) { 385 LOG.info("Unable to close reader of file " + m_filePath, f); 386 } 387 } 388 } 389 } 390 return lines; 391 } 392 393 400 public void setEnabled(boolean preview) { 401 402 m_enabled = preview; 403 } 404 405 419 public void setFileEncoding(String fileEncoding) { 420 421 checkFrozen(); 422 try { 423 m_fileEncoding = Charset.forName(fileEncoding); 424 } catch (IllegalCharsetNameException icne) { 425 throw new CmsIllegalArgumentException(Messages.get().container( 426 Messages.ERR_CHARSET_ILLEGAL_NAME_1, 427 fileEncoding)); 428 } catch (UnsupportedCharsetException ucse) { 429 throw new CmsIllegalArgumentException(Messages.get().container( 430 Messages.ERR_CHARSET_UNSUPPORTED_1, 431 fileEncoding)); 432 433 } 434 435 } 436 437 450 public void setFilePath(String path) throws CmsRfsException, CmsRuntimeException { 451 452 checkFrozen(); 453 454 if (path != null) { 455 path = path.trim(); 457 } 458 if (CmsStringUtil.isEmpty(path)) { 459 throw new CmsRfsException(Messages.get().container( 460 Messages.ERR_FILE_ARG_EMPTY_1, 461 new Object [] {String.valueOf(path)})); 462 } 463 try { 464 File file = new File (path); 466 if (file.isDirectory()) { 467 if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) { 469 m_filePath = null; 471 m_isLogfile = true; 472 } else { 473 throw new CmsRfsException(Messages.get().container( 474 Messages.ERR_FILE_ARG_IS_FOLDER_1, 475 new Object [] {String.valueOf(path)})); 476 } 477 } else if (!file.isFile()) { 478 if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) { 480 m_filePath = null; 482 m_isLogfile = true; 483 } else { 484 throw new CmsRfsException(Messages.get().container( 485 Messages.ERR_FILE_ARG_NOT_FOUND_1, 486 new Object [] {String.valueOf(path)})); 487 } 488 489 } else if (!file.canRead()) { 490 if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) { 492 m_filePath = null; 494 m_isLogfile = true; 495 } else { 496 throw new CmsRfsException(Messages.get().container( 497 Messages.ERR_FILE_ARG_NOT_READ_1, 498 new Object [] {String.valueOf(path)})); 499 } 500 } else { 501 m_filePath = file.getCanonicalPath(); 502 } 503 } catch (FileNotFoundException fnfe) { 504 if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) { 506 m_filePath = null; 508 m_isLogfile = true; 509 } else { 510 511 throw new CmsRfsException(Messages.get().container( 512 Messages.ERR_FILE_ARG_NOT_FOUND_1, 513 new Object [] {String.valueOf(path)}), fnfe); 514 } 515 516 } catch (IOException ioex) { 517 if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) { 519 m_filePath = null; 521 m_isLogfile = true; 522 } else { 523 524 throw new CmsRfsException(Messages.get().container( 525 Messages.ERR_FILE_ARG_ACCESS_1, 526 new Object [] {String.valueOf(path)}), ioex); 527 } 528 529 } 530 } 531 532 543 public void setFrozen(boolean frozen) throws CmsRuntimeException { 544 545 m_frozen = frozen; 546 } 547 548 565 public void setIsLogfile(boolean isLogfile) throws CmsRuntimeException { 566 567 checkFrozen(); 568 m_isLogfile = isLogfile; 569 } 570 571 582 public void setWindowPos(int windowPos) throws CmsRuntimeException { 583 584 checkFrozen(); 585 m_windowPos = windowPos; 586 } 587 588 597 public void setWindowSize(int windowSize) throws CmsRuntimeException { 598 599 checkFrozen(); 600 m_windowSize = windowSize; 601 } 602 } | Popular Tags |