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.exception.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() throws RepositoryException {
68 if (node == null || !node.getSession().isLive()) {
69 Session session = getSessionForWrappedNode(this.workspace);
70 node = session.getNodeByIdentifier(this.nodeIdentifier);
71 }
72 return node;
73 }
74
75 @Override
76 public void setWrappedNode(Node node) {
77 try {
78 this.workspace = node.getSession().getWorkspace().getName();
79 this.nodeIdentifier = node.getIdentifier();
80 this.node = node;
81 } catch (RepositoryException e) {
82 throw new RuntimeRepositoryException(e);
83 }
84 }
85
86 protected Session getSessionForWrappedNode(String workspace) throws RepositoryException {
87 return MgnlContext.getSystemContext().getJCRSession(workspace);
88 }
89 }