1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.operations.extended;
21  
22  import javax.naming.Name;
23  import javax.naming.NamingException;
24  
25  import org.apache.directory.server.core.CoreSession;
26  import org.apache.directory.server.core.entry.ClonedServerEntry;
27  import org.apache.directory.server.core.filtering.EntryFilteringCursor;
28  import org.apache.directory.shared.ldap.message.AliasDerefMode;
29  import org.apache.directory.shared.ldap.name.LdapDN;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  public class DITUtilitiesSP
35  {
36      private static final Logger log = LoggerFactory.getLogger( DITUtilitiesSP.class );
37      
38      /**
39       * Recursively deletes a subtree including the apex given.
40       * 
41       * If you do not want to wait for the developers to implement the
42       * following RFC
43       * http://kahuna.telstra.net/ietf/all-ids/draft-armijo-ldap-treedelete-02.txt
44       * you can do it yourself!
45       * 
46       * @param ctx an LDAP context to perform operations on
47       * @param rdn ctx relative name of the entry which is root of
48       *        the subtree to be deleted
49       * @throws NamingException
50       */
51      public static void deleteSubtree( CoreSession session, Name rdn ) throws Exception
52      {
53          EntryFilteringCursor results = session.list( (LdapDN)rdn, AliasDerefMode.DEREF_ALWAYS, null );
54          
55          results.beforeFirst();
56          
57          while ( results.next() )
58          {
59              ClonedServerEntry result = results.get();
60              Name childRdn = result.getDn();
61              childRdn.remove( 0 );
62              deleteSubtree( session, childRdn );
63          }
64          
65          session.delete( (LdapDN)rdn );
66          log.info( "Deleted: " + rdn );
67      }
68  }