KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/cache/CmsVfsDiskCache.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.2 $
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.util.CmsFileUtil;
35 import org.opencms.util.CmsStringUtil;
36
37 import java.io.File JavaDoc;
38 import java.io.FileOutputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Implements a RFS file based disk cache, that handles parameter based versions of VFS files,
43  * providing a cache for the "online" and another for the "offline" project.<p>
44  *
45  * @author Alexander Kandzior
46  *
47  * @version $Revision: 1.2 $
48  *
49  * @since 6.2.0
50  */

51 public class CmsVfsDiskCache {
52
53     /** The name of the cache base repository folder in the RFS. */
54     private String JavaDoc m_rfsRepository;
55
56     /**
57      * Creates a new disk cache.<p>
58      *
59      * @param basepath the base path for the cache in the RFS
60      * @param foldername the folder name for this cache, to be used a subfolder for the base folder
61      */

62     public CmsVfsDiskCache(String JavaDoc basepath, String JavaDoc foldername) {
63
64         // normalize the given folder name
65
m_rfsRepository = CmsFileUtil.normalizePath(basepath + foldername + File.separatorChar);
66     }
67
68     /**
69      * Saves the given file content to a RFS file of the given name (full path).<p>
70      *
71      * If the required pared folders do not exists, they are also created.<p>
72      *
73      * @param rfsName the RFS name of the file to save the content in
74      * @param content the content of the file to save
75      *
76      * @return a reference to the File that was saved
77      * @throws IOException in case of disk access errors
78      */

79     protected static File JavaDoc saveFile(String JavaDoc rfsName, byte[] content) throws IOException JavaDoc {
80
81         File JavaDoc f = new File JavaDoc(rfsName);
82         File JavaDoc p = f.getParentFile();
83         if (!p.exists()) {
84             // create parent folders
85
p.mkdirs();
86         }
87         // write file contents
88
FileOutputStream JavaDoc fs = new FileOutputStream JavaDoc(f);
89         fs.write(content);
90         fs.close();
91         return f;
92     }
93
94     /**
95      * Returns the content of the requested file in the disk cache, or <code>null</code> if the
96      * file is not found in the cache, or is found but outdated.<p>
97      *
98      * @param rfsName the file RFS name to look up in the cache
99      * @param dateLastModified the date of last modification for the cache
100      *
101      * @return the content of the requested file in the VFS disk cache, or <code>null</code>
102      */

103     public byte[] getCacheContent(String JavaDoc rfsName, long dateLastModified) {
104
105         dateLastModified = simplifyDateLastModified(dateLastModified);
106         try {
107             File JavaDoc f = new File JavaDoc(rfsName);
108             if (f.exists()) {
109                 if (f.lastModified() != dateLastModified) {
110                     // last modification time different, remove cached file in RFS
111
f.delete();
112                 } else {
113                     return CmsFileUtil.readFile(f);
114                 }
115             }
116         } catch (IOException JavaDoc e) {
117             // unable to read content
118
}
119         return null;
120     }
121
122     /**
123      * Returns the RFS name to use for caching the given VFS resource with parameters in the disk cache.<p>
124      *
125      * @param online if true, the online disk cache is used, the offline disk cache otherwise
126      * @param rootPath the VFS resource root path to get the RFS cache name for
127      * @param parameters the parameters of the request to the VFS resource
128      *
129      * @return the RFS name to use for caching the given VFS resource with parameters
130      */

131     public String JavaDoc getCacheName(boolean online, String JavaDoc rootPath, String JavaDoc parameters) {
132
133         String JavaDoc rfsName = CmsFileUtil.getRepositoryName(m_rfsRepository, rootPath, online);
134         if (CmsStringUtil.isNotEmpty(parameters)) {
135             String JavaDoc extension = CmsFileUtil.getFileExtension(rfsName);
136             // build the RFS name for the VFS name with parameters
137
rfsName = CmsFileUtil.getRfsPath(rfsName, extension, parameters);
138         }
139
140         return rfsName;
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      * @param dateLastModified the date of last modification to set for the save file
159      *
160      * @throws IOException in case of disk access errors
161      */

162     public void saveCacheFile(String JavaDoc rfsName, byte[] content, long dateLastModified) throws IOException JavaDoc {
163
164         dateLastModified = simplifyDateLastModified(dateLastModified);
165         File JavaDoc f = saveFile(rfsName, content);
166         // set last modification date
167
f.setLastModified(dateLastModified);
168     }
169
170     /**
171      * Simplifies the "date last modified" from the OpenCms VFS based milisseconds
172      * to just seconds, to support file systems that don't use milisseconds.
173      *
174      * @param dateLastModified the date to simplify
175      *
176      * @return the simplified date last modified
177      */

178     private long simplifyDateLastModified(long dateLastModified) {
179
180         return dateLastModified / 1000L;
181     }
182 }
Popular Tags