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.net.URL;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * This refresh handler spawns a new thread that waits the specified
25   * number of seconds before refreshing the specified page, using the
26   * specified URL.
27   * <p>
28   * If you want a refresh handler that ignores the wait time, see
29   * {@link ImmediateRefreshHandler}.
30   *
31   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
32   * @author Daniel Gredler
33   */
34  public class ThreadedRefreshHandler implements RefreshHandler {
35  
36      /** Logging support. */
37      private static final Log LOG = LogFactory.getLog(ThreadedRefreshHandler.class);
38  
39      /**
40       * Refreshes the specified page using the specified URL after the specified number
41       * of seconds.
42       * @param page the page that is going to be refreshed
43       * @param url the URL where the new page will be loaded
44       * @param seconds the number of seconds to wait before reloading the page
45       */
46      @Override
47      public void handleRefresh(final Page page, final URL url, final int seconds) {
48          final Thread thread = new Thread("ThreadedRefreshHandler Thread") {
49              @Override
50              public void run() {
51                  try {
52                      new WaitingRefreshHandler().handleRefresh(page, url, seconds);
53                  }
54                  catch (final IOException e) {
55                      LOG.error("Unable to refresh page!", e);
56                      throw new RuntimeException("Unable to refresh page!", e);
57                  }
58              }
59          };
60          thread.setDaemon(true);
61          thread.start();
62      }
63  
64  }