KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > cache > CmsVfsNameBasedDiskCache


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/cache/CmsVfsNameBasedDiskCache.java,v $
3  * Date : $Date: 2006/07/20 12:38:54 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.cache;
33
34 import org.opencms.file.CmsResource;
35 import org.opencms.util.CmsFileUtil;
36
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Implements a name based RFS file based disk cache, that handles parameter based versions of VFS files,
42  * providing a cache for the "online" and another for the "offline" project.<p>
43  *
44  * This RFS cache operates on file names, plus a hashcode calculated from
45  * {@link org.opencms.file.CmsResource#getDateLastModified()}, {@link org.opencms.file.CmsResource#getDateCreated()}
46  * and {@link org.opencms.file.CmsResource#getLength()}. Optional parameters can be appended to this name,
47  * which will be added as a second hashcode. This way a file can have multiple versions based on different parameters.<p>
48  *
49  * This cache should be usable for resources from the online AND the offline project at the same time,
50  * because any change to a resource will result in a changed hashcode. This means a resource changed in the offline
51  * project will have a new hashcode compared to the online project.<p>
52  *
53  * @author Alexander Kandzior
54  *
55  * @version $Revision: 1.3 $
56  *
57  * @since 6.2.0
58  */

59 public class CmsVfsNameBasedDiskCache {
60
61     /** The name of the cache base repository folder in the RFS. */
62     private String JavaDoc m_rfsRepository;
63
64     /**
65      * Creates a new disk cache.<p>
66      *
67      * @param basepath the base path for the cache in the RFS
68      * @param foldername the folder name for this cache, to be used a subfolder for the base folder
69      */

70     public CmsVfsNameBasedDiskCache(String JavaDoc basepath, String JavaDoc foldername) {
71
72         // normalize the given folder name
73
m_rfsRepository = CmsFileUtil.normalizePath(basepath + foldername + File.separatorChar);
74     }
75
76     /**
77      * Returns the content of the requested file in the disk cache, or <code>null</code> if the
78      * file is not found in the cache, or is found but outdated.<p>
79      *
80      * @param rfsName the file RFS name to look up in the cache
81      *
82      * @return the content of the requested file in the VFS disk cache, or <code>null</code>
83      */

84     public byte[] getCacheContent(String JavaDoc rfsName) {
85
86         try {
87             File JavaDoc f = new File JavaDoc(rfsName);
88             if (f.exists()) {
89                 long age = f.lastModified();
90                 if ((System.currentTimeMillis() - age) > 3600000) {
91                     // file has not been touched for 1 hour, touch the file with the current date
92
f.setLastModified(System.currentTimeMillis());
93                 }
94                 return CmsFileUtil.readFile(f);
95             }
96         } catch (IOException JavaDoc e) {
97             // unable to read content
98
}
99         return null;
100     }
101
102     /**
103      * Returns the RFS name to use for caching the given VFS resource with parameters in the disk cache.<p>
104      *
105      * @param resource the VFS resource to generate the cache name for
106      * @param parameters the parameters of the request to the VFS resource
107      *
108      * @return the RFS name to use for caching the given VFS resource with parameters
109      */

110     public String JavaDoc getCacheName(CmsResource resource, String JavaDoc parameters) {
111
112         // calculate the base cache path for the resource
113
String JavaDoc rfsName = m_rfsRepository + resource.getRootPath();
114         String JavaDoc extension = CmsFileUtil.getFileExtension(rfsName);
115
116         // create a StringBuffer for the result
117
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(rfsName.length() + 24);
118         buf.append(rfsName.substring(0, rfsName.length() - extension.length()));
119
120         // calculate a hash code that contains the resource DateLastModified, DateCreated and Length
121
StringBuffer JavaDoc ext = new StringBuffer JavaDoc(48);
122         ext.append(resource.getDateLastModified());
123         ext.append(';');
124         ext.append(resource.getDateCreated());
125         if (resource.getLength() > 0) {
126             ext.append(';');
127             ext.append(resource.getLength());
128         }
129         // append hash code to the result buffer
130
buf.append(ext.toString().hashCode());
131
132         // check if parameters are provided, if so add them as well
133
if (parameters != null) {
134             buf.append('_');
135             buf.append(parameters.hashCode());
136         }
137
138         // finally append the extension from the original file name
139
buf.append(extension);
140         return buf.toString();
141     }
142
143     /**
144      * Returns the absolute path of the cache repository in the RFS.<p>
145      *
146      * @return the absolute path of the cache repository in the RFS
147      */

148     public String JavaDoc getRepositoryPath() {
149
150         return m_rfsRepository;
151     }
152
153     /**
154      * Saves the given file content in the disk cache.<p>
155      *
156      * @param rfsName the RFS name of the file to save the content in
157      * @param content the content of the file to save
158      *
159      * @throws IOException in case of disk access errors
160      */

161     public void saveCacheFile(String JavaDoc rfsName, byte[] content) throws IOException JavaDoc {
162
163         CmsVfsDiskCache.saveFile(rfsName, content);
164     }
165 }
Popular Tags