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