|
75 | 75 | import com.google.common.collect.ImmutableMap; |
76 | 76 | import com.google.common.io.Resources; |
77 | 77 | import java.io.IOException; |
| 78 | +import java.io.UncheckedIOException; |
78 | 79 | import java.net.URL; |
79 | 80 | import java.nio.charset.StandardCharsets; |
80 | 81 | import java.util.ArrayList; |
81 | 82 | import java.util.Collection; |
82 | 83 | import java.util.HashMap; |
83 | 84 | import java.util.List; |
84 | 85 | import java.util.Map; |
| 86 | +import java.util.concurrent.ConcurrentHashMap; |
85 | 87 | import java.util.stream.Collectors; |
86 | 88 | import java.util.stream.Stream; |
87 | 89 | import org.openqa.selenium.InvalidSelectorException; |
|
96 | 98 | */ |
97 | 99 | public class W3CHttpCommandCodec extends AbstractHttpCommandCodec { |
98 | 100 |
|
| 101 | + private static final ConcurrentHashMap<String, String> ATOM_SCRIPTS = new ConcurrentHashMap<>(); |
| 102 | + |
99 | 103 | public W3CHttpCommandCodec() { |
100 | 104 | String sessionId = "/session/:sessionId"; |
101 | 105 |
|
@@ -338,15 +342,22 @@ private List<String> stringToUtf8Array(String toConvert) { |
338 | 342 |
|
339 | 343 | private Map<String, ?> executeAtom(String atomFileName, Object... args) { |
340 | 344 | try { |
341 | | - String scriptName = "/org/openqa/selenium/remote/" + atomFileName; |
342 | | - URL url = getClass().getResource(scriptName); |
343 | | - |
344 | | - String rawFunction = Resources.toString(url, StandardCharsets.UTF_8); |
345 | | - String atomName = atomFileName.replace(".js", ""); |
346 | | - String script = |
347 | | - String.format("/* %s */return (%s).apply(null, arguments);", atomName, rawFunction); |
| 345 | + String script = ATOM_SCRIPTS.computeIfAbsent(atomFileName, (fileName) -> { |
| 346 | + String scriptName = "/org/openqa/selenium/remote/" + atomFileName; |
| 347 | + URL url = getClass().getResource(scriptName); |
| 348 | + String rawFunction; |
| 349 | + try { |
| 350 | + rawFunction = Resources.toString(url, StandardCharsets.UTF_8); |
| 351 | + } catch (IOException e) { |
| 352 | + throw new UncheckedIOException(e); |
| 353 | + } |
| 354 | + String atomName = fileName.replace(".js", ""); |
| 355 | + return String.format("/* %s */return (%s).apply(null, arguments);", atomName, rawFunction); |
| 356 | + }); |
348 | 357 | return toScript(script, args); |
349 | | - } catch (IOException | NullPointerException e) { |
| 358 | + } catch (UncheckedIOException e) { |
| 359 | + throw new WebDriverException(e.getCause()); |
| 360 | + } catch (NullPointerException e) { |
350 | 361 | throw new WebDriverException(e); |
351 | 362 | } |
352 | 363 | } |
|
0 commit comments