1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.prefs.impl; |
18 |
|
|
19 |
|
import java.util.ArrayList; |
20 |
|
import java.util.Collection; |
21 |
|
import java.util.Iterator; |
22 |
|
import java.util.Vector; |
23 |
|
|
24 |
|
import org.apache.jetspeed.cache.CacheElement; |
25 |
|
import org.apache.jetspeed.cache.DistributedCacheObject; |
26 |
|
import org.apache.jetspeed.cache.JetspeedCache; |
27 |
|
import org.apache.jetspeed.components.dao.InitablePersistenceBrokerDaoSupport; |
28 |
|
import org.apache.jetspeed.prefs.FailedToCreateNodeException; |
29 |
|
import org.apache.jetspeed.prefs.NodeAlreadyExistsException; |
30 |
|
import org.apache.jetspeed.prefs.NodeDoesNotExistException; |
31 |
|
import org.apache.jetspeed.prefs.PreferencesProvider; |
32 |
|
import org.apache.jetspeed.prefs.om.Node; |
33 |
|
import org.apache.jetspeed.prefs.om.Property; |
34 |
|
import org.apache.jetspeed.prefs.om.impl.NodeImpl; |
35 |
|
import org.apache.jetspeed.prefs.om.impl.PropertyImpl; |
36 |
|
import org.apache.ojb.broker.query.Criteria; |
37 |
|
import org.apache.ojb.broker.query.Query; |
38 |
|
import org.apache.ojb.broker.query.QueryFactory; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
public class PersistenceBrokerPreferencesProvider extends InitablePersistenceBrokerDaoSupport implements |
49 |
|
PreferencesProvider |
50 |
|
{ |
51 |
|
|
52 |
|
private static class NodeCache implements DistributedCacheObject |
53 |
|
{ |
54 |
|
|
55 |
|
private static final long serialVersionUID = 1853381807991868844L; |
56 |
0 |
NodeImplProxy node = null; |
57 |
0 |
String key = null;; |
58 |
0 |
Collection children = null;; |
59 |
|
|
60 |
|
public NodeCache(NodeImplProxy node) |
61 |
0 |
{ |
62 |
|
|
63 |
0 |
this.node = node; |
64 |
0 |
this.key = node.getFullPath() + "-" + node.getNodeType(); |
65 |
0 |
} |
66 |
|
|
67 |
|
public NodeCache(String fullpath, int type) |
68 |
0 |
{ |
69 |
|
|
70 |
0 |
this.key = fullpath + "-" + type; |
71 |
0 |
} |
72 |
|
|
73 |
|
public boolean isChildrenLoaded() |
74 |
|
{ |
75 |
|
|
76 |
0 |
return children != null; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
public NodeImplProxy getNode() |
82 |
|
{ |
83 |
|
|
84 |
0 |
return node; |
85 |
|
} |
86 |
|
|
87 |
|
public void setNode(NodeImplProxy node) |
88 |
|
{ |
89 |
|
|
90 |
0 |
this.node = node; |
91 |
0 |
} |
92 |
|
|
93 |
|
public Collection getChildren() |
94 |
|
{ |
95 |
|
|
96 |
0 |
return children; |
97 |
|
} |
98 |
|
|
99 |
|
public void setChildren(Collection children) |
100 |
|
{ |
101 |
|
|
102 |
0 |
this.children = children; |
103 |
0 |
} |
104 |
|
|
105 |
|
public boolean equals(Object obj) |
106 |
|
{ |
107 |
0 |
if (obj != null && obj instanceof NodeCache) |
108 |
|
{ |
109 |
0 |
NodeCache other = (NodeCache) obj; |
110 |
0 |
return getKey().equals(other.getKey()); |
111 |
|
} |
112 |
0 |
return false; |
113 |
|
} |
114 |
|
|
115 |
|
public int hashCode() |
116 |
|
{ |
117 |
0 |
return getKey().hashCode(); |
118 |
|
} |
119 |
|
|
120 |
|
public String getCacheKey() |
121 |
|
{ |
122 |
0 |
return getKey(); |
123 |
|
} |
124 |
|
|
125 |
|
public String getKey() |
126 |
|
{ |
127 |
0 |
return key; |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
public void notifyChange(int action) |
132 |
|
{ |
133 |
|
|
134 |
0 |
switch (action) |
135 |
|
{ |
136 |
|
case CacheElement.ActionAdded: |
137 |
|
|
138 |
0 |
break; |
139 |
|
case CacheElement.ActionChanged: |
140 |
|
|
141 |
0 |
if (this.node != null) |
142 |
0 |
this.node.invalidate(); |
143 |
|
break; |
144 |
|
case CacheElement.ActionRemoved: |
145 |
|
|
146 |
0 |
if (this.node != null) |
147 |
0 |
this.node.invalidate(); |
148 |
|
break; |
149 |
|
case CacheElement.ActionEvicted: |
150 |
|
|
151 |
0 |
if (this.node != null) |
152 |
0 |
this.node.invalidate(); |
153 |
|
break; |
154 |
|
case CacheElement.ActionExpired: |
155 |
|
|
156 |
0 |
if (this.node != null) |
157 |
0 |
this.node.invalidate(); |
158 |
|
break; |
159 |
|
default: |
160 |
0 |
System.out.println("CacheObject - UNKOWN OPRERATION =" + this.getKey()); |
161 |
0 |
return; |
162 |
|
} |
163 |
0 |
return; |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
|
private JetspeedCache preferenceCache; |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
public PersistenceBrokerPreferencesProvider(String repositoryPath) |
182 |
|
throws ClassNotFoundException |
183 |
|
{ |
184 |
|
super(repositoryPath); |
185 |
|
NodeImplProxy.setProvider(this); |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
public PersistenceBrokerPreferencesProvider(String repositoryPath, JetspeedCache preferenceCache) |
200 |
|
throws ClassNotFoundException |
201 |
|
{ |
202 |
|
this(repositoryPath); |
203 |
|
this.preferenceCache = preferenceCache; |
204 |
|
} |
205 |
|
|
206 |
|
protected void addToCache(NodeCache content) |
207 |
|
{ |
208 |
|
CacheElement cachedElement = preferenceCache.createElement(content.getCacheKey(), content); |
209 |
|
cachedElement.setTimeToIdleSeconds(preferenceCache.getTimeToIdleSeconds()); |
210 |
|
cachedElement.setTimeToLiveSeconds(preferenceCache.getTimeToLiveSeconds()); |
211 |
|
preferenceCache.put(cachedElement); |
212 |
|
} |
213 |
|
|
214 |
|
private NodeCache getNode(String cacheKey) |
215 |
|
{ |
216 |
|
CacheElement cachedElement = preferenceCache.get(cacheKey); |
217 |
|
if (cachedElement != null) |
218 |
|
return (NodeCache)cachedElement.getContent(); |
219 |
|
return null; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
public Node getNode(String fullPath, int nodeType) throws NodeDoesNotExistException |
224 |
|
{ |
225 |
|
NodeCache key = new NodeCache(fullPath, nodeType); |
226 |
|
NodeCache hit = getNode(key.getCacheKey()); |
227 |
|
if (hit != null) |
228 |
|
{ |
229 |
|
return hit.getNode(); |
230 |
|
} |
231 |
|
|
232 |
|
Criteria c = new Criteria(); |
233 |
|
c.addEqualTo("fullPath", fullPath); |
234 |
|
c.addEqualTo("nodeType", new Integer(nodeType)); |
235 |
|
Query query = QueryFactory.newQuery(NodeImpl.class, c); |
236 |
|
|
237 |
|
Node nodeObj = (Node) getPersistenceBrokerTemplate().getObjectByQuery(query); |
238 |
|
if (null != nodeObj) |
239 |
|
{ |
240 |
|
NodeImplProxy proxy = new NodeImplProxy(nodeObj); |
241 |
|
addToCache(new NodeCache(proxy)); |
242 |
|
return proxy; |
243 |
|
|
244 |
|
} |
245 |
|
else |
246 |
|
{ |
247 |
|
throw new NodeDoesNotExistException("No node of type " + nodeType + "found at path: " + fullPath); |
248 |
|
} |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
public void redoNode(NodeImplProxy proxy, String fullPath, int nodeType) throws NodeDoesNotExistException |
254 |
|
{ |
255 |
|
|
256 |
|
Criteria c = new Criteria(); |
257 |
|
c.addEqualTo("fullPath", fullPath); |
258 |
|
c.addEqualTo("nodeType", new Integer(nodeType)); |
259 |
|
Query query = QueryFactory.newQuery(NodeImpl.class, c); |
260 |
|
|
261 |
|
Node nodeObj = (Node) getPersistenceBrokerTemplate().getObjectByQuery(query); |
262 |
|
if (null != nodeObj) |
263 |
|
{ |
264 |
|
proxy.setNode(nodeObj); |
265 |
|
NodeCache cn = new NodeCache(nodeObj.getFullPath(), nodeObj.getNodeType()); |
266 |
|
cn.setNode(proxy); |
267 |
|
addToCache(cn); |
268 |
|
} |
269 |
|
else |
270 |
|
{ |
271 |
|
throw new NodeDoesNotExistException("No node of type " + nodeType + "found at path: " + fullPath); |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
public boolean nodeExists(String fullPath, int nodeType) |
279 |
|
{ |
280 |
|
NodeCache key = new NodeCache(fullPath, nodeType); |
281 |
|
if (preferenceCache.isKeyInCache(key)) |
282 |
|
return true; |
283 |
|
Criteria c = new Criteria(); |
284 |
|
c.addEqualTo("fullPath", fullPath); |
285 |
|
c.addEqualTo("nodeType", new Integer(nodeType)); |
286 |
|
Query query = QueryFactory.newQuery(NodeImpl.class, c); |
287 |
|
|
288 |
|
Node nodeObj = (Node) getPersistenceBrokerTemplate().getObjectByQuery(query); |
289 |
|
if (null != nodeObj) |
290 |
|
{ |
291 |
|
NodeImplProxy proxy = new NodeImplProxy(nodeObj); |
292 |
|
addToCache(new NodeCache(proxy)); |
293 |
|
return true; |
294 |
|
} |
295 |
|
else |
296 |
|
{ |
297 |
|
return false; |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
public Node createNode(Node parent, String nodeName, int nodeType, String fullPath) |
305 |
|
throws FailedToCreateNodeException, NodeAlreadyExistsException |
306 |
|
{ |
307 |
|
if (nodeExists(fullPath, nodeType)) |
308 |
|
{ |
309 |
|
throw new NodeAlreadyExistsException("Node of type " + nodeType + " already exists at path " + fullPath); |
310 |
|
} |
311 |
|
else |
312 |
|
{ |
313 |
|
Long parentNodeId = null; |
314 |
|
if (null != parent) |
315 |
|
{ |
316 |
|
parentNodeId = new Long(parent.getNodeId()); |
317 |
|
} |
318 |
|
|
319 |
|
Node nodeObj = new NodeImpl(parentNodeId, nodeName, nodeType, fullPath); |
320 |
|
|
321 |
|
try |
322 |
|
{ |
323 |
|
getPersistenceBrokerTemplate().store(nodeObj); |
324 |
|
NodeImplProxy proxy = new NodeImplProxy(nodeObj); |
325 |
|
addToCache(new NodeCache(proxy)); |
326 |
|
return proxy; |
327 |
|
} |
328 |
|
catch (Exception e) |
329 |
|
{ |
330 |
|
throw new FailedToCreateNodeException("Failed to create node of type " + nodeType + " for the path " |
331 |
|
+ fullPath + ". " + e.toString(), e); |
332 |
|
} |
333 |
|
|
334 |
|
} |
335 |
|
} |
336 |
|
|
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
public Collection getChildren(Node parentNode) |
341 |
|
{ |
342 |
|
NodeCache key = new NodeCache(parentNode.getFullPath(), parentNode.getNodeType()); |
343 |
|
|
344 |
|
NodeCache hit = getNode(key.getCacheKey()); |
345 |
|
if (hit == null) |
346 |
|
{ |
347 |
|
NodeImplProxy proxy = new NodeImplProxy(parentNode); |
348 |
|
hit = new NodeCache(proxy); |
349 |
|
addToCache(hit); |
350 |
|
} |
351 |
|
if (hit.isChildrenLoaded()) |
352 |
|
{ |
353 |
|
return resolveChildren(hit.getChildren()); |
354 |
|
} |
355 |
|
|
356 |
|
Criteria c = new Criteria(); |
357 |
|
c.addEqualTo("parentNodeId", new Long(parentNode.getNodeId())); |
358 |
|
Query query = QueryFactory.newQuery(NodeImpl.class, c); |
359 |
|
Collection children = getPersistenceBrokerTemplate().getCollectionByQuery(query); |
360 |
|
hit.setChildren(cacheChildren(children)); |
361 |
|
|
362 |
|
return children; |
363 |
|
} |
364 |
|
|
365 |
|
|
366 |
|
private Collection resolveChildren(Collection children) |
367 |
|
{ |
368 |
|
if (children == null) |
369 |
|
return null; |
370 |
|
try |
371 |
|
{ |
372 |
|
Iterator it = children.iterator(); |
373 |
|
Vector v = new Vector(); |
374 |
|
while (it.hasNext()) |
375 |
|
{ |
376 |
|
String s = (String) it.next(); |
377 |
|
NodeCache hit =getNode(s); |
378 |
|
if (hit != null) |
379 |
|
v.add(hit.getNode()); |
380 |
|
} |
381 |
|
return v; |
382 |
|
} |
383 |
|
catch (Exception e) |
384 |
|
{ |
385 |
|
e.printStackTrace(); |
386 |
|
return null; |
387 |
|
} |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
|
private Collection cacheChildren(Collection children) |
392 |
|
{ |
393 |
|
Iterator it = children.iterator(); |
394 |
|
Vector v = new Vector(); |
395 |
|
while (it.hasNext()) |
396 |
|
{ |
397 |
|
Node key = (Node)it.next(); |
398 |
|
NodeCache nodeKey = new NodeCache(key.getFullPath(),key.getNodeType()); |
399 |
|
NodeCache hit = getNode(nodeKey.getCacheKey()); |
400 |
|
if (hit == null) |
401 |
|
{ |
402 |
|
NodeImplProxy proxy = new NodeImplProxy(key); |
403 |
|
nodeKey.setNode(proxy); |
404 |
|
addToCache(nodeKey); |
405 |
|
hit= nodeKey; |
406 |
|
} |
407 |
|
v.add(hit.getCacheKey()); |
408 |
|
} |
409 |
|
return v; |
410 |
|
} |
411 |
|
|
412 |
|
|
413 |
|
|
414 |
|
|
415 |
|
public void storeNode(Node node) |
416 |
|
{ |
417 |
|
NodeImplProxy hit = null; |
418 |
|
if (node instanceof NodeImplProxy) |
419 |
|
{ |
420 |
|
hit = (NodeImplProxy)node; |
421 |
|
} |
422 |
|
else |
423 |
|
{ |
424 |
|
|
425 |
|
hit = new NodeImplProxy(node); |
426 |
|
} |
427 |
|
|
428 |
|
NodeCache key = new NodeCache(hit); |
429 |
|
getPersistenceBrokerTemplate().store(hit.getNode()); |
430 |
|
|
431 |
|
preferenceCache.remove(key.getCacheKey()); |
432 |
|
addToCache(key); |
433 |
|
} |
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
public void removeNode(Node parentNode, Node node) |
439 |
|
{ |
440 |
|
NodeImplProxy hit = null; |
441 |
|
NodeImplProxy parentHit = null; |
442 |
|
|
443 |
|
if (node instanceof NodeImplProxy) |
444 |
|
{ |
445 |
|
getPersistenceBrokerTemplate().delete(((NodeImplProxy)node).getNode()); |
446 |
|
} |
447 |
|
else |
448 |
|
getPersistenceBrokerTemplate().delete(node); |
449 |
|
|
450 |
|
if (node instanceof NodeImplProxy) |
451 |
|
{ |
452 |
|
hit = (NodeImplProxy)node; |
453 |
|
} |
454 |
|
else |
455 |
|
{ |
456 |
|
|
457 |
|
hit = new NodeImplProxy(node); |
458 |
|
} |
459 |
|
NodeCache key = new NodeCache(hit); |
460 |
|
preferenceCache.remove(key.getCacheKey()); |
461 |
|
if ( parentNode != null ) |
462 |
|
{ |
463 |
|
if (parentNode instanceof NodeImplProxy) |
464 |
|
{ |
465 |
|
parentHit = (NodeImplProxy)parentNode; |
466 |
|
} |
467 |
|
else |
468 |
|
{ |
469 |
|
|
470 |
|
parentHit = new NodeImplProxy(parentNode); |
471 |
|
} |
472 |
|
NodeCache parentKey = new NodeCache(parentHit); |
473 |
|
parentKey = getNode(parentKey.getCacheKey()); |
474 |
|
if ( parentKey != null && parentKey.isChildrenLoaded() ) |
475 |
|
{ |
476 |
|
parentKey.getChildren().remove(key.getCacheKey()); |
477 |
|
} |
478 |
|
} |
479 |
|
} |
480 |
|
|
481 |
|
|
482 |
|
|
483 |
|
|
484 |
|
public Collection lookupPreference(String nodeName, String propertyName, String propertyValue) |
485 |
|
{ |
486 |
|
Criteria c = new Criteria(); |
487 |
|
if (nodeName != null) |
488 |
|
{ |
489 |
|
c.addEqualTo("nodeName", nodeName); |
490 |
|
} |
491 |
|
if (propertyName != null) |
492 |
|
{ |
493 |
|
c.addEqualTo("nodeProperties.propertyName", propertyName); |
494 |
|
} |
495 |
|
if (propertyValue != null) |
496 |
|
{ |
497 |
|
c.addEqualTo("nodeProperties.propertyValue", propertyValue); |
498 |
|
} |
499 |
|
Query query = QueryFactory.newQuery(NodeImpl.class, c); |
500 |
|
Collection children = getPersistenceBrokerTemplate().getCollectionByQuery(query); |
501 |
|
Collection proxied = new ArrayList(); |
502 |
|
Iterator iter = children.iterator(); |
503 |
|
while (iter.hasNext()) |
504 |
|
{ |
505 |
|
NodeImpl node = (NodeImpl)iter.next(); |
506 |
|
NodeCache key = new NodeCache(node.getFullPath(), node.getNodeType()); |
507 |
|
NodeCache hit = getNode(key.getCacheKey()); |
508 |
|
if (hit == null) |
509 |
|
{ |
510 |
|
NodeImplProxy proxy = new NodeImplProxy(node); |
511 |
|
addToCache(new NodeCache(proxy)); |
512 |
|
proxied.add(proxy); |
513 |
|
} |
514 |
|
else |
515 |
|
{ |
516 |
|
proxied.add(hit.getNode()); |
517 |
|
} |
518 |
|
} |
519 |
|
return proxied; |
520 |
|
} |
521 |
|
|
522 |
|
public Property createProperty(Node node, String name, Object value) |
523 |
|
{ |
524 |
|
return new PropertyImpl(node.getNodeId(), name, value); |
525 |
|
} |
526 |
|
|
527 |
|
} |