Source Code Cross Referenced for FileMonitor.java in  » Portal » Open-Portal » com » sun » portal » log » monitor » 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 » Portal » Open Portal » com.sun.portal.log.monitor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
003:         * PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
004:         */
005:        package com.sun.portal.log.monitor;
006:
007:        import java.util.*;
008:        import java.io.*;
009:        import java.net.*;
010:
011:        /**
012:         * This class is responsible for providing methods for
013:         * File monitoring.
014:         */
015:        public class FileMonitor {
016:
017:            private static final FileMonitor instance = new FileMonitor();
018:            public static final String SRA_COMPONENT_TYPE_PROPERTY = "com.sun.portal.sra.component.type";
019:
020:            private Timer timer;
021:            private Hashtable timerEntries;
022:            private FileMonitorTask task;
023:
024:            public static FileMonitor getInstance() {
025:                return instance;
026:            }
027:
028:            protected FileMonitor() {
029:                // Create timer, run timer thread as daemon.
030:                timer = new Timer(true);
031:                timerEntries = new Hashtable();
032:            }
033:
034:            public void addFileChangeListener(FileChangeListener listener,
035:                    String fileName, long period) throws FileNotFoundException {
036:                String monitoredFileName = getMonitoredConfigFile(fileName);
037:                removeFileChangeListener(listener, monitoredFileName);
038:                task = new FileMonitorTask(listener, fileName);
039:                timerEntries.put(monitoredFileName + listener.hashCode(), task);
040:                timer.schedule(task, period, period);
041:            }
042:
043:            /** Remove the listener from the notification list.
044:             * @param listener the listener to be removed.
045:             */
046:            public void removeFileChangeListener(FileChangeListener listener,
047:                    String fileName) {
048:                FileMonitorTask task = (FileMonitorTask) timerEntries
049:                        .remove(fileName + listener.hashCode());
050:                if (task != null) {
051:                    task.cancel();
052:                }
053:            }
054:
055:            protected void fireFileChangeEvent(FileChangeListener listener,
056:                    String fileName) {
057:                listener.fileChanged(fileName);
058:            }
059:
060:            class FileMonitorTask extends TimerTask {
061:                FileChangeListener listener;
062:                String fileName;
063:                File monitoredFile;
064:                long lastModified;
065:
066:                public FileMonitorTask(FileChangeListener listener,
067:                        String fileName) {
068:                    this .listener = listener;
069:                    this .fileName = fileName;
070:                    this .lastModified = 0;
071:                    String monitoredFileName = getMonitoredConfigFile(fileName);
072:                    // Create the file to be monitored
073:                    FileOutputStream monitoredFileStream = null;
074:                    try {
075:                        monitoredFileStream = new FileOutputStream(
076:                                monitoredFileName);
077:                    } catch (Exception e) {
078:                        // no action
079:                    } finally {
080:                        try {
081:                            if (monitoredFileStream != null)
082:                                monitoredFileStream.close();
083:                        } catch (Exception e) {
084:                            //drop-through
085:                        }
086:                    }
087:
088:                    monitoredFile = new File(monitoredFileName);
089:                    if (!monitoredFile.exists()) { // but is it on CLASSPATH?
090:                        URL fileURL = listener.getClass().getClassLoader()
091:                                .getResource(monitoredFileName);
092:                        if (fileURL != null) {
093:                            monitoredFile = new File(fileURL.getFile());
094:                        } else {
095:                            //even if the file does not exist at the moment, it may be created
096:                            //later, so continue with scheduling
097:                        }
098:                    }
099:                    this .lastModified = monitoredFile.lastModified();
100:                }
101:
102:                public void run() {
103:                    long lastModified = monitoredFile.lastModified();
104:                    if (lastModified != this .lastModified) {
105:                        this .lastModified = lastModified;
106:                        fireFileChangeEvent(this .listener, this .fileName);
107:                    }
108:                }
109:            }
110:
111:            /**
112:             * Returns the monitored config file name. 
113:             * The monitored config file name is same as the config file name with
114:             * "." prepended. 
115:             * In case of sra components, the sra component type is appended.
116:             */
117:            public static String getMonitoredConfigFile(String logConfigFileName) {
118:                int index = logConfigFileName.lastIndexOf("/") + 1;
119:                StringBuffer buffer = new StringBuffer();
120:                buffer.append(logConfigFileName.substring(0, index));
121:                buffer.append(".");
122:                buffer.append(logConfigFileName.substring(index));
123:                String sraComponentType = System
124:                        .getProperty(SRA_COMPONENT_TYPE_PROPERTY);
125:                if (sraComponentType != null) {
126:                    buffer.append(".");
127:                    buffer.append(sraComponentType);
128:                }
129:                return buffer.toString();
130:            }
131:
132:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.