KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > VirtualFileCache


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.vfs;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Generic cache for VirtualFileObjects. The default timeout for these objects
28  * is 5 minutes, this can be overridden.
29  *
30  * @author Matthew Large
31  * @version $Revision: 1.1 $
32  *
33  */

34 public class VirtualFileCache {
35
36     /**
37      * Map of path to {@link TimestampedObject} objects.
38      */

39     private HashMap JavaDoc m_files = new HashMap JavaDoc(71);
40     
41     /**
42      * Timeout value for virtual files in the cache in milliseconds.
43      */

44     private int m_nTimeout = 60000 /*1 minute in milliseconds*/ * 20 /*minutes*/;
45
46     /**
47      * Constructs a VirtualFileCache with a specific timeout, overriding the default
48      * 5 minute timeout.
49      *
50      * @param nTimeout Sets the cache's timeout value in milliseconds. (60000=1 minute)
51      */

52     public VirtualFileCache(int nTimeout) {
53         super();
54         this.m_nTimeout=nTimeout;
55     }
56     
57     public VirtualFileCache() {
58         super();
59     }
60
61     /**
62      * Checks if the cache contains a specific VirtualFile as indexed by path.
63      *
64      * @param sFullpath The full path of the VirtualFile to be checked
65      * @return true if the cache contains the VirtualFile. If the VirtualFile has timed out it will be removed from the cache and False will be returned
66      */

67     public synchronized boolean hasFile(String JavaDoc sFullpath) {
68       boolean bRetn = false;
69       if(sFullpath!=null && sFullpath.endsWith("/")) {
70         sFullpath = sFullpath.substring(0, sFullpath.length()-1);
71       }
72     
73       if( sFullpath!=null && m_files.containsKey(sFullpath.trim()) && m_files.get(sFullpath.trim())!=null ) {
74           if( !((TimestampedObject)m_files.get(sFullpath.trim())).isTimedOut(this.m_nTimeout)) {
75               bRetn=true;
76           } else {
77               this.removeFile(sFullpath.trim());
78           }
79       }
80       if(!bRetn) {
81         
82       }
83       
84       return bRetn;
85     }
86   
87     /**
88      * Gets a List object containing all the path Strings in the cache.
89      *
90      * @return List of path Strings
91      */

92     public List JavaDoc getPaths() {
93       List JavaDoc vPaths = new ArrayList JavaDoc(this.m_files.keySet());
94     
95       return vPaths;
96     }
97
98     /**
99      * Adds a VirtualFile to the cache, if a VirtualFile with the path is already in the cache it will be overwritten with this one.
100      *
101      * @param vfFile VirtualFile to be added
102      */

103     public synchronized void addFile(VirtualFile vfFile) {
104       TimestampedObject tsObj = new TimestampedObject(vfFile);
105       if(m_files.keySet().contains(vfFile.getFullPath().trim())) {
106         if( !((VirtualFile)((TimestampedObject)m_files.get(vfFile.getFullPath().trim())).getObject()).isChanged() ) {
107             m_files.put(vfFile.getFullPath().trim(), tsObj);
108         }
109       } else {
110         m_files.put(vfFile.getFullPath().trim(), tsObj);
111       }
112     }
113
114     /**
115      * Gets a VirtualFile from the cache as specified by the path.
116      *
117      * @param sFullpath Path of the VirtualFile to be returned
118      * @return Requested VirtualFile or null if path is not found in cache
119      */

120     public VirtualFile getFile(String JavaDoc sFullpath) {
121       VirtualFile vfRetn = null;
122       if(sFullpath.endsWith("/")) {
123         sFullpath = sFullpath.substring(0, sFullpath.length()-1);
124       }
125     
126       TimestampedObject tsObj = (TimestampedObject)m_files.get(sFullpath.trim());
127       if( tsObj!=null ) {
128         
129             if( !((VirtualFile)tsObj.getObject()).getFullPath().equalsIgnoreCase(sFullpath)) {
130             }
131         
132           long currTime = new Date JavaDoc().getTime();
133           if( !tsObj.isTimedOut(this.m_nTimeout) ) {
134               vfRetn = (VirtualFile)tsObj.getObject();
135               tsObj.resetTimestamp();
136           } else {
137               this.removeFile(sFullpath);
138           }
139       }
140
141       return vfRetn;
142     }
143
144
145     /**
146      * Removes a specific VirtualFile from the cache.
147      *
148      * @param sFullpath Path of VirtualFile to be removed
149      * @return previous value associated with specified key, or null if there was no mapping for key
150      */

151     public VirtualFile removeFile(String JavaDoc sFullpath) {
152       return (VirtualFile)((TimestampedObject)m_files.remove(sFullpath)).getObject();
153     }
154
155         /**
156        * Wraps an object with a timestamp
157        *
158        * @author Matthew Large
159        */

160       private class TimestampedObject {
161           private Object JavaDoc m_object;
162           private Long JavaDoc m_timestamp;
163
164           /**
165          * Wraps the supplied object and gives it a timestamp of the current system time.
166          *
167          * @param obj Object to be timestamped
168          */

169         public TimestampedObject(Object JavaDoc obj) {
170               this(obj, new Long JavaDoc(new Date JavaDoc().getTime()));
171           }
172
173           /**
174          * Wraps the supplied object and gives it the specified timestamp.
175          *
176          * @param obj Object to be timestamped
177          * @param timestamp Timestamp to give to the object
178          */

179         public TimestampedObject(Object JavaDoc obj, Long JavaDoc timestamp) {
180               m_timestamp = timestamp;
181               m_object = obj;
182           }
183         
184           /**
185          * Checks a timeout value
186          *
187          * @param nTimeout
188          * @return True if the object is timed out
189          */

190         private boolean isTimedOut(int nTimeout) {
191           long currTime = new Date JavaDoc().getTime();
192           return ( !((VirtualFile)this.getObject()).hasVirtualFileListeners() && !((VirtualFile)this.getObject()).isChanged() && (currTime-getTimestamp().longValue())>nTimeout);
193       }
194
195           /**
196          * Gets the object
197          *
198          * @return Object that is wrapped by this Timestampedobject
199          */

200         public Object JavaDoc getObject() {
201               return m_object;
202           }
203
204           /**
205          * Gets the current timestamp value
206          *
207          * @return Timestamp in milliseconds
208          */

209         public Long JavaDoc getTimestamp() {
210               return m_timestamp;
211           }
212         
213           /**
214          * Resets the timestamp value to the current system time
215          */

216         public void resetTimestamp() {
217               m_timestamp=new Long JavaDoc(new Date JavaDoc().getTime());
218           }
219
220           /* (non-Javadoc)
221          * @see java.lang.Object#equals(java.lang.Object)
222          */

223         public boolean equals(Object JavaDoc obj) {
224               TimestampedObject to = (TimestampedObject) obj;
225
226               boolean bReturn = m_object.equals(to.getObject());
227
228               return bReturn;
229           }
230
231           /* (non-Javadoc)
232          * @see java.lang.Object#toString()
233          */

234         public String JavaDoc toString() {
235               return "(" + m_object.hashCode() + "," + m_timestamp + ")";
236           }
237       }
238
239 }
240
Popular Tags