View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.io.IOException;
18  import java.io.ObjectInputStream;
19  import java.lang.ref.WeakReference;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.htmlunit.html.HtmlPage;
24  
25  /**
26   * This {@link AjaxController} resynchronizes calls calling from the main thread.
27   * The idea is that asynchronous AJAX calls performed directly in response to a user
28   * action (therefore in the "main" thread and not in the thread of a background task)
29   * are directly useful for the user. To easily have a testable state, these calls
30   * are performed synchronously.
31   *
32   * @author Marc Guillemot
33   * @author Ronald Brill
34   */
35  public class NicelyResynchronizingAjaxController extends AjaxController {
36  
37      private static final Log LOG = LogFactory.getLog(NicelyResynchronizingAjaxController.class);
38  
39      private transient WeakReference<Thread> originatedThread_;
40  
41      /**
42       * Creates an instance.
43       */
44      public NicelyResynchronizingAjaxController() {
45          super();
46          init();
47      }
48  
49      /**
50       * Initializes this instance.
51       */
52      private void init() {
53          originatedThread_ = new WeakReference<>(Thread.currentThread());
54      }
55  
56      /**
57       * Resynchronizes calls performed from the thread where this instance has been created.
58       * <p>
59       * {@inheritDoc}
60       */
61      @Override
62      public boolean processSynchron(final HtmlPage page, final WebRequest settings, final boolean async) {
63          if (async && isInOriginalThread()) {
64              LOG.info("Re-synchronized call to " + settings.getUrl());
65              return true;
66          }
67          return !async;
68      }
69  
70      /**
71       * Indicates if the currently executing thread is the one in which this instance has been created.
72       * @return {@code true} if it's the same thread
73       */
74      boolean isInOriginalThread() {
75          return Thread.currentThread() == originatedThread_.get();
76      }
77  
78      /**
79       * Custom deserialization logic.
80       * @param stream the stream from which to read the object
81       * @throws IOException if an IO error occurs
82       * @throws ClassNotFoundException if a class cannot be found
83       */
84      private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
85          stream.defaultReadObject();
86          init();
87      }
88  
89  }