1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.jcr;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import javax.jcr.AccessDeniedException;
29 import javax.jcr.InvalidItemStateException;
30 import javax.jcr.Property;
31 import javax.jcr.PropertyIterator;
32 import javax.jcr.RepositoryException;
33 import javax.jcr.nodetype.ConstraintViolationException;
34 import net.jcip.annotations.NotThreadSafe;
35 import org.modeshape.graph.Location;
36 import org.modeshape.graph.property.Path;
37 import org.modeshape.graph.session.GraphSession.Node;
38 import org.modeshape.jcr.SessionCache.JcrNodePayload;
39 import org.modeshape.jcr.SessionCache.JcrPropertyPayload;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 @NotThreadSafe
56 class JcrSharedNode extends JcrNode {
57
58
59 private AbstractJcrNode original;
60 private AbstractJcrNode proxy;
61
62 JcrSharedNode( AbstractJcrNode proxy,
63 AbstractJcrNode original ) {
64
65
66 super(proxy.cache, original.nodeId, original.location);
67 this.proxy = proxy;
68 this.original = original;
69 assert proxy.cache == original.cache : "Only able to share nodes within the same cache";
70 assert !proxy.isRoot() : "The root node can never be a shared node";
71 assert !original.isRoot() : "The root node can never be shareable";
72 }
73
74
75
76
77
78
79
80 AbstractJcrNode proxyNode() {
81 return proxy;
82 }
83
84
85
86
87
88
89 AbstractJcrNode originalNode() {
90 return original;
91 }
92
93
94
95
96
97
98 @Override
99 boolean isShareable() {
100 return original != null;
101 }
102
103
104
105
106
107
108 @Override
109 boolean isShared() {
110 return true;
111 }
112
113
114
115
116
117
118 @Override
119 void setLocation( Location location ) {
120 proxyNode().setLocation(location);
121 }
122
123 @Override
124 protected Location locationToDestroy() {
125 return proxyNode().location();
126 }
127
128 @Override
129 protected void doDestroy() throws AccessDeniedException, RepositoryException {
130 proxyNode().editor().destroy();
131 }
132
133 @Override
134 Node<JcrNodePayload, JcrPropertyPayload> nodeInfo()
135 throws InvalidItemStateException, AccessDeniedException, RepositoryException {
136 return original.nodeInfo();
137 }
138
139 Node<JcrNodePayload, JcrPropertyPayload> proxyInfo()
140 throws InvalidItemStateException, AccessDeniedException, RepositoryException {
141 return proxy.nodeInfo();
142 }
143
144
145
146
147
148
149
150
151
152 @Override
153 Node<JcrNodePayload, JcrPropertyPayload> parentNodeInfo()
154 throws InvalidItemStateException, AccessDeniedException, RepositoryException {
155 return proxyInfo().getParent();
156 }
157
158
159
160
161
162
163
164
165
166 @Override
167 Path path() throws RepositoryException {
168 return proxyInfo().getPath();
169 }
170
171
172
173
174
175
176
177
178
179 @Override
180 Path.Segment segment() throws RepositoryException {
181 return proxyInfo().getSegment();
182 }
183
184
185
186
187
188
189 @Override
190 protected CorrespondenceId getCorrespondenceId() throws RepositoryException {
191 return original.getCorrespondenceId();
192 }
193
194
195
196
197
198
199 @Override
200 public void removeMixin( String mixinName ) throws RepositoryException {
201 if (cache.stringFactory().create(JcrMixLexicon.SHAREABLE).equals(mixinName)) {
202
203
204
205
206 throw new ConstraintViolationException();
207 }
208 super.removeMixin(mixinName);
209 }
210
211
212
213
214
215
216 @Override
217 public Property getProperty( String relativePath ) throws RepositoryException {
218 return adapt(super.getProperty(relativePath));
219 }
220
221
222
223
224
225
226 @Override
227 public PropertyIterator getProperties() throws RepositoryException {
228 return adapt(super.getProperties());
229 }
230
231
232
233
234
235
236 @Override
237 public PropertyIterator getProperties( String namePattern ) throws RepositoryException {
238 return adapt(super.getProperties(namePattern));
239 }
240
241
242
243
244
245
246 @Override
247 public PropertyIterator getProperties( String[] nameGlobs ) throws RepositoryException {
248 return adapt(super.getProperties(nameGlobs));
249 }
250
251
252
253
254
255
256
257 protected Property adapt( Property property ) {
258 if (property instanceof JcrSingleValueProperty) {
259 JcrSingleValueProperty original = (JcrSingleValueProperty)property;
260 return new JcrSingleValueProperty(cache, this, original.name());
261 }
262 if (property instanceof JcrMultiValueProperty) {
263 JcrMultiValueProperty original = (JcrMultiValueProperty)property;
264 return new JcrMultiValueProperty(cache, this, original.name());
265 }
266 return property;
267 }
268
269 protected PropertyIterator adapt( PropertyIterator propertyIter ) {
270 Collection<Property> props = new ArrayList<Property>((int)propertyIter.getSize());
271 while (propertyIter.hasNext()) {
272 props.add(adapt(propertyIter.nextProperty()));
273 }
274 return new JcrPropertyIterator(props);
275 }
276 }