1 17 package org.alfresco.filesys.server.filesys.cache; 18 19 import java.io.FileNotFoundException ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.concurrent.locks.Lock ; 24 import java.util.concurrent.locks.ReadWriteLock ; 25 import java.util.concurrent.locks.ReentrantReadWriteLock ; 26 27 import org.alfresco.error.AlfrescoRuntimeException; 28 import org.alfresco.repo.cache.SimpleCache; 29 import org.alfresco.repo.node.NodeServicePolicies; 30 import org.alfresco.repo.policy.JavaBehaviour; 31 import org.alfresco.repo.policy.PolicyComponent; 32 import org.alfresco.service.cmr.repository.ChildAssociationRef; 33 import org.alfresco.service.cmr.repository.NodeRef; 34 import org.alfresco.service.namespace.NamespaceService; 35 import org.alfresco.service.namespace.QName; 36 import org.springframework.beans.factory.InitializingBean; 37 38 51 public class FilePathCache 52 implements InitializingBean, 53 NodeServicePolicies.OnCreateNodePolicy, 54 NodeServicePolicies.OnUpdateNodePolicy, 55 NodeServicePolicies.OnDeleteNodePolicy 56 { 57 58 private PolicyComponent policyComponent; 59 60 private SimpleCache<NodeRef, HashMap <String , FileState>> fileStatesCache; 61 62 private SimpleCache<NodeRef, HashMap <String , List <NodeRef>>> pathResultsCache; 63 64 private Lock cacheReadLock; 65 66 private Lock cacheWriteLock; 67 68 public FilePathCache() 69 { 70 ReadWriteLock cacheLock = new ReentrantReadWriteLock (); 72 cacheReadLock = cacheLock.readLock(); 73 cacheWriteLock = cacheLock.writeLock(); 74 } 75 76 80 public void setPolicyComponent(PolicyComponent policyComponent) 81 { 82 this.policyComponent = policyComponent; 83 } 84 85 89 public void setFileStatesCache(SimpleCache<NodeRef, HashMap <String , FileState>> cache) 90 { 91 this.fileStatesCache = cache; 92 } 93 94 98 public void setPathResultsCache(SimpleCache<NodeRef, HashMap <String , List <NodeRef>>> cache) 99 { 100 this.pathResultsCache = cache; 101 } 102 103 106 public void afterPropertiesSet() throws Exception 107 { 108 if (policyComponent == null) 109 { 110 throw new AlfrescoRuntimeException("policyComponent property not set"); 111 } 112 if (fileStatesCache == null || pathResultsCache == null) 113 { 114 throw new AlfrescoRuntimeException("fileStatesCache and pathResultsCache properties not set"); 115 } 116 policyComponent.bindClassBehaviour( 118 QName.createQName(NamespaceService.ALFRESCO_URI, "onCreateNode"), 119 this, 120 new JavaBehaviour(this, "onCreateNode")); 121 policyComponent.bindClassBehaviour( 122 QName.createQName(NamespaceService.ALFRESCO_URI, "onUpdateNode"), 123 this, 124 new JavaBehaviour(this, "onUpdateNode")); 125 policyComponent.bindClassBehaviour( 126 QName.createQName(NamespaceService.ALFRESCO_URI, "onDeleteNode"), 127 this, 128 new JavaBehaviour(this, "onDeleteNode")); 129 } 130 131 136 public void onCreateNode(ChildAssociationRef childAssocRef) 137 { 138 reset(); 140 } 141 142 147 public void onUpdateNode(NodeRef nodeRef) 148 { 149 reset(); 150 } 151 152 157 public void onDeleteNode(ChildAssociationRef childAssocRef) 158 { 159 reset(); 160 } 161 162 166 public void reset() 167 { 168 cacheWriteLock.lock(); 169 try 170 { 171 fileStatesCache.clear(); 172 pathResultsCache.clear(); 173 } 174 finally 175 { 176 cacheWriteLock.unlock(); 177 } 178 } 179 180 188 public FileState getFileState(NodeRef nodeRef, String path) 189 { 190 cacheReadLock.lock(); 191 try 192 { 193 Map <String , FileState> fileStatesByPath = fileStatesCache.get(nodeRef); 194 if (fileStatesByPath == null) 195 { 196 return null; 198 } 199 return fileStatesByPath.get(path); 200 } 201 finally 202 { 203 cacheReadLock.unlock(); 204 } 205 } 206 207 216 public FileState getExistingFileState(NodeRef nodeRef, String path) throws FileNotFoundException 217 { 218 FileState fileState = getFileState(nodeRef, path); 219 if (fileState == null) 220 { 221 return null; 223 } 224 else if (!fileState.exists()) 225 { 226 throw new FileNotFoundException (path); 228 } 229 else 230 { 231 return fileState; 233 } 234 } 235 236 243 public void setFileState(NodeRef pathRootNodeRef, String path, FileState fileState) 244 { 245 cacheWriteLock.lock(); 246 try 247 { 248 HashMap <String , FileState> fileStatesByPath = fileStatesCache.get(pathRootNodeRef); 249 if (fileStatesByPath == null) 250 { 251 fileStatesByPath = new HashMap <String , FileState>(5); 252 fileStatesCache.put(pathRootNodeRef, fileStatesByPath); 253 } 254 fileStatesByPath.put(path, fileState); 255 } 256 finally 257 { 258 cacheWriteLock.unlock(); 259 } 260 } 261 262 public List <NodeRef> getPathResults(NodeRef pathRootNodeRef, String path) 263 { 264 cacheReadLock.lock(); 265 try 266 { 267 HashMap <String , List <NodeRef>> resultsByPath = pathResultsCache.get(pathRootNodeRef); 268 if (resultsByPath == null) 269 { 270 return null; 272 } 273 return resultsByPath.get(path); 274 } 275 finally 276 { 277 cacheReadLock.unlock(); 278 } 279 } 280 281 public void setPathResults(NodeRef pathRootNodeRef, String path, List <NodeRef> results) 282 { 283 cacheWriteLock.lock(); 284 try 285 { 286 HashMap <String , List <NodeRef>> resultsByPath = pathResultsCache.get(pathRootNodeRef); 287 if (resultsByPath == null) 288 { 289 resultsByPath = new HashMap <String , List <NodeRef>>(5); 291 pathResultsCache.put(pathRootNodeRef, resultsByPath); 292 } 293 resultsByPath.put(path, results); 294 } 295 finally 296 { 297 cacheWriteLock.unlock(); 298 } 299 } 300 } 301 | Popular Tags |