View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr;
25  
26  import javax.jcr.AccessDeniedException;
27  import javax.jcr.ItemNotFoundException;
28  import javax.jcr.Node;
29  import javax.jcr.RepositoryException;
30  import javax.jcr.lock.Lock;
31  import javax.jcr.lock.LockException;
32  import javax.jcr.nodetype.ConstraintViolationException;
33  import javax.jcr.version.VersionException;
34  import net.jcip.annotations.NotThreadSafe;
35  import org.modeshape.graph.Location;
36  import org.modeshape.graph.session.GraphSession.NodeId;
37  
38  /**
39   * A concrete {@link Node JCR Node} implementation.
40   * 
41   * @see JcrRootNode
42   * @see JcrSharedNode
43   */
44  @NotThreadSafe
45  class JcrNode extends AbstractJcrNode {
46  
47      JcrNode( SessionCache cache,
48               NodeId nodeId,
49               Location location ) {
50          super(cache, nodeId, location);
51      }
52  
53      /**
54       * {@inheritDoc}
55       * 
56       * @see org.modeshape.jcr.AbstractJcrNode#isRoot()
57       */
58      @Override
59      final boolean isRoot() {
60          return false;
61      }
62  
63      /**
64       * {@inheritDoc}
65       * 
66       * @see javax.jcr.Node#getIndex()
67       */
68      public int getIndex() throws RepositoryException {
69          return segment().getIndex();
70      }
71  
72      /**
73       * {@inheritDoc}
74       * 
75       * @see javax.jcr.Item#getName()
76       */
77      public String getName() throws RepositoryException {
78          return segment().getName().getString(namespaces());
79      }
80  
81      /**
82       * {@inheritDoc}
83       * 
84       * @see javax.jcr.Item#getParent()
85       */
86      @Override
87      public AbstractJcrNode getParent() throws ItemNotFoundException, RepositoryException {
88          checkSession();
89          return parentNodeInfo().getPayload().getJcrNode();
90      }
91  
92      /**
93       * {@inheritDoc}
94       * 
95       * @see javax.jcr.Item#getPath()
96       */
97      public String getPath() throws RepositoryException {
98          // checkSession(); ideally we don't have to do this, because getting the path is a useful thing and is used in 'toString'
99          return path().getString(namespaces());
100     }
101 
102     /**
103      * {@inheritDoc}
104      * 
105      * @see org.modeshape.jcr.AbstractJcrNode#doRemove()
106      */
107     @Override
108     protected void doRemove() throws RepositoryException, LockException {
109         Node parentNode = getParent();
110         if (parentNode.isLocked()) {
111             Lock parentLock = lockManager().getLock(this);
112             if (parentLock != null && !parentLock.isLockOwningSession()) {
113                 throw new LockException(JcrI18n.lockTokenNotHeld.text(this.location));
114             }
115         }
116 
117         if (!parentNode.isCheckedOut()) {
118             throw new VersionException(JcrI18n.nodeIsCheckedIn.text(parentNode.getPath()));
119         }
120 
121         JcrNodeDefinition nodeDefn = cache.nodeTypes().getNodeDefinition(nodeInfo().getPayload().getDefinitionId());
122 
123         if (nodeDefn.isProtected()) {
124             throw new ConstraintViolationException(JcrI18n.cannotRemoveItemWithProtectedDefinition.text(getPath()));
125         }
126         session().recordRemoval(locationToDestroy()); // do this first before we destroy the node!
127         doDestroy();
128     }
129 
130     protected Location locationToDestroy() {
131         return location;
132     }
133 
134     protected void doDestroy() throws AccessDeniedException, RepositoryException {
135         editor().destroy();
136     }
137 
138 }