1 package org.sapia.regis.cache; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.sapia.regis.DuplicateNodeException; 11 import org.sapia.regis.Node; 12 import org.sapia.regis.Path; 13 import org.sapia.regis.Property; 14 import org.sapia.regis.Query; 15 import org.sapia.regis.Registry; 16 17 30 public class CacheNode implements Node{ 31 32 private Map props; 33 private Path path; 34 private String name, type; 35 private Registry registry; 36 private long lastChecksum, lastCheck, interval; 37 38 CacheNode( 39 Node node, 40 Registry registry, 41 long refreshIntervalMillis){ 42 init(node, registry, refreshIntervalMillis); 43 } 44 45 CacheNode( 46 Path path, 47 Registry registry, 48 long refreshIntervalMillis){ 49 init(registry.getRoot().getChild(path), registry, refreshIntervalMillis); 50 } 51 52 void init(Node node, 53 Registry registry, 54 long refreshIntervalMillis){ 55 this.name = node.getName(); 56 this.type = node.getType(); 57 this.path = node.getAbsolutePath(); 58 this.props = node.getProperties(); 59 this.registry = registry; 60 this.interval = refreshIntervalMillis; 61 this.lastChecksum = node.lastModifChecksum(); 62 this.lastCheck = System.currentTimeMillis(); 63 } 64 65 public Collection getPropertyKeys() { 66 refresh(); 67 return new ArrayList (props().keySet()); 68 } 69 70 public Path getAbsolutePath() { 71 return path; 72 } 73 74 public Node getChild(Path path) { 75 Node child = node().getChild(path); 76 if(child == null){ 77 return child; 78 } 79 else return new CacheNode(child, registry, interval); 80 } 81 82 public Node getChild(String name) { 83 Node child = node().getChild(name); 84 if(child == null){ 85 return child; 86 } 87 else return new CacheNode(child, registry, interval); 88 } 89 90 public Collection getChildren() { 91 return doGetNodes(node().getChildren()); 92 } 93 94 public Collection getIncludes() { 95 return doGetNodes(node().getIncludes()); 96 } 97 98 public Collection getLinks(boolean prepended) { 99 return doGetNodes(node().getLinks(prepended)); 100 } 101 102 public Collection getNodes(Query query) { 103 return doGetNodes(node().getNodes(query)); 104 } 105 106 public String getName() { 107 return name; 108 } 109 110 public String getType() { 111 return type; 112 } 113 114 public Node getParent() { 115 return new CacheNode(node().getParent(), registry, interval); 116 } 117 118 public Map getProperties() { 119 return new HashMap (props()); 120 } 121 122 public Map getProperties(Map values) { 123 return node().getProperties(new RemoteMapProxy(values)); 124 } 125 126 public Property getProperty(String key) { 127 refresh(); 128 String value = (String )props().get(key); 129 return new CacheProperty(key, value, this); 130 } 131 132 public Property renderProperty(String key) { 133 return getProperty(key); 134 } 135 136 public Property renderProperty(String key, Map values) { 137 return node().renderProperty(key, new RemoteMapProxy(values)); 138 } 139 140 public boolean isInheritsParent() { 141 return node().isInheritsParent(); 142 } 143 144 public boolean isRoot() { 145 return name == null || name.equals(Node.ROOT_NAME); 146 } 147 148 public long lastModifChecksum() { 149 refresh(); 150 return lastChecksum; 151 } 152 153 156 public Node internal(){ 157 return node(); 158 } 159 160 protected Node node(){ 161 Node node = registry.getRoot().getChild(path); 162 if(node == null){ 163 throw new NoSuchNodeException(path.toString()); 164 } 165 return node; 166 } 167 168 private void refresh(){ 169 if(System.currentTimeMillis() - lastCheck > interval){ 170 synchronized(this){ 171 if(System.currentTimeMillis() - lastCheck > interval){ 172 Node node = node(); 173 long currentChecksum = node.lastModifChecksum(); 174 if(lastChecksum != currentChecksum){ 175 try{ 176 Map newProps = node.getProperties(); 177 props = newProps; 178 lastChecksum = currentChecksum; 179 }catch(RuntimeException e){} 180 lastCheck = System.currentTimeMillis(); 181 } 182 } 183 } 184 } 185 } 186 187 private Map props(){ 188 Map toReturn = props; 189 return toReturn; 190 } 191 192 private List doGetNodes(Collection original){ 193 Iterator nodes = original.iterator(); 194 List toReturn = new ArrayList (original.size()); 195 while(nodes.hasNext()){ 196 Node node = (Node)nodes.next(); 197 toReturn.add(new CacheNode(node, registry, interval)); 198 } 199 return toReturn; 200 } 201 202 204 207 public void deleteChild(String name) { 208 throw new UnsupportedOperationException (); 209 } 210 211 214 public void deleteChildren() { 215 throw new UnsupportedOperationException (); 216 } 217 218 221 public void deleteProperties() { 222 throw new UnsupportedOperationException (); 223 } 224 225 228 public void deleteProperty(String key) { 229 throw new UnsupportedOperationException (); 230 } 231 232 235 public void appendLink(Node node) { 236 throw new UnsupportedOperationException (); 237 } 238 239 242 public Node createChild(String name) throws DuplicateNodeException { 243 throw new UnsupportedOperationException (); 244 } 245 246 247 250 public void moveTo(Node newParent) { 251 throw new UnsupportedOperationException (); 252 } 253 254 257 public void prependLink(Node node) { 258 throw new UnsupportedOperationException (); 259 } 260 261 264 public void removeAppendedLink(Node node) { 265 throw new UnsupportedOperationException (); 266 } 267 268 271 public void removePrependedLink(Node node) { 272 throw new UnsupportedOperationException (); 273 } 274 275 278 public void setInheritsParent(boolean inheritsParent) { 279 throw new UnsupportedOperationException (); 280 } 281 282 285 public void setProperty(String key, String value) { 286 throw new UnsupportedOperationException (); 287 } 288 289 292 public void setType(String type) { 293 throw new UnsupportedOperationException (); 294 } 295 } 296 | Popular Tags |