001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.ldap.handlers;
021
022
023 import org.apache.directory.server.core.event.DirectoryListener;
024 import org.apache.directory.server.core.filtering.EntryFilteringCursor;
025 import org.apache.directory.server.ldap.LdapServer;
026 import org.apache.directory.shared.ldap.exception.OperationAbandonedException;
027 import org.apache.directory.shared.ldap.message.AbandonListener;
028 import org.apache.directory.shared.ldap.message.InternalAbandonableRequest;
029 import org.slf4j.Logger;
030 import org.slf4j.LoggerFactory;
031
032
033 /**
034 * An AbandonListener implementation which closes an associated cursor or
035 * removes a DirectoryListener.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev$, $Date$
039 */
040 public class SearchAbandonListener implements AbandonListener
041 {
042 private static final Logger LOG = LoggerFactory.getLogger( SearchAbandonListener.class );
043 private final LdapServer ldapServer;
044 private EntryFilteringCursor cursor;
045 private DirectoryListener listener;
046
047
048 public SearchAbandonListener( LdapServer ldapServer, EntryFilteringCursor cursor, DirectoryListener listener )
049 {
050 if ( ldapServer == null )
051 {
052 throw new NullPointerException( "ldapServer" );
053 }
054
055 this.ldapServer = ldapServer;
056 this.cursor = cursor;
057 this.listener = listener;
058 }
059
060
061 public SearchAbandonListener( LdapServer ldapServer, DirectoryListener listener )
062 {
063 this ( ldapServer, null, listener );
064 }
065
066
067 public SearchAbandonListener( LdapServer ldapServer, EntryFilteringCursor cursor )
068 {
069 this ( ldapServer, cursor, null );
070 }
071
072
073 public void requestAbandoned( InternalAbandonableRequest req )
074 {
075 if ( listener != null )
076 {
077 ldapServer.getDirectoryService().getEventService().removeListener( listener );
078 }
079
080 try
081 {
082 if ( cursor != null )
083 {
084 /*
085 * When this method is called due to an abandon request it
086 * will close the cursor but other threads processing the
087 * search will get an OperationAbandonedException which as
088 * seen below will make sure the proper handling is
089 * performed.
090 */
091 cursor.close( new OperationAbandonedException() );
092 }
093 }
094 catch ( Exception e )
095 {
096 LOG.error( "Failed to close the search cursor for message {} on abandon request.",
097 req.getMessageId(), e );
098 }
099 }
100 }
101
102