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:
044: For more information on EdenLib, please see <http://edenlib.sf.net/>.
045:
046: */
047:
048: package com.anthonyeden.lib.resource;
049:
050: import java.io.FileNotFoundException;
051: import java.io.InputStream;
052:
053: import org.apache.commons.vfs.VFS;
054: import org.apache.commons.vfs.FileObject;
055: import org.apache.commons.vfs.FileContent;
056: import org.apache.commons.vfs.FileSystemManager;
057: import org.apache.commons.vfs.FileSystemException;
058:
059: import org.apache.commons.logging.Log;
060: import org.apache.commons.logging.LogFactory;
061:
062: import com.anthonyeden.lib.util.IOUtilities;
063:
064: /** Implementation of the ResourceLoader interface which loads data from
065: a virtual file system.
066:
067: @author Anthony Eden
068: */
069:
070: public class VFSResourceLoader extends AbstractResourceLoader {
071:
072: private static final Log log = LogFactory
073: .getLog(VFSResourceLoader.class);
074:
075: private FileSystemManager fileSystemManager = null;
076:
077: /** Get the FileSystemManager.
078:
079: @return The FileSystemManager
080: @throws FileSystemException
081: */
082:
083: public synchronized FileSystemManager getFileSystemManager()
084: throws FileSystemException {
085: if (fileSystemManager == null) {
086: fileSystemManager = VFS.getManager();
087: }
088: return fileSystemManager;
089: }
090:
091: /** Set the FileSystemManager.
092:
093: @param fileSystemManager The new FileSystemManager
094: */
095:
096: public void setFileSystemManager(FileSystemManager fileSystemManager) {
097: this .fileSystemManager = fileSystemManager;
098: }
099:
100: /** Load the resource specified by the given path. Calling this method will
101: cause the resource to be loaded and monitored.
102:
103: @param path The path
104: @param handler The ResourceReceipient callback
105: @throws ResourceException
106: */
107:
108: public void loadResource(String path, ResourceRecipient handler)
109: throws ResourceException {
110: loadResource(path, handler, true);
111: }
112:
113: /** Load the resource specified by the given path. If monitor is true then
114: the ResourceLoader implementation will monitor the resource and call the
115: ResourceRecipient each time the resource is modified.
116:
117: @param path The path
118: @param handler The ResourceRecipient callback
119: @param monitor True to monitor the resource
120: @throws ResourceException
121: */
122:
123: public void loadResource(String path, ResourceRecipient handler,
124: boolean monitor) throws ResourceException {
125: InputStream in = null;
126: try {
127: FileSystemManager fsManager = getFileSystemManager();
128: log.debug("Resource path: " + path);
129: FileObject file = fsManager.resolveFile(path);
130: if (!file.exists()) {
131: throw new FileNotFoundException("File not found: "
132: + file);
133: }
134:
135: FileContent content = file.getContent();
136: in = content.getInputStream();
137: handler.load(in);
138:
139: if (monitor) {
140: ResourceVFSMonitor resourceMonitor = new ResourceVFSMonitor(
141: file, getDelay(), handler);
142: getMonitors().add(resourceMonitor);
143: resourceMonitor.startMonitor();
144: }
145: } catch (Exception e) {
146: e.printStackTrace();
147: throw new ResourceException(e);
148: } finally {
149: IOUtilities.close(in);
150: }
151: }
152:
153: }
|