Source Code Cross Referenced for FileListener.java in  » Development » Tracelog » net » sourceforge » tracelog » listeners » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Tracelog » net.sourceforge.tracelog.listeners 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sourceforge.tracelog.listeners;
002:
003:        import java.io.BufferedReader;
004:        import java.io.ByteArrayInputStream;
005:        import java.io.File;
006:        import java.io.FileNotFoundException;
007:        import java.io.InputStreamReader;
008:        import java.io.RandomAccessFile;
009:
010:        import org.apache.log4j.Logger;
011:
012:        public class FileListener implements  Runnable {
013:            private static Logger log = Logger.getLogger(FileListener.class);
014:            private long filePointerOffset;
015:            private File logFile;
016:
017:            private String logPath;
018:            private LogViewerHandler logViewerHandler;
019:            private long previousFileSize;
020:
021:            public FileListener(LogViewerHandler logViewerHandler,
022:                    String logPath) {
023:                this .logPath = logPath;
024:                this .logViewerHandler = logViewerHandler;
025:
026:                this .logFile = new File(logPath);
027:                this .previousFileSize = this .logFile.length();
028:            }
029:
030:            /**
031:             * Polls the log file size to check if new content is written to the file.
032:             * If the file size has changed, then new log messages are read from the
033:             * file and written in the log viewer.
034:             */
035:            public void run() {
036:                try {
037:                    // the first read basically fastforward the file pointer offset to
038:                    // the very end of the file without displaying any old contents in
039:                    // the log viewer yet
040:                    readLogFile();
041:
042:                    // prepares to write incoming log contents
043:                    logViewerHandler.write("Listening on " + logPath + "...");
044:
045:                    while (!Thread.interrupted()) {
046:                        // need to check if the file exists because the file could be
047:                        // renamed during some backup process. If the file goes missing
048:                        // in the middle of this running application, then sleep it for
049:                        // 5 seconds then try it again
050:                        if (!logFile.exists()) {
051:                            Thread.sleep(5000);
052:                            continue;
053:                        }
054:                        // as long as the log file size changes, then we know the file
055:                        // content changes.
056:                        else if (previousFileSize != logFile.length()) {
057:                            previousFileSize = logFile.length();
058:                            byte[] buf = readLogFile();
059:                            writeLogMessages(buf);
060:                        }
061:
062:                        Thread.sleep(1000);
063:                    }
064:                } catch (InterruptedException ignored) {
065:                    logViewerHandler.write("Stopping log... done.");
066:                } catch (Exception e) {
067:                    log.error("Unexpected error has occured: " + logPath);
068:                    log.error("Error message: " + e);
069:                    e.printStackTrace();
070:                }
071:            }
072:
073:            /**
074:             * Reads a chunk of log messages from the log file.
075:             * 
076:             * @return Log messages.
077:             * @throws Exception
078:             */
079:            private byte[] readLogFile() throws Exception {
080:                RandomAccessFile raf = null;
081:                byte[] buf = null;
082:
083:                try {
084:                    raf = new RandomAccessFile(logFile, "r");
085:
086:                    long currentFileLen = raf.length();
087:
088:                    // this happen when new lines are appended into the existing log
089:                    // file, then continue reading from the last time it was stopped
090:                    if (filePointerOffset < currentFileLen) {
091:                        buf = new byte[(int) (currentFileLen - filePointerOffset)];
092:                        raf.seek(filePointerOffset);
093:                    }
094:                    // this happen when the content in the log file is deleted or the
095:                    // log file is re-created, then read everything from the file
096:                    else {
097:                        buf = new byte[(int) currentFileLen];
098:                    }
099:
100:                    raf.read(buf);
101:
102:                    filePointerOffset = currentFileLen;
103:                } catch (FileNotFoundException e) {
104:                    logViewerHandler.write("ERROR: Log file not found: "
105:                            + logPath);
106:                    logViewerHandler
107:                            .write("ERROR: Please make sure this log path is correct and you have enough privilege to access this log path.");
108:                } catch (Exception e) {
109:                    log.error("Error reading log file: " + logPath);
110:                    log.error("Error message: " + e);
111:                    e.printStackTrace();
112:                } finally {
113:                    try {
114:                        raf.close();
115:                    } catch (Exception ignored) {
116:                    }
117:                }
118:
119:                return buf;
120:            }
121:
122:            /**
123:             * Separate out each line from the byte buffer and write each of them to the
124:             * log viewer.
125:             * 
126:             * @param buf
127:             *            Log messages to be written.
128:             * @throws Exception
129:             */
130:            private void writeLogMessages(byte[] buf) throws Exception {
131:                if (buf != null) {
132:                    BufferedReader br = null;
133:
134:                    try {
135:                        br = new BufferedReader(new InputStreamReader(
136:                                new ByteArrayInputStream(buf)));
137:                        String line = "";
138:
139:                        // separate out each line
140:                        while ((line = br.readLine()) != null) {
141:                            logViewerHandler.write(line);
142:                        }
143:                    } catch (Exception e) {
144:                        log.error("Error writing log messages: " + logPath);
145:                        log.error("Error message: " + e);
146:                        e.printStackTrace();
147:                    } finally {
148:                        try {
149:                            br.close();
150:                        } catch (Exception ignored) {
151:                        }
152:                    }
153:                }
154:            }
155:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.