View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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   */
34  public class NicelyResynchronizingAjaxController extends AjaxController {
35  
36      private static final Log LOG = LogFactory.getLog(NicelyResynchronizingAjaxController.class);
37  
38      private transient WeakReference<Thread> originatedThread_;
39  
40      /**
41       * Creates an instance.
42       */
43      public NicelyResynchronizingAjaxController() {
44          super();
45          init();
46      }
47  
48      /**
49       * Initializes this instance.
50       */
51      private void init() {
52          originatedThread_ = new WeakReference<>(Thread.currentThread());
53      }
54  
55      /**
56       * Resynchronizes calls performed from the thread where this instance has been created.
57       * <p>
58       * {@inheritDoc}
59       */
60      @Override
61      public boolean processSynchron(final HtmlPage page, final WebRequest settings, final boolean async) {
62          if (async && isInOriginalThread()) {
63              LOG.info("Re-synchronized call to " + settings.getUrl());
64              return true;
65          }
66          return !async;
67      }
68  
69      /**
70       * Indicates if the currently executing thread is the one in which this instance has been created.
71       * @return {@code true} if it's the same thread
72       */
73      boolean isInOriginalThread() {
74          return Thread.currentThread() == originatedThread_.get();
75      }
76  
77      /**
78       * Custom deserialization logic.
79       * @param stream the stream from which to read the object
80       * @throws IOException if an IO error occurs
81       * @throws ClassNotFoundException if a class cannot be found
82       */
83      private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
84          stream.defaultReadObject();
85          init();
86      }
87  
88  }