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.i18n.I18n;
026    import org.apache.directory.server.ldap.LdapServer;
027    import org.apache.directory.shared.ldap.exception.OperationAbandonedException;
028    import org.apache.directory.shared.ldap.message.AbandonListener;
029    import org.apache.directory.shared.ldap.message.internal.InternalAbandonableRequest;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    
034    /**
035     * An AbandonListener implementation which closes an associated cursor or 
036     * removes a DirectoryListener.
037     *
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev$, $Date$
040     */
041    public class SearchAbandonListener implements AbandonListener
042    {
043        private static final Logger LOG = LoggerFactory.getLogger( SearchAbandonListener.class );
044        private final LdapServer ldapServer;
045        private EntryFilteringCursor cursor;
046        private DirectoryListener listener;
047        
048        
049        public SearchAbandonListener( LdapServer ldapServer, EntryFilteringCursor cursor, DirectoryListener listener )
050        {
051            if ( ldapServer == null )
052            {
053                throw new NullPointerException( "ldapServer" );
054            }
055            
056            this.ldapServer = ldapServer;
057            this.cursor = cursor;
058            this.listener = listener;
059        }
060        
061        
062        public SearchAbandonListener( LdapServer ldapServer, DirectoryListener listener )
063        {
064            this ( ldapServer, null, listener );
065        }
066        
067        
068        public SearchAbandonListener( LdapServer ldapServer, EntryFilteringCursor cursor )
069        {
070            this ( ldapServer, cursor, null );
071        }
072        
073        
074        public void requestAbandoned( InternalAbandonableRequest req )
075        {
076            if ( listener != null )
077            {
078                ldapServer.getDirectoryService().getEventService().removeListener( listener );
079            }
080    
081            try
082            {
083                if ( cursor != null )
084                {
085                    /*
086                     * When this method is called due to an abandon request it 
087                     * will close the cursor but other threads processing the 
088                     * search will get an OperationAbandonedException which as
089                     * seen below will make sure the proper handling is 
090                     * performed.
091                     */
092                    cursor.close( new OperationAbandonedException() );
093                }
094            }
095            catch ( Exception e )
096            {
097                LOG.error( I18n.err( I18n.ERR_166, req.getMessageId() ), e );
098            }
099        }
100    }
101    
102