KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > resource > DefaultResourceManager


1 /*--
2  Copyright (C) 2001-2003 Aetrion LLC.
3  All rights reserved.
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions
6  are met:
7  1. Redistributions of source code must retain the above copyright
8     notice, this list of conditions, and the following disclaimer.
9  2. Redistributions in binary form must reproduce the above copyright
10     notice, this list of conditions, and the disclaimer that follows
11     these conditions in the documentation and/or other materials
12     provided with the distribution.
13  3. The name "JPublish" must not be used to endorse or promote products
14     derived from this software without prior written permission. For
15     written permission, please contact info@aetrion.com.
16  4. Products derived from this software may not be called "JPublish", nor
17     may "JPublish" appear in their name, without prior written permission
18     from Aetrion LLC (info@aetrion.com).
19  In addition, the authors of this software request (but do not require)
20  that you include in the end-user documentation provided with the
21  redistribution and/or in the software itself an acknowledgement equivalent
22  to the following:
23      "This product includes software developed by
24       Aetrion LLC (http://www.aetrion.com/)."
25  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
26  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
29  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36  For more information on JPublish, please see <http://www.jpublish.org/>.
37  */

38 package org.jpublish.resource;
39
40 import java.io.ByteArrayOutputStream JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44
45 import com.anthonyeden.lib.util.IOUtilities;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48 import org.apache.commons.vfs.FileContent;
49 import org.apache.commons.vfs.FileObject;
50 import org.apache.commons.vfs.FileSystemManager;
51 import org.jpublish.SiteContext;
52 import org.jpublish.cache.CacheEntry;
53 import org.jpublish.cache.CacheStorage;
54 import org.jpublish.cache.HashMapCacheStorage;
55 import org.jpublish.util.PathUtilities;
56
57 /**
58  * Implementation of the ResourceManager interface which pulls static data from the file system.
59  *
60  * @author Anthony Eden
61  */

62
63 public class DefaultResourceManager extends AbstractResourceManager {
64
65     private Log log = LogFactory.getLog(DefaultResourceManager.class);
66     private CacheStorage resourceCache = null;
67
68     /**
69      * Get the resource cache.
70      *
71      * @return The resource cache
72      */

73     public synchronized CacheStorage getResourceCache() {
74         if (resourceCache == null) {
75             resourceCache = new HashMapCacheStorage();
76         }
77         return resourceCache;
78     }
79
80     /**
81      * Set the resource cache.
82      *
83      * @param resourceCache The new resource cache
84      */

85     public void setResourceCache(CacheStorage resourceCache) {
86         this.resourceCache = resourceCache;
87     }
88
89     /**
90      * Return the virtual file system manager. This method may return null if the implementation does not support a
91      * virtual file system.
92      *
93      * @return The FileSystemManager
94      * @throws IOException Description of the Exception
95      */

96     public synchronized FileSystemManager getFileSystemManager()
97             throws IOException JavaDoc {
98         if (fileSystemManager == null) {
99             configureDefaultFileSystemManager(SiteContext.DEFAULT_STATIC_ROOT);
100         }
101         return fileSystemManager;
102     }
103
104     /**
105      * Return true if the resource for the specified path exists.
106      *
107      * @param path The resource path
108      * @return True if the resource exists
109      * @throws IOException
110      */

111     public boolean exists(String JavaDoc path) throws IOException JavaDoc {
112         FileSystemManager fileSystemManager = getFileSystemManager();
113         if (log.isDebugEnabled()) {
114             log.debug("FileSystemManager: " + fileSystemManager);
115         }
116         FileObject baseFile = fileSystemManager.getBaseFile();
117         if (log.isDebugEnabled()) {
118             log.debug("Base file: " + baseFile);
119         }
120         FileObject file = fileSystemManager.resolveFile(baseFile,
121                 PathUtilities.toRelativePath(path));
122         if (log.isDebugEnabled()) {
123             log.debug("File: " + file);
124         }
125         boolean exists = file.exists();
126         if (log.isDebugEnabled()) {
127             log.debug("File exists? " + exists);
128         }
129         return exists;
130     }
131
132     /**
133      * Get the last modified time for the specified resource path.
134      *
135      * @param path The resource path
136      * @return The last modified time
137      * @throws IOException
138      */

139
140     public long getLastModified(String JavaDoc path) throws IOException JavaDoc {
141         FileSystemManager fileSystemManager = getFileSystemManager();
142         FileObject baseFile = fileSystemManager.getBaseFile();
143         FileObject file = fileSystemManager.resolveFile(baseFile,
144                 PathUtilities.toRelativePath(path));
145         FileContent content = file.getContent();
146         return content.getLastModifiedTime();
147     }
148
149     /**
150      * Get the content legnth of the resource with the given path. This method should return -1 if the content length is
151      * not known.
152      *
153      * @param path The resource path
154      * @return The content length
155      * @throws IOException Any IOException
156      */

157
158     public long getContentLength(String JavaDoc path) throws IOException JavaDoc {
159         FileSystemManager fileSystemManager = getFileSystemManager();
160         FileObject baseFile = fileSystemManager.getBaseFile();
161         FileObject file = fileSystemManager.resolveFile(baseFile,
162                 PathUtilities.toRelativePath(path));
163         FileContent content = file.getContent();
164         return content.getSize();
165     }
166
167     /**
168      * Load the resource data at the given path and write it to the specified OutputStream.
169      *
170      * @param path The resource path
171      * @param out The OutputStream
172      * @throws IOException
173      * @throws ResourceNotFoundException
174      */

175
176     public void load(String JavaDoc path, OutputStream JavaDoc out) throws IOException JavaDoc,
177             ResourceNotFoundException {
178         if (log.isDebugEnabled()) {
179             log.debug("Loading resource: " + path);
180         }
181
182         FileSystemManager fileSystemManager = getFileSystemManager();
183         FileObject baseFile = fileSystemManager.getBaseFile();
184         FileObject file = fileSystemManager.resolveFile(baseFile,
185                 PathUtilities.toRelativePath(path));
186         if (!file.exists()) {
187             throw new ResourceNotFoundException("Resource " + path +
188                     " not found");
189         }
190
191         FileContent content = file.getContent();
192         long lastModified = content.getLastModifiedTime();
193
194         // check the cache and load the template if necessary
195
CacheStorage resourceCache = getResourceCache();
196         CacheEntry cacheEntry = (CacheEntry) resourceCache.get(path);
197         byte[] data = null;
198
199         if (cacheEntry == null) {
200             log.debug("Resource (" + path + ") not found in cache.");
201             data = loadResourceData(content);
202             log.debug("Loaded data length: " + data.length);
203             resourceCache.put(path, new CacheEntry(data, lastModified));
204         } else {
205             if (log.isDebugEnabled()) {
206                 log.debug("Resource (" + path + ") found in cache.");
207             }
208             data = (byte[]) cacheEntry.getObject();
209             if (cacheEntry.getLastModified() != lastModified) {
210                 log.debug("Resource modification dates do not match.");
211                 log.debug("Reloading resource.");
212
213                 data = loadResourceData(content);
214                 resourceCache.put(path, new CacheEntry(data, lastModified));
215             }
216         }
217
218         if (data != null) {
219             log.debug("Writing data to output stream");
220             //OutputStream bout = new BufferedOutputStream(out);
221
out.write(data);
222             log.debug("Wrote " + data.length + " bytes");
223             //bout.flush();
224
//log.debug("Flushed output stream");
225
} else {
226             log.error("Data array was null");
227         }
228     }
229
230     /**
231      * Load the resource data.
232      *
233      * @param content The FileContent object
234      * @return The resource data
235      * @throws IOException
236      */

237     private byte[] loadResourceData(FileContent content) throws IOException JavaDoc {
238         InputStream JavaDoc in = null;
239         ByteArrayOutputStream JavaDoc bout = null;
240         try {
241             in = content.getInputStream();
242             bout = new ByteArrayOutputStream JavaDoc();
243
244             int c = -1;
245             while ((c = in.read()) != -1) {
246                 bout.write(c);
247             }
248             bout.flush();
249
250             return bout.toByteArray();
251         } finally {
252             IOUtilities.close(in);
253             IOUtilities.close(bout);
254         }
255     }
256
257 }
258
259
Popular Tags