View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2012-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.http.servlet;
18  
19  import jakarta.servlet.http.HttpServletRequest;
20  import jakarta.servlet.http.HttpServletResponse;
21  import java.util.concurrent.CountDownLatch;
22  
23  /**
24   * An adapter for use in Servlet 2.x containers.
25   */
26  final class Servlet2Adapter implements ServletVersionAdapter {
27  
28      /**
29       * Synchronization implementation. Package private because it is used as the
30       * fall-back implementation in Servlet 3 when async is not supported.
31       */
32      final static class Servlet2Synchronizer implements ServletSynchronizer {
33          private final CountDownLatch requestCompletionLatch = new CountDownLatch(1);
34          private Runnable listener;
35  
36          Servlet2Synchronizer() {
37              // Nothing to do.
38          }
39  
40          @Override
41          public void setAsyncListener(Runnable runnable) {
42              listener = runnable;
43          }
44  
45          @Override
46          public void awaitIfNeeded() throws InterruptedException {
47              requestCompletionLatch.await();
48              if (listener != null) {
49                  listener.run();
50              }
51          }
52  
53          @Override
54          public void signalAndComplete() {
55              requestCompletionLatch.countDown();
56          }
57      }
58  
59      Servlet2Adapter() {
60          // Nothing to do.
61      }
62  
63      @Override
64      public ServletSynchronizer createServletSynchronizer(HttpServletRequest httpRequest,
65              HttpServletResponse httpResponse) {
66          return new Servlet2Synchronizer();
67      }
68  }