1 5 6 package org.exoplatform.services.jcr.impl.core; 7 8 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import javax.jcr.PathNotFoundException; 24 import javax.jcr.RepositoryException; 25 26 import org.apache.commons.logging.Log; 27 import org.exoplatform.services.jcr.core.NodeChange; 28 import org.exoplatform.services.jcr.core.NodeData; 29 import org.exoplatform.services.jcr.core.ReferenceableNodeLocation; 30 import org.exoplatform.services.jcr.storage.RepositoryManager; 31 import org.exoplatform.services.jcr.storage.WorkspaceContainer; 32 import org.exoplatform.services.jcr.util.PathUtil; 33 import org.exoplatform.services.log.LogUtil; 34 35 public class NodesStorage { 36 37 private Map nodeChanges; 39 private Map nodeReferences; 41 private RepositoryManager repositoryManager; 42 private WorkspaceContainer workspaceContainer; 43 protected Log log; 44 45 NodesStorage(RepositoryManager repositoryManager, WorkspaceContainer workspaceContainer) { 46 nodeChanges = new HashMap (); 47 nodeReferences = new HashMap (); 48 this.repositoryManager = repositoryManager; 49 this.workspaceContainer = workspaceContainer; 50 log = LogUtil.getLog("org.exoplatform.services.jcr"); 51 } 52 53 void add(NodeImpl node) { 54 55 NodeChange change = new NodeChangeImpl(node, NodeChangeState.ADDED, node.getPath()); 56 if(node != null) 57 nodeChanges.put(change.getPath(), change); 58 59 String uuid; 61 try { 62 uuid = node.getUUID(); 63 nodeReferences.put(uuid, change); 64 } catch (Exception e) { 65 } 66 67 } 68 69 void addReference(String relatedUUID, String refPath) { 70 log.debug("NodesStorage.addReference() "+refPath+" added to "+relatedUUID); 71 NodeChange change = (NodeChange)nodeReferences.get(relatedUUID); 73 if(change == null) { 74 75 try { 76 ReferenceableNodeLocation loc = repositoryManager.getLocationByUUID(workspaceContainer.getName(), relatedUUID); 77 loc.addReferencedPath(refPath); 78 change = new NodeChangeImpl(workspaceContainer.getNodeByPath(loc.getRealPath()), NodeChangeState.REF_ADDED, refPath); 81 } catch (PathNotFoundException e) { 82 log.error("NodesStorage.addReference node for "+relatedUUID+"('"+refPath+"') not found "+change); 83 return; 84 } catch (RepositoryException e) { 85 e.printStackTrace(); 86 throw new RuntimeException ("NodesStorage.addReference node for "+relatedUUID+" FAILED "+e); 87 } 88 } 89 nodeChanges.put(refPath, change); 90 nodeReferences.put(relatedUUID, change); 91 92 } 93 94 NodeChange getNodeChangeByPath(String path) { 95 return (NodeChange)nodeChanges.get(path); 96 } 97 98 NodeChange getNodeChangeByUUID(String UUID) { 99 return (NodeChange)nodeReferences.get(UUID); 100 } 101 102 void remove(String path) { 103 NodeChange change = (NodeChange)nodeChanges.get(path); 105 log.debug("NodesStorage.remove "+change); 106 if(change != null) { 107 try { 108 String uuid = change.getNode().getUUID(); 109 nodeReferences.remove(uuid); 110 } catch (Exception e) { 111 } 113 } 114 nodeChanges.remove(path); 115 } 116 117 void setState(String path, int state) { 118 NodeChange change = (NodeChange)nodeChanges.get(path); 119 if(change != null) 120 change.setState(state); 121 } 122 123 Set getKeys() { 124 return nodeChanges.keySet(); 125 } 126 127 128 NodeData getNodeByUUID(String UUID) { 129 130 NodeChange nc = (NodeChange)nodeReferences.get(UUID); 132 if(nc != null) { 133 if(nc.getState() != NodeChangeState.UNCHANGED) 136 return nc.getNode(); 137 else { try { 139 NodeData temp = workspaceContainer.getNodeByPath(nc.getPath()); 140 return nc.refreshNode(temp); 141 } catch (Exception e) { 142 e.printStackTrace(); 143 log.error("NodesStorage.getNodeByUUID can't refresh "+nc.getNode()+" NULL is returned!!!! Reason: "+e.getMessage()); 144 return null; 145 } 146 } 147 } 148 149 ReferenceableNodeLocation loc; 152 try { 153 loc = repositoryManager.getLocationByUUID(workspaceContainer.getName(), UUID); 154 } catch (PathNotFoundException e) { 155 log.debug("NodesStorage.getNodeByUUID: UUID '"+UUID+"' not found in RepositoryManager"); 156 return null; 157 } 158 159 NodeData temp = null; 161 try { 162 temp = workspaceContainer.getNodeByPath(loc.getRealPath()); 163 } catch (RepositoryException e) { 164 e.printStackTrace(); 165 throw new RuntimeException ("NodesStorage.getNodeByPath() for "+loc.getRealPath()+" FAILED "+e); 166 } 167 168 if (temp == null) { 169 log.error("NodesStorage.getNodeByUUID: UUID '"+UUID+"' not found in "+workspaceContainer.getName()+"probably error in container/repository impl!!!"); 170 return null; 171 } 172 nc = new NodeChangeImpl(temp, NodeChangeState.UNCHANGED, temp.getPath()); 174 nodeChanges.put(loc.getRealPath(), nc); 175 nodeReferences.put(UUID, nc); 176 return nc.getNode(); 177 178 } 179 180 NodeData getNodeByPath(String absPath) { 181 NodeChange nc = (NodeChange)nodeChanges.get(absPath); 183 log.debug("NodesStorage.getNodeByPath(" + absPath + ") NodeChange :"+nc); 184 185 if (nc != null) { 186 190 if(nc.getState() != NodeChangeState.UNCHANGED) 191 return nc.getNode(); 192 else { try { 194 195 NodeData temp = workspaceContainer.getNodeByPath(nc.getPath()); 196 return nc.refreshNode(temp); 197 } catch (Exception e) { 198 e.printStackTrace(); 199 log.error("NodesStorage.getNodeByPath can't refresh "+nc.getNode()+" NULL is returned!!!! Reason: "+e.getMessage()); 200 return null; 201 } 202 } 203 } 204 205 NodeImpl temp = null; 207 208 try { 209 temp = (NodeImpl)workspaceContainer.getNodeByPath(absPath); 210 211 } catch (RepositoryException e) { 212 e.printStackTrace(); 213 throw new RuntimeException ("NodesStorage.getNodeByPath() for "+absPath+" FAILED "+e); 214 } 215 216 if(temp != null) { 217 nc = new NodeChangeImpl(temp, NodeChangeState.UNCHANGED, temp.getPath()); 218 log.debug("Node "+temp+" added to changes from container" ); 219 nodeChanges.put(absPath, nc); 220 try { 221 String uuid = nc.getNode().getUUID(); 222 nodeReferences.put(uuid, nc); 223 } catch (Exception e) { 224 } 226 return nc.getNode(); 227 } 228 229 230 ReferenceableNodeLocation loc = null; 232 try { 233 loc = repositoryManager.getLocationByPath(workspaceContainer.getName(), absPath); 234 } catch (PathNotFoundException e) { 235 log.debug("NodesStorage.getNodeByPath: Path '"+absPath+"' not found in RepositoryManager"); 236 } 238 239 240 if(loc != null) { 242 try { 243 temp = (NodeImpl)workspaceContainer.getNodeByPath(loc.getRealPath()); 244 } catch (RepositoryException e) { 245 e.printStackTrace(); 246 throw new RuntimeException ("NodesStorage.getNodeByPath() for "+absPath+" FAILED "+e); 247 } 248 if (temp == null) { 249 log.debug("NodesStorage.getNodeByPath: Path '"+loc.getRealPath()+"' not found in "+workspaceContainer.getName()); 250 return null; 251 } 252 253 nc = new NodeChangeImpl(temp, NodeChangeState.UNCHANGED, temp.getPath()); 254 log.debug("Node "+temp+" added to changes from container" ); 255 nodeChanges.put(absPath, nc); 256 try { 257 String uuid = nc.getNode().getUUID(); 258 nodeReferences.put(uuid, nc); 259 } catch (Exception e) { 260 } 262 return nc.getNode(); 263 } else 264 return null; 265 266 267 } 268 269 Set getChildren(String absPath) { 270 271 Set children = new HashSet (); 272 List changes = getChildrenChanges(absPath, true); 273 for(int i=0; i<changes.size(); i++) { 274 NodeImpl node =(NodeImpl)((NodeChange)changes.get(i)).getNode(); 275 String path = node.getPath(); 276 children.add(path); 278 } 279 280 try { 281 List nodes = workspaceContainer.getChildren(absPath); 282 for(int i=0; i<nodes.size(); i++) { 283 children.add((String )nodes.get(i)); 285 } 286 287 } catch (RepositoryException e) { 288 e.printStackTrace(); 289 throw new RuntimeException ("NodesStorage.getChildren for "+absPath+" FAILED "+e); 290 } 291 292 return children; 293 } 294 295 297 List getChildrenChanges(String path, boolean shallow) { 298 ArrayList list = new ArrayList (); 299 Iterator entries = nodeChanges.keySet().iterator(); 300 while (entries.hasNext()) { 301 String testPath = (String ) entries.next(); 302 if (PathUtil.isDescendant(testPath, path, shallow)) { 303 NodeImpl node = (NodeImpl)((NodeChange)nodeChanges.get(testPath)).getNode(); 304 log.debug("NodesStorage.getChildrenChanges for " +path+ "=" + testPath+" "+node); 306 list.add(nodeChanges.get(testPath)); 307 } 308 } 309 return list; 310 } 311 312 Set getChangedReferencedPaths(String uuid) { 314 Set list = new HashSet (); 315 if(!nodeReferences.containsKey(uuid)) 316 return list; 317 318 NodeImpl node = (NodeImpl)((NodeChange)nodeReferences.get(uuid)).getNode(); 319 Iterator entries = nodeChanges.values().iterator(); 320 while (entries.hasNext()) { 321 NodeImpl testNode = (NodeImpl)((NodeChange)entries.next()).getNode(); 322 if(testNode.equals(node)) { 323 list.add(testNode.getPath()); 324 } 325 } 326 return list; 327 } 328 329 } 330 | Popular Tags |