1 19 package org.openharmonise.vfs; 20 21 import java.util.ArrayList ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 26 34 public class VirtualFileCache { 35 36 39 private HashMap m_files = new HashMap (71); 40 41 44 private int m_nTimeout = 60000 * 20 ; 45 46 52 public VirtualFileCache(int nTimeout) { 53 super(); 54 this.m_nTimeout=nTimeout; 55 } 56 57 public VirtualFileCache() { 58 super(); 59 } 60 61 67 public synchronized boolean hasFile(String 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 92 public List getPaths() { 93 List vPaths = new ArrayList (this.m_files.keySet()); 94 95 return vPaths; 96 } 97 98 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 120 public VirtualFile getFile(String 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 ().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 151 public VirtualFile removeFile(String sFullpath) { 152 return (VirtualFile)((TimestampedObject)m_files.remove(sFullpath)).getObject(); 153 } 154 155 160 private class TimestampedObject { 161 private Object m_object; 162 private Long m_timestamp; 163 164 169 public TimestampedObject(Object obj) { 170 this(obj, new Long (new Date ().getTime())); 171 } 172 173 179 public TimestampedObject(Object obj, Long timestamp) { 180 m_timestamp = timestamp; 181 m_object = obj; 182 } 183 184 190 private boolean isTimedOut(int nTimeout) { 191 long currTime = new Date ().getTime(); 192 return ( !((VirtualFile)this.getObject()).hasVirtualFileListeners() && !((VirtualFile)this.getObject()).isChanged() && (currTime-getTimestamp().longValue())>nTimeout); 193 } 194 195 200 public Object getObject() { 201 return m_object; 202 } 203 204 209 public Long getTimestamp() { 210 return m_timestamp; 211 } 212 213 216 public void resetTimestamp() { 217 m_timestamp=new Long (new Date ().getTime()); 218 } 219 220 223 public boolean equals(Object obj) { 224 TimestampedObject to = (TimestampedObject) obj; 225 226 boolean bReturn = m_object.equals(to.getObject()); 227 228 return bReturn; 229 } 230 231 234 public String toString() { 235 return "(" + m_object.hashCode() + "," + m_timestamp + ")"; 236 } 237 } 238 239 } 240 | Popular Tags |