View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  
22  /* ------------------------------------------------------------ */
23  /** Iterating Nested Callback.
24   * <p>This specialized callback is used when breaking up an
25   * asynchronous task into smaller asynchronous tasks.  A typical pattern
26   * is that a successful callback is used to schedule the next sub task, but 
27   * if that task completes quickly and uses the calling thread to callback
28   * the success notification, this can result in a growing stack depth.
29   * </p>
30   * <p>To avoid this issue, this callback uses an AtomicBoolean to note 
31   * if the success callback has been called during the processing of a 
32   * sub task, and if so then the processing iterates rather than recurses.
33   * </p>
34   * <p>This callback is passed to the asynchronous handling of each sub
35   * task and a call the {@link #succeeded()} on this call back represents
36   * completion of the subtask.  Only once all the subtasks are completed is 
37   * the {@link Callback#succeeded()} method called on the {@link Callback} instance
38   * passed the the {@link #IteratingNestedCallback(Callback)} constructor.</p>
39   *  
40   */
41  public abstract class IteratingNestedCallback extends IteratingCallback
42  {
43      final Callback _callback;
44      
45      public IteratingNestedCallback(Callback callback)
46      {
47          _callback=callback;
48      }
49  
50      @Override
51      public boolean isNonBlocking()
52      { 
53          return _callback.isNonBlocking();
54      }
55      
56      @Override
57      protected void onCompleteSuccess()
58      {
59          _callback.succeeded();
60      }
61      
62      @Override
63      protected void onCompleteFailure(Throwable x)
64      {
65          _callback.failed(x);
66      }
67  
68      @Override
69      public String toString()
70      {
71          return String.format("%s@%x",getClass().getSimpleName(),hashCode());
72      }
73  }