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