KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > cache > CacheNode


1 package org.sapia.regis.cache;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
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 /**
18  * An instance of this class wraps a given <code>Node</code>, copying the
19  * properties of the wrapped node and refreshing them on a periodic basis, in a
20  * synchronous manner, and at a predefined time interval.
21  * <p>
22  * All child nodes of an instance of this class also perform caching behaviour.
23  * <p>
24  * Note that this instance is read-only. All "write" methods throw
25  * <code>UnsupportedOperationException</code>s.
26  *
27  * @author yduchesne
28  *
29  */

30 public class CacheNode implements Node{
31   
32   private Map JavaDoc props;
33   private Path path;
34   private String JavaDoc 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 JavaDoc getPropertyKeys() {
66     refresh();
67     return new ArrayList JavaDoc(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 JavaDoc 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 JavaDoc getChildren() {
91     return doGetNodes(node().getChildren());
92   }
93   
94   public Collection JavaDoc getIncludes() {
95     return doGetNodes(node().getIncludes());
96   }
97
98   public Collection JavaDoc getLinks(boolean prepended) {
99     return doGetNodes(node().getLinks(prepended));
100   }
101   
102   public Collection JavaDoc getNodes(Query query) {
103     return doGetNodes(node().getNodes(query));
104   }
105
106   public String JavaDoc getName() {
107     return name;
108   }
109   
110   public String JavaDoc getType() {
111     return type;
112   }
113
114   public Node getParent() {
115     return new CacheNode(node().getParent(), registry, interval);
116   }
117
118   public Map JavaDoc getProperties() {
119     return new HashMap JavaDoc(props());
120   }
121
122   public Map JavaDoc getProperties(Map JavaDoc values) {
123      return node().getProperties(new RemoteMapProxy(values));
124   }
125
126   public Property getProperty(String JavaDoc key) {
127     refresh();
128     String JavaDoc value = (String JavaDoc)props().get(key);
129     return new CacheProperty(key, value, this);
130   }
131   
132   public Property renderProperty(String JavaDoc key) {
133     return getProperty(key);
134   }
135   
136   public Property renderProperty(String JavaDoc key, Map JavaDoc 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   /**
154    * @return the <code>RegistryNode</code> corresponding to this instance
155    */

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 JavaDoc newProps = node.getProperties();
177               props = newProps;
178               lastChecksum = currentChecksum;
179             }catch(RuntimeException JavaDoc e){}
180             lastCheck = System.currentTimeMillis();
181           }
182         }
183       }
184     }
185   }
186   
187   private Map JavaDoc props(){
188     Map JavaDoc toReturn = props;
189     return toReturn;
190   }
191   
192   private List JavaDoc doGetNodes(Collection JavaDoc original){
193     Iterator JavaDoc nodes = original.iterator();
194     List JavaDoc toReturn = new ArrayList JavaDoc(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   ////////////////////// UNSUPPORTED /////////////////////
203

204   /**
205    * @throws UnsupportedOperationException
206    */

207   public void deleteChild(String JavaDoc name) {
208     throw new UnsupportedOperationException JavaDoc();
209   }
210
211   /**
212    * @throws UnsupportedOperationException
213    */

214   public void deleteChildren() {
215     throw new UnsupportedOperationException JavaDoc();
216   }
217   
218   /**
219    * @throws UnsupportedOperationException
220    */

221   public void deleteProperties() {
222     throw new UnsupportedOperationException JavaDoc();
223   }
224
225   /**
226    * @throws UnsupportedOperationException
227    */

228   public void deleteProperty(String JavaDoc key) {
229     throw new UnsupportedOperationException JavaDoc();
230   }
231   
232   /**
233    * @throws UnsupportedOperationException
234    */

235   public void appendLink(Node node) {
236     throw new UnsupportedOperationException JavaDoc();
237   }
238
239   /**
240    * @throws UnsupportedOperationException
241    */

242   public Node createChild(String JavaDoc name) throws DuplicateNodeException {
243     throw new UnsupportedOperationException JavaDoc();
244   }
245   
246   
247   /**
248    * @throws UnsupportedOperationException
249    */

250   public void moveTo(Node newParent) {
251     throw new UnsupportedOperationException JavaDoc();
252   }
253
254   /**
255    * @throws UnsupportedOperationException
256    */

257   public void prependLink(Node node) {
258     throw new UnsupportedOperationException JavaDoc();
259   }
260
261   /**
262    * @throws UnsupportedOperationException
263    */

264   public void removeAppendedLink(Node node) {
265     throw new UnsupportedOperationException JavaDoc();
266   }
267
268   /**
269    * @throws UnsupportedOperationException
270    */

271   public void removePrependedLink(Node node) {
272     throw new UnsupportedOperationException JavaDoc();
273   }
274
275   /**
276    * @throws UnsupportedOperationException
277    */

278   public void setInheritsParent(boolean inheritsParent) {
279     throw new UnsupportedOperationException JavaDoc();
280   }
281
282   /**
283    * @throws UnsupportedOperationException
284    */

285   public void setProperty(String JavaDoc key, String JavaDoc value) {
286     throw new UnsupportedOperationException JavaDoc();
287   }
288   
289   /**
290    * @throws UnsupportedOperationException
291    */

292   public void setType(String JavaDoc type) {
293     throw new UnsupportedOperationException JavaDoc();
294   }
295 }
296
Popular Tags