1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.jcr.wrapper;
35
36 import javax.jcr.Node;
37 import javax.jcr.RepositoryException;
38 import javax.jcr.Session;
39
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.jcr.RuntimeRepositoryException;
42
43
44
45
46
47
48
49 public class LazyNodeWrapper extends DelegateNodeWrapper {
50
51 private String workspace;
52 private String nodeIdentifier;
53 private transient Node node;
54
55 public LazyNodeWrapper(String workspace, String nodeIdentifier) {
56 this.workspace = workspace;
57 this.nodeIdentifier = nodeIdentifier;
58 }
59
60 public LazyNodeWrapper(Node node) throws RepositoryException {
61 this.workspace = node.getSession().getWorkspace().getName();
62 this.nodeIdentifier = node.getIdentifier();
63 this.node = node;
64 }
65
66 @Override
67 public synchronized Node getWrappedNode() {
68 try {
69 if (node == null || !node.getSession().isLive()) {
70 Session session = getSessionForWrappedNode(this.workspace);
71 node = session.getNodeByIdentifier(this.nodeIdentifier);
72 }
73 }
74 catch (RepositoryException e) {
75 throw new RuntimeException(e);
76 }
77 return node;
78 }
79
80 @Override
81 public void setWrappedNode(Node node) {
82 try {
83 this.workspace = node.getSession().getWorkspace().getName();
84 this.nodeIdentifier = node.getIdentifier();
85 this.node = node;
86 } catch (RepositoryException e) {
87 throw new RuntimeRepositoryException(e);
88 }
89 }
90
91 protected Session getSessionForWrappedNode(String workspace) throws RepositoryException {
92 return MgnlContext.getSystemContext().getJCRSession(workspace);
93 }
94 }