View Javadoc

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.ldap.handlers;
21  
22  
23  import org.apache.directory.server.core.event.DirectoryListener;
24  import org.apache.directory.server.core.filtering.EntryFilteringCursor;
25  import org.apache.directory.server.ldap.LdapService;
26  import org.apache.directory.shared.ldap.exception.OperationAbandonedException;
27  import org.apache.directory.shared.ldap.message.AbandonListener;
28  import org.apache.directory.shared.ldap.message.AbandonableRequest;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * An AbandonListener implementation which closes an associated cursor or 
35   * removes a DirectoryListener.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
39   */
40  public class SearchAbandonListener implements AbandonListener
41  {
42      private static final Logger LOG = LoggerFactory.getLogger( SearchAbandonListener.class );
43      private final LdapService ldapService;
44      private EntryFilteringCursor cursor;
45      private DirectoryListener listener;
46      
47      
48      public SearchAbandonListener( LdapService ldapService, EntryFilteringCursor cursor, DirectoryListener listener )
49      {
50          if ( ldapService == null )
51          {
52              throw new NullPointerException( "ldapService" );
53          }
54          
55          this.ldapService = ldapService;
56          this.cursor = cursor;
57          this.listener = listener;
58      }
59      
60      
61      public SearchAbandonListener( LdapService ldapService, DirectoryListener listener )
62      {
63          this ( ldapService, null, listener );
64      }
65      
66      
67      public SearchAbandonListener( LdapService ldapService, EntryFilteringCursor cursor )
68      {
69          this ( ldapService, cursor, null );
70      }
71      
72      
73      public void requestAbandoned( AbandonableRequest req )
74      {
75          if ( listener != null )
76          {
77              ldapService.getDirectoryService().getEventService().removeListener( listener );
78          }
79  
80          try
81          {
82              if ( cursor != null )
83              {
84                  /*
85                   * When this method is called due to an abandon request it 
86                   * will close the cursor but other threads processing the 
87                   * search will get an OperationAbandonedException which as
88                   * seen below will make sure the proper handling is 
89                   * performed.
90                   */
91                  cursor.close( new OperationAbandonedException() );
92              }
93          }
94          catch ( Exception e )
95          {
96              LOG.error( "Failed to close the search cursor for message {} on abandon request.", 
97                  req.getMessageId(), e );
98          }
99      }
100 }
101 
102