001: /*--
002:
003: Copyright (C) 2000-2003 Anthony Eden.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The name "EdenLib" must not be used to endorse or promote products
019: derived from this software without prior written permission. For
020: written permission, please contact me@anthonyeden.com.
021:
022: 4. Products derived from this software may not be called "EdenLib", nor
023: may "EdenLib" appear in their name, without prior written permission
024: from Anthony Eden (me@anthonyeden.com).
025:
026: In addition, I request (but do not require) that you include in the
027: end-user documentation provided with the redistribution and/or in the
028: software itself an acknowledgement equivalent to the following:
029: "This product includes software developed by
030: Anthony Eden (http://www.anthonyeden.com/)."
031:
032: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
036: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
037: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
038: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
039: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
040: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
041: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
042: POSSIBILITY OF SUCH DAMAGE.
043: For more information on EdenLib, please see <http://edenlib.sf.net/>.
044:
045: */
046:
047: package com.anthonyeden.lib.resource;
048:
049: import java.io.InputStream;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: import org.apache.commons.vfs.FileObject;
055: import org.apache.commons.vfs.FileContent;
056: import org.apache.commons.vfs.FileSystemException;
057:
058: import com.anthonyeden.lib.ChainedRuntimeException;
059: import com.anthonyeden.lib.util.IOUtilities;
060:
061: /** Monitors the given File for changes.
062:
063: @author Anthony Eden
064: */
065:
066: public class ResourceVFSMonitor implements Runnable, ResourceMonitor {
067:
068: private static final Log log = LogFactory
069: .getLog(ResourceVFSMonitor.class);
070:
071: private FileObject file;
072: private int delay;
073: private long lastModified;
074: private ResourceRecipient handler;
075:
076: private Thread thread;
077: private boolean running = false;
078:
079: /** Construct a ResourceFileMonitor for the given file.
080:
081: @param file The file
082: @param delay The delay
083: @param handler The handler
084: ?*/
085:
086: public ResourceVFSMonitor(FileObject file, int delay,
087: ResourceRecipient handler) {
088: this .file = file;
089: this .delay = delay;
090: this .handler = handler;
091: }
092:
093: /** Start the monitoring thread. If the thread is already running then
094: this method will not attempt to start a new thread.
095: */
096:
097: public void startMonitor() {
098: if (!running) {
099: log.debug("Starting FileMonitor");
100:
101: running = true;
102: thread = new Thread(this );
103: thread.setDaemon(true);
104: thread.start();
105: }
106: }
107:
108: /** Stop the monitoring thread. If the monitoring thread is not running
109: then this method will do nothing.
110: */
111:
112: public void stopMonitor() {
113: if (thread != null && running) {
114: log.debug("Stopping FileMonitor");
115:
116: running = false;
117: thread.interrupt();
118: }
119: }
120:
121: /** The thread's run method. This method should not be executed directly
122: nor should a thread be manually created, rather the <code>startMonitor()</code>
123: and <code>stopMonitor()</code> methods should be used to start and stop
124: the monitor.
125: */
126:
127: public void run() {
128: try {
129: log.debug("ResourceVFSMonitor start.");
130: FileContent content = file.getContent();
131: lastModified = content.getLastModifiedTime();
132: while (running) {
133: long currentLastModified = content
134: .getLastModifiedTime();
135: if (currentLastModified != lastModified) {
136: log.info("Reloading " + file);
137:
138: InputStream in = null;
139: try {
140: in = content.getInputStream();
141: handler.load(in);
142: } catch (Exception e) {
143: e.printStackTrace();
144: } finally {
145: IOUtilities.close(in);
146: }
147:
148: lastModified = currentLastModified;
149: }
150:
151: try {
152: Thread.sleep(delay);
153: } catch (InterruptedException e) {
154: // do nothing
155: }
156: }
157: log.debug("FileMonitor stopped.");
158: } catch (FileSystemException e) {
159: throw new ChainedRuntimeException(e);
160: }
161: }
162:
163: }
|