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.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   * </p>
31   *
32   * @author Mike Bowler
33   * @author Daniel Gredler
34   * @author Ronald Brill
35   */
36  public class ThreadedRefreshHandler implements RefreshHandler {
37  
38      /** Logging support. */
39      private static final Log LOG = LogFactory.getLog(ThreadedRefreshHandler.class);
40  
41      /**
42       * Refreshes the specified page using the specified URL after the specified number
43       * of seconds.
44       * @param page the page that is going to be refreshed
45       * @param url the URL where the new page will be loaded
46       * @param seconds the number of seconds to wait before reloading the page
47       */
48      @Override
49      public void handleRefresh(final Page page, final URL url, final int seconds) {
50          final Thread thread = new Thread("ThreadedRefreshHandler Thread") {
51              @Override
52              public void run() {
53                  try {
54                      new WaitingRefreshHandler().handleRefresh(page, url, seconds);
55                  }
56                  catch (final IOException e) {
57                      LOG.error("Unable to refresh page!", e);
58                      throw new RuntimeException("Unable to refresh page!", e);
59                  }
60              }
61          };
62          thread.setDaemon(true);
63          thread.start();
64      }
65  
66  }