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.core.integ;
21  
22  
23  import org.apache.directory.api.ldap.model.entry.Entry;
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.exception.LdapOperationErrorException;
26  import org.apache.directory.server.core.api.filtering.EntryFilter;
27  import org.apache.directory.server.core.api.filtering.EntryFilteringCursor;
28  import org.apache.directory.server.core.api.interceptor.BaseInterceptor;
29  import org.apache.directory.server.core.api.interceptor.Interceptor;
30  import org.apache.directory.server.core.api.interceptor.context.SearchOperationContext;
31  
32  
33  /**
34   * An {@link Interceptor} that fakes a specified amount of delay to each
35   * search iteration so we can make sure search time limits are adhered to.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class DelayInducingInterceptor extends BaseInterceptor
40  {
41      private Long delayMillis;
42  
43  
44      public DelayInducingInterceptor()
45      {
46          super( "DelayInterceptor" );
47      }
48  
49  
50      @Override
51      public EntryFilteringCursor search( SearchOperationContext searchContext ) throws LdapException
52      {
53          EntryFilteringCursor cursor = next( searchContext );
54          cursor.addEntryFilter( new EntryFilter()
55          {
56              /**
57               * {@inheritDoc}
58               */
59              public boolean accept( SearchOperationContext operation, Entry result ) throws LdapException
60              {
61                  if ( delayMillis != null )
62                  {
63                      try
64                      {
65                          Thread.sleep( delayMillis );
66                      }
67                      catch ( InterruptedException ie )
68                      {
69                          throw new LdapOperationErrorException( ie.getMessage() );
70                      }
71                  }
72  
73                  return true;
74              }
75              
76              
77              /**
78               * {@inheritDoc}
79               */
80              public String toString( String tabs )
81              {
82                  return tabs + "DelayInducingFilter";
83              }
84          } );
85  
86          return cursor;
87      }
88  
89  
90      public void setDelayMillis( long delayMillis )
91      {
92          if ( delayMillis <= 0 )
93          {
94              this.delayMillis = null;
95          }
96  
97          this.delayMillis = delayMillis;
98      }
99  }