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 */
020package org.apache.directory.server.core.integ;
021
022
023import org.apache.directory.api.ldap.model.entry.Entry;
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.exception.LdapOperationErrorException;
026import org.apache.directory.server.core.api.filtering.EntryFilter;
027import org.apache.directory.server.core.api.filtering.EntryFilteringCursor;
028import org.apache.directory.server.core.api.interceptor.BaseInterceptor;
029import org.apache.directory.server.core.api.interceptor.Interceptor;
030import org.apache.directory.server.core.api.interceptor.context.SearchOperationContext;
031
032
033/**
034 * An {@link Interceptor} that fakes a specified amount of delay to each
035 * search iteration so we can make sure search time limits are adhered to.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class DelayInducingInterceptor extends BaseInterceptor
040{
041    private Long delayMillis;
042
043
044    public DelayInducingInterceptor()
045    {
046        super( "DelayInterceptor" );
047    }
048
049
050    @Override
051    public EntryFilteringCursor search( SearchOperationContext searchContext ) throws LdapException
052    {
053        EntryFilteringCursor cursor = next( searchContext );
054        cursor.addEntryFilter( new EntryFilter()
055        {
056            /**
057             * {@inheritDoc}
058             */
059            public boolean accept( SearchOperationContext operation, Entry result ) throws LdapException
060            {
061                if ( delayMillis != null )
062                {
063                    try
064                    {
065                        Thread.sleep( delayMillis );
066                    }
067                    catch ( InterruptedException ie )
068                    {
069                        throw new LdapOperationErrorException( ie.getMessage() );
070                    }
071                }
072
073                return true;
074            }
075            
076            
077            /**
078             * {@inheritDoc}
079             */
080            public String toString( String tabs )
081            {
082                return tabs + "DelayInducingFilter";
083            }
084        } );
085
086        return cursor;
087    }
088
089
090    public void setDelayMillis( long delayMillis )
091    {
092        if ( delayMillis <= 0 )
093        {
094            this.delayMillis = null;
095        }
096
097        this.delayMillis = delayMillis;
098    }
099}