View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 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.IOException;
37  import java.io.InputStream;
38  import java.io.OutputStream;
39  import java.security.AccessControlException;
40  
41  import javax.jcr.AccessDeniedException;
42  import javax.jcr.Credentials;
43  import javax.jcr.InvalidItemStateException;
44  import javax.jcr.InvalidSerializedDataException;
45  import javax.jcr.Item;
46  import javax.jcr.ItemExistsException;
47  import javax.jcr.ItemNotFoundException;
48  import javax.jcr.LoginException;
49  import javax.jcr.NamespaceException;
50  import javax.jcr.Node;
51  import javax.jcr.PathNotFoundException;
52  import javax.jcr.Property;
53  import javax.jcr.ReferentialIntegrityException;
54  import javax.jcr.Repository;
55  import javax.jcr.RepositoryException;
56  import javax.jcr.Session;
57  import javax.jcr.UnsupportedRepositoryOperationException;
58  import javax.jcr.ValueFactory;
59  import javax.jcr.Workspace;
60  import javax.jcr.lock.LockException;
61  import javax.jcr.nodetype.ConstraintViolationException;
62  import javax.jcr.nodetype.NoSuchNodeTypeException;
63  import javax.jcr.retention.RetentionManager;
64  import javax.jcr.security.AccessControlManager;
65  import javax.jcr.version.VersionException;
66  
67  import org.xml.sax.ContentHandler;
68  import org.xml.sax.SAXException;
69  
70  /**
71   * Wrapper for JCR Session.
72   *
73   * @version $Id$
74   */
75  public abstract class DelegateSessionWrapper implements Session, Cloneable {
76  
77      protected Session wrapped;
78  
79      protected DelegateSessionWrapper(Session wrapped) {
80          this.wrapped = wrapped;
81      }
82  
83      public Session getWrappedSession() {
84          return wrapped;
85      }
86  
87      public void setWrappedSession(Session session) {
88          this.wrapped = session;
89      }
90  
91      @Override
92      public String toString() {
93          return wrapped != null ? wrapped.toString() : "";
94      }
95  
96      /////////////
97      //
98      //  Delegating method stubs
99      //
100     /////////////
101 
102     @Override
103     public void addLockToken(String lt) {
104         getWrappedSession().addLockToken(lt);
105     }
106 
107     @Override
108     public void checkPermission(String absPath, String actions) throws AccessControlException, RepositoryException {
109         getWrappedSession().checkPermission(absPath, actions);
110     }
111 
112     @Override
113     public void exportDocumentView(String absPath, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException {
114         getWrappedSession().exportDocumentView(absPath, contentHandler, skipBinary, noRecurse);
115     }
116 
117     @Override
118     public void exportDocumentView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse) throws IOException, PathNotFoundException, RepositoryException {
119         getWrappedSession().exportDocumentView(absPath, out, skipBinary, noRecurse);
120     }
121 
122     @Override
123     public void exportSystemView(String absPath, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException {
124         getWrappedSession().exportSystemView(absPath, contentHandler, skipBinary, noRecurse);
125     }
126 
127     @Override
128     public void exportSystemView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse) throws IOException, PathNotFoundException, RepositoryException {
129         getWrappedSession().exportSystemView(absPath, out, skipBinary, noRecurse);
130     }
131 
132     @Override
133     public AccessControlManager getAccessControlManager() throws UnsupportedRepositoryOperationException, RepositoryException {
134         return getWrappedSession().getAccessControlManager();
135     }
136 
137     @Override
138     public Object getAttribute(String name) {
139         return getWrappedSession().getAttribute(name);
140     }
141 
142     @Override
143     public String[] getAttributeNames() {
144         return getWrappedSession().getAttributeNames();
145     }
146 
147     @Override
148     public ContentHandler getImportContentHandler(String parentAbsPath, int uuidBehavior) throws PathNotFoundException, ConstraintViolationException, VersionException, LockException, RepositoryException {
149         return getWrappedSession().getImportContentHandler(parentAbsPath, uuidBehavior);
150     }
151 
152     @Override
153     public Item getItem(String absPath) throws PathNotFoundException, RepositoryException {
154         return getWrappedSession().getItem(absPath);
155     }
156 
157     @Override
158     public String[] getLockTokens() {
159         return getWrappedSession().getLockTokens();
160     }
161 
162     @Override
163     public String getNamespacePrefix(String uri) throws NamespaceException, RepositoryException {
164         return getWrappedSession().getNamespacePrefix(uri);
165     }
166 
167     @Override
168     public String[] getNamespacePrefixes() throws RepositoryException {
169         return getWrappedSession().getNamespacePrefixes();
170     }
171 
172     @Override
173     public String getNamespaceURI(String prefix) throws NamespaceException, RepositoryException {
174         return getWrappedSession().getNamespaceURI(prefix);
175     }
176 
177     @Override
178     public Node getNode(String absPath) throws PathNotFoundException, RepositoryException {
179         return getWrappedSession().getNode(absPath);
180     }
181 
182     @Override
183     public Node getNodeByIdentifier(String id) throws ItemNotFoundException, RepositoryException {
184         return getWrappedSession().getNodeByIdentifier(id);
185     }
186 
187     @Override
188     public Node getNodeByUUID(String uuid) throws ItemNotFoundException, RepositoryException {
189         return getWrappedSession().getNodeByUUID(uuid);
190     }
191 
192     @Override
193     public Property getProperty(String absPath) throws PathNotFoundException, RepositoryException {
194         return getWrappedSession().getProperty(absPath);
195     }
196 
197     @Override
198     public Repository getRepository() {
199         return getWrappedSession().getRepository();
200     }
201 
202     @Override
203     public RetentionManager getRetentionManager() throws UnsupportedRepositoryOperationException, RepositoryException {
204         return getWrappedSession().getRetentionManager();
205     }
206 
207     @Override
208     public Node getRootNode() throws RepositoryException {
209         return getWrappedSession().getRootNode();
210     }
211 
212     @Override
213     public String getUserID() {
214         return getWrappedSession().getUserID();
215     }
216 
217     @Override
218     public ValueFactory getValueFactory() throws UnsupportedRepositoryOperationException, RepositoryException {
219         return getWrappedSession().getValueFactory();
220     }
221 
222     @Override
223     public Workspace getWorkspace() {
224         return getWrappedSession().getWorkspace();
225     }
226 
227     @Override
228     public boolean hasCapability(String methodName, Object target, Object[] arguments) throws RepositoryException {
229         return getWrappedSession().hasCapability(methodName, target, arguments);
230     }
231 
232     @Override
233     public boolean hasPendingChanges() throws RepositoryException {
234         return getWrappedSession().hasPendingChanges();
235     }
236 
237     @Override
238     public boolean hasPermission(String absPath, String actions) throws RepositoryException {
239         return getWrappedSession().hasPermission(absPath, actions);
240     }
241 
242     @Override
243     public Session impersonate(Credentials credentials) throws LoginException, RepositoryException {
244         return getWrappedSession().impersonate(credentials);
245     }
246 
247     @Override
248     public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException, InvalidSerializedDataException, LockException, RepositoryException {
249         getWrappedSession().importXML(parentAbsPath, in, uuidBehavior);
250     }
251 
252     @Override
253     public boolean isLive() {
254         return getWrappedSession().isLive();
255     }
256 
257     @Override
258     public boolean itemExists(String absPath) throws RepositoryException {
259         return getWrappedSession().itemExists(absPath);
260     }
261 
262     @Override
263     public void logout() {
264         getWrappedSession().logout();
265     }
266 
267     @Override
268     public void move(String srcAbsPath, String destAbsPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
269         getWrappedSession().move(srcAbsPath, destAbsPath);
270     }
271 
272     @Override
273     public boolean nodeExists(String absPath) throws RepositoryException {
274         return getWrappedSession().nodeExists(absPath);
275     }
276 
277     @Override
278     public boolean propertyExists(String absPath) throws RepositoryException {
279         return getWrappedSession().propertyExists(absPath);
280     }
281 
282     @Override
283     public void refresh(boolean keepChanges) throws RepositoryException {
284         getWrappedSession().refresh(keepChanges);
285     }
286 
287     @Override
288     public void removeItem(String absPath) throws VersionException, LockException, ConstraintViolationException, AccessDeniedException, RepositoryException {
289         getWrappedSession().removeItem(absPath);
290     }
291 
292     @Override
293     public void removeLockToken(String lt) {
294         getWrappedSession().removeLockToken(lt);
295     }
296 
297     @Override
298     public void save() throws AccessDeniedException, ItemExistsException, ReferentialIntegrityException, ConstraintViolationException, InvalidItemStateException, VersionException, LockException, NoSuchNodeTypeException, RepositoryException {
299         getWrappedSession().save();
300     }
301 
302     @Override
303     public void setNamespacePrefix(String prefix, String uri) throws NamespaceException, RepositoryException {
304         getWrappedSession().setNamespacePrefix(prefix, uri);
305     }
306 
307     /**
308      * @return the unwrapped proper JCRSession
309      */
310     public Session unwrap() {
311         Session session = getWrappedSession();
312         if (session instanceof DelegateSessionWrapper) {
313             session = ((DelegateSessionWrapper) session).unwrap();
314         }
315         return session;
316     }
317 
318     /**
319      * Removes a wrapper by type. The wrapper can be deep in a chain of wrappers in which case wrappers before it will
320      * be cloned creating a new chain that leads to the same real node.
321      */
322     public Session deepUnwrap(Class<? extends DelegateSessionWrapper> wrapper) {
323 
324         if (this.getClass().equals(wrapper)) {
325             return getWrappedSession();
326         }
327 
328         Session next = getWrappedSession();
329 
330         // If the next session is the real node then we can skip cloning ourselves.
331         // This happens when the wrapper to remove isn't present
332         if (!(next instanceof DelegateSessionWrapper)) {
333             return this;
334         }
335 
336         // 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
337         Session deepUnwrappedNext = ((DelegateSessionWrapper) next).deepUnwrap(wrapper);
338         if (deepUnwrappedNext == next) {
339             return this;
340         }
341 
342         try {
343             DelegateSessionWrapper clone = ((DelegateSessionWrapper) this.clone());
344             clone.initClone(deepUnwrappedNext);
345             return clone;
346         } catch (CloneNotSupportedException e) {
347             throw new RuntimeException("Failed to unwrap " + this.getClass().getName() + " due to " + e.getMessage(), e);
348         }
349     }
350 
351     protected void initClone(Session newSession) {
352         setWrappedSession(newSession);
353     }
354 
355     @Override
356     protected Object clone() throws CloneNotSupportedException {
357         // just shallow clone ... keep it that way at least for wrappedSession otherwise deepUnwrap generates zillions of objects unnecessarily
358         return super.clone();
359     }
360 
361     @Override
362     public boolean equals(Object obj) {
363         if (obj == this) {
364             return true;
365         }
366         if (obj == null || !(obj instanceof DelegateSessionWrapper)) {
367             return false;
368         }
369         DelegateSessionWrapper that = (DelegateSessionWrapper) obj;
370         return this.getClass().equals(that.getClass()) && this.wrapped.equals(that.wrapped);
371     }
372 
373 }