View Javadoc

1   /**
2    * This file Copyright (c) 2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.jcr.wrapper;
35  
36  import java.io.InputStream;
37  import java.math.BigDecimal;
38  import java.util.Calendar;
39  
40  import javax.jcr.AccessDeniedException;
41  import javax.jcr.Binary;
42  import javax.jcr.InvalidItemStateException;
43  import javax.jcr.InvalidLifecycleTransitionException;
44  import javax.jcr.Item;
45  import javax.jcr.ItemExistsException;
46  import javax.jcr.ItemNotFoundException;
47  import javax.jcr.ItemVisitor;
48  import javax.jcr.MergeException;
49  import javax.jcr.NoSuchWorkspaceException;
50  import javax.jcr.Node;
51  import javax.jcr.NodeIterator;
52  import javax.jcr.PathNotFoundException;
53  import javax.jcr.Property;
54  import javax.jcr.PropertyIterator;
55  import javax.jcr.ReferentialIntegrityException;
56  import javax.jcr.RepositoryException;
57  import javax.jcr.Session;
58  import javax.jcr.UnsupportedRepositoryOperationException;
59  import javax.jcr.Value;
60  import javax.jcr.ValueFormatException;
61  import javax.jcr.lock.Lock;
62  import javax.jcr.lock.LockException;
63  import javax.jcr.nodetype.ConstraintViolationException;
64  import javax.jcr.nodetype.NoSuchNodeTypeException;
65  import javax.jcr.nodetype.NodeDefinition;
66  import javax.jcr.nodetype.NodeType;
67  import javax.jcr.version.ActivityViolationException;
68  import javax.jcr.version.Version;
69  import javax.jcr.version.VersionException;
70  import javax.jcr.version.VersionHistory;
71  
72  /**
73   * Wrapper for JCR node.
74   *
75   * @version $Id$
76   */
77  public abstract class DelegateNodeWrapper implements Node, Cloneable {
78  
79      protected Node wrapped;
80  
81      protected DelegateNodeWrapper() {
82      }
83  
84      protected DelegateNodeWrapper(Node node) {
85          setWrappedNode(node);
86      }
87  
88      public Node getWrappedNode() {
89          return this.wrapped;
90      }
91  
92      public void setWrappedNode(Node node) {
93          this.wrapped = node;
94      }
95  
96      @Override
97      public String toString() {
98          return wrapped != null ? wrapped.toString() : "";
99      }
100 
101     /////////////
102     //
103     //  Delegating method stubs
104     //
105     /////////////
106 
107     @Override
108     public void addMixin(String mixinName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
109         getWrappedNode().addMixin(mixinName);
110     }
111 
112     @Override
113     public Node addNode(String relPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
114         return getWrappedNode().addNode(relPath);
115     }
116 
117     @Override
118     public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
119         return getWrappedNode().addNode(relPath, primaryNodeTypeName);
120     }
121 
122     @Override
123     public boolean canAddMixin(String mixinName) throws NoSuchNodeTypeException, RepositoryException {
124         return getWrappedNode().canAddMixin(mixinName);
125     }
126 
127     @Override
128     public void cancelMerge(Version version) throws VersionException, InvalidItemStateException, UnsupportedRepositoryOperationException, RepositoryException {
129         getWrappedNode().cancelMerge(version);
130     }
131 
132     @Override
133     public Version checkin() throws VersionException, UnsupportedRepositoryOperationException, InvalidItemStateException, LockException, RepositoryException {
134         return getWrappedNode().checkin();
135     }
136 
137     @Override
138     public void checkout() throws UnsupportedRepositoryOperationException, LockException, ActivityViolationException, RepositoryException {
139         getWrappedNode().checkout();
140     }
141 
142     @Override
143     public void doneMerge(Version version) throws VersionException, InvalidItemStateException, UnsupportedRepositoryOperationException, RepositoryException {
144 
145     }
146 
147     @Override
148     public void followLifecycleTransition(String transition) throws UnsupportedRepositoryOperationException, InvalidLifecycleTransitionException, RepositoryException {
149         getWrappedNode().followLifecycleTransition(transition);
150     }
151 
152     @Override
153     public String[] getAllowedLifecycleTransistions() throws UnsupportedRepositoryOperationException, RepositoryException {
154         return getWrappedNode().getAllowedLifecycleTransistions();
155     }
156 
157     @Override
158     public Version getBaseVersion() throws UnsupportedRepositoryOperationException, RepositoryException {
159         return getWrappedNode().getBaseVersion();
160     }
161 
162     @Override
163     public String getCorrespondingNodePath(String workspaceName) throws ItemNotFoundException, NoSuchWorkspaceException, AccessDeniedException, RepositoryException {
164         return getWrappedNode().getCorrespondingNodePath(workspaceName);
165     }
166 
167     @Override
168     public NodeDefinition getDefinition() throws RepositoryException {
169         return getWrappedNode().getDefinition();
170     }
171 
172     @Override
173     public String getIdentifier() throws RepositoryException {
174         return getWrappedNode().getIdentifier();
175     }
176 
177     @Override
178     public int getIndex() throws RepositoryException {
179         return getWrappedNode().getIndex();
180     }
181 
182     @Override
183     public Lock getLock() throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, RepositoryException {
184         return getWrappedNode().getLock();
185     }
186 
187     @Override
188     public NodeType[] getMixinNodeTypes() throws RepositoryException {
189         return getWrappedNode().getMixinNodeTypes();
190     }
191 
192     @Override
193     public Node getNode(String relPath) throws PathNotFoundException, RepositoryException {
194         return getWrappedNode().getNode(relPath);
195     }
196 
197     @Override
198     public NodeIterator getNodes() throws RepositoryException {
199         return getWrappedNode().getNodes();
200     }
201 
202     @Override
203     public NodeIterator getNodes(String namePattern) throws RepositoryException {
204         return getWrappedNode().getNodes(namePattern);
205     }
206 
207     @Override
208     public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
209         return getWrappedNode().getNodes(nameGlobs);
210     }
211 
212     @Override
213     public Item getPrimaryItem() throws ItemNotFoundException, RepositoryException {
214         return getWrappedNode().getPrimaryItem();
215     }
216 
217     @Override
218     public NodeType getPrimaryNodeType() throws RepositoryException {
219         return getWrappedNode().getPrimaryNodeType();
220     }
221 
222     @Override
223     public PropertyIterator getProperties() throws RepositoryException {
224         return getWrappedNode().getProperties();
225     }
226 
227     @Override
228     public PropertyIterator getProperties(String namePattern) throws RepositoryException {
229         return getWrappedNode().getProperties(namePattern);
230     }
231 
232     @Override
233     public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
234         return getWrappedNode().getProperties(nameGlobs);
235     }
236 
237     @Override
238     public Property getProperty(String relPath) throws PathNotFoundException, RepositoryException {
239         return getWrappedNode().getProperty(relPath);
240     }
241 
242     @Override
243     public PropertyIterator getReferences() throws RepositoryException {
244         return getWrappedNode().getReferences();
245     }
246 
247     @Override
248     public PropertyIterator getReferences(String name) throws RepositoryException {
249         return getWrappedNode().getReferences(name);
250     }
251 
252     @Override
253     public NodeIterator getSharedSet() throws RepositoryException {
254         return getWrappedNode().getSharedSet();
255     }
256 
257     @Override
258     public String getUUID() throws UnsupportedRepositoryOperationException, RepositoryException {
259         return getWrappedNode().getUUID();
260     }
261 
262     @Override
263     public VersionHistory getVersionHistory() throws UnsupportedRepositoryOperationException, RepositoryException {
264         return getWrappedNode().getVersionHistory();
265     }
266 
267     @Override
268     public PropertyIterator getWeakReferences() throws RepositoryException {
269         return getWrappedNode().getWeakReferences();
270     }
271 
272     @Override
273     public PropertyIterator getWeakReferences(String name) throws RepositoryException {
274         return getWrappedNode().getWeakReferences(name);
275     }
276 
277     @Override
278     public boolean hasNode(String relPath) throws RepositoryException {
279         return getWrappedNode().hasNode(relPath);
280     }
281 
282     @Override
283     public boolean hasNodes() throws RepositoryException {
284         return getWrappedNode().hasNodes();
285     }
286 
287     @Override
288     public boolean hasProperties() throws RepositoryException {
289         return getWrappedNode().hasProperties();
290     }
291 
292     @Override
293     public boolean hasProperty(String relPath) throws RepositoryException {
294         return getWrappedNode().hasProperty(relPath);
295     }
296 
297     @Override
298     public boolean holdsLock() throws RepositoryException {
299         return getWrappedNode().holdsLock();
300     }
301 
302     @Override
303     public boolean isCheckedOut() throws RepositoryException {
304         return getWrappedNode().isCheckedOut();
305     }
306 
307     @Override
308     public boolean isLocked() throws RepositoryException {
309         return getWrappedNode().isLocked();
310     }
311 
312     @Override
313     public boolean isNodeType(String nodeTypeName) throws RepositoryException {
314         return getWrappedNode().isNodeType(nodeTypeName);
315     }
316 
317     @Override
318     public Lock lock(boolean isDeep, boolean isSessionScoped) throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, InvalidItemStateException, RepositoryException {
319         return getWrappedNode().lock(isDeep, isSessionScoped);
320     }
321 
322     @Override
323     public NodeIterator merge(String srcWorkspace, boolean bestEffort) throws NoSuchWorkspaceException, AccessDeniedException, MergeException, LockException, InvalidItemStateException, RepositoryException {
324         return getWrappedNode().merge(srcWorkspace, bestEffort);
325     }
326 
327     @Override
328     public void orderBefore(String srcChildRelPath, String destChildRelPath) throws UnsupportedRepositoryOperationException, VersionException, ConstraintViolationException, ItemNotFoundException, LockException, RepositoryException {
329         getWrappedNode().orderBefore(srcChildRelPath, destChildRelPath);
330     }
331 
332     @Override
333     public void removeMixin(String mixinName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
334         getWrappedNode().removeMixin(mixinName);
335     }
336 
337     @Override
338     public void removeShare() throws VersionException, LockException, ConstraintViolationException, RepositoryException {
339         getWrappedNode().removeShare();
340     }
341 
342     @Override
343     public void removeSharedSet() throws VersionException, LockException, ConstraintViolationException, RepositoryException {
344         getWrappedNode().removeSharedSet();
345     }
346 
347     @Override
348     public void restore(String versionName, boolean removeExisting) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
349         getWrappedNode().restore(versionName, removeExisting);
350     }
351 
352     @Override
353     public void restore(Version version, boolean removeExisting) throws VersionException, ItemExistsException, InvalidItemStateException, UnsupportedRepositoryOperationException, LockException, RepositoryException {
354         getWrappedNode().restore(version, removeExisting);
355     }
356 
357     @Override
358     public void restore(Version version, String relPath, boolean removeExisting) throws PathNotFoundException, ItemExistsException, VersionException, ConstraintViolationException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
359         getWrappedNode().restore(version, relPath, removeExisting);
360     }
361 
362     @Override
363     public void restoreByLabel(String versionLabel, boolean removeExisting) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
364         getWrappedNode().restoreByLabel(versionLabel, removeExisting);
365     }
366 
367     @Override
368     public void setPrimaryType(String nodeTypeName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
369         getWrappedNode().setPrimaryType(nodeTypeName);
370     }
371 
372     @Override
373     public Property setProperty(String name, Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
374         return getWrappedNode().setProperty(name, value);
375     }
376 
377     @Override
378     public Property setProperty(String name, Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
379         return getWrappedNode().setProperty(name, values);
380     }
381 
382     @Override
383     public Property setProperty(String name, String[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
384         return getWrappedNode().setProperty(name, values);
385     }
386 
387     @Override
388     public Property setProperty(String name, String value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
389         return getWrappedNode().setProperty(name, value);
390     }
391 
392     @Override
393     public Property setProperty(String name, InputStream value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
394         return getWrappedNode().setProperty(name, value);
395     }
396 
397     @Override
398     public Property setProperty(String name, Binary value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
399         return getWrappedNode().setProperty(name, value);
400     }
401 
402     @Override
403     public Property setProperty(String name, boolean value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
404         return getWrappedNode().setProperty(name, value);
405     }
406 
407     @Override
408     public Property setProperty(String name, double value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
409         return getWrappedNode().setProperty(name, value);
410     }
411 
412     @Override
413     public Property setProperty(String name, BigDecimal value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
414         return getWrappedNode().setProperty(name, value);
415     }
416 
417     @Override
418     public Property setProperty(String name, long value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
419         return getWrappedNode().setProperty(name, value);
420     }
421 
422     @Override
423     public Property setProperty(String name, Calendar value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
424         return getWrappedNode().setProperty(name, value);
425     }
426 
427     @Override
428     public Property setProperty(String name, Node value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
429         return getWrappedNode().setProperty(name, value);
430     }
431 
432     @Override
433     public Property setProperty(String name, Value value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
434         return getWrappedNode().setProperty(name, value, type);
435     }
436 
437     @Override
438     public Property setProperty(String name, Value[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
439         return getWrappedNode().setProperty(name, values, type);
440     }
441 
442     @Override
443     public Property setProperty(String name, String[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
444         return getWrappedNode().setProperty(name, values, type);
445     }
446 
447     @Override
448     public Property setProperty(String name, String value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
449         return getWrappedNode().setProperty(name, value, type);
450     }
451 
452     @Override
453     public void unlock() throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, InvalidItemStateException, RepositoryException {
454         getWrappedNode().unlock();
455     }
456 
457     @Override
458     public void update(String srcWorkspace) throws NoSuchWorkspaceException, AccessDeniedException, LockException, InvalidItemStateException, RepositoryException {
459         getWrappedNode().update(srcWorkspace);
460     }
461 
462     @Override
463     public void accept(ItemVisitor visitor) throws RepositoryException {
464         getWrappedNode().accept(visitor);
465     }
466 
467     @Override
468     public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
469         return getWrappedNode().getAncestor(depth);
470     }
471 
472     @Override
473     public int getDepth() throws RepositoryException {
474         return getWrappedNode().getDepth();
475     }
476 
477     @Override
478     public String getName() throws RepositoryException {
479         return getWrappedNode().getName();
480     }
481 
482     @Override
483     public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
484         return getWrappedNode().getParent();
485     }
486 
487     @Override
488     public String getPath() throws RepositoryException {
489         return getWrappedNode().getPath();
490     }
491 
492     @Override
493     public Session getSession() throws RepositoryException {
494         return getWrappedNode().getSession();
495     }
496 
497     @Override
498     public boolean isModified() {
499         return getWrappedNode().isModified();
500     }
501 
502     @Override
503     public boolean isNew() {
504         return getWrappedNode().isNew();
505     }
506 
507     @Override
508     public boolean isNode() {
509         return getWrappedNode().isNode();
510     }
511 
512     @Override
513     public boolean isSame(Item otherItem) throws RepositoryException {
514         return getWrappedNode().isSame(otherItem);
515     }
516 
517     @Override
518     public void refresh(boolean keepChanges) throws InvalidItemStateException, RepositoryException {
519         getWrappedNode().refresh(keepChanges);
520     }
521 
522     @Override
523     public void remove() throws VersionException, LockException, ConstraintViolationException, AccessDeniedException, RepositoryException {
524         getWrappedNode().remove();
525     }
526 
527     @Override
528     public void save() throws AccessDeniedException, ItemExistsException, ConstraintViolationException, InvalidItemStateException, ReferentialIntegrityException, VersionException, LockException, NoSuchNodeTypeException, RepositoryException {
529         getWrappedNode().save();
530     }
531 
532     /**
533      * Removes a wrapper by type. The wrapper can be deep in a chain of wrappers in which case wrappers before it will
534      * be cloned creating a new chain that leads to the same real node.
535      */
536     public Node deepUnwrap(Class<? extends DelegateNodeWrapper> wrapper)  {
537 
538         if (this.getClass().equals(wrapper)) {
539             return getWrappedNode();
540         }
541 
542         Node next = getWrappedNode();
543 
544         // If the next node is the real node then we can skip cloning ourselves.
545         // This happens when the wrapper to remove isn't present
546         if (!(next instanceof DelegateNodeWrapper)) {
547             return this;
548         }
549 
550         // We let the next wrapper do deepUnwrap first, if it returns itself then the wrapper isn't in the chain and cloning is not necessary
551         Node deepUnwrappedNext = ((DelegateNodeWrapper) next).deepUnwrap(wrapper);
552         if (deepUnwrappedNext == next) {
553             return this;
554         }
555 
556         try {
557             DelegateNodeWrapper clone = ((DelegateNodeWrapper) this.clone());
558             clone.initClone(deepUnwrappedNext);
559             return clone;
560         } catch (CloneNotSupportedException e) {
561             throw new RuntimeException("Failed to unwrap " + this.getClass().getName() + " due to " + e.getMessage(), e);
562         }
563     }
564 
565     protected void initClone(Node newNode) {
566         setWrappedNode(newNode);
567     }
568 
569     @Override
570     protected Object clone() throws CloneNotSupportedException {
571         // just shallow clone ... keep it that way at least for wrappedNode otherwise deepUnwrap generates zillions of objects unnecessarily
572         return super.clone();
573     }
574 
575 }