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: }
|