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.javascript.configuration;
16
17 import java.util.Map;
18 import java.util.WeakHashMap;
19
20 import org.htmlunit.BrowserVersion;
21 import org.htmlunit.javascript.HtmlUnitScriptable;
22 import org.htmlunit.javascript.proxyautoconfig.ProxyAutoConfig;
23
24 /**
25 * A container for all the JavaScript configuration information used for the
26 * proxy AutoConfig support.
27 *
28 * @author Ronald Brill
29 */
30 public final class ProxyAutoConfigJavaScriptConfiguration extends AbstractJavaScriptConfiguration {
31
32 @SuppressWarnings("unchecked")
33 static final Class<? extends HtmlUnitScriptable>[] CLASSES_ = new Class[] {ProxyAutoConfig.class};
34
35 /** Cache of browser versions and their corresponding JavaScript configurations. */
36 private static final Map<String, ProxyAutoConfigJavaScriptConfiguration> CONFIGURATION_MAP_ = new WeakHashMap<>();
37
38 /**
39 * Constructor is only called from {@link #getInstance(BrowserVersion)} which is synchronized.
40 * @param browser the browser version to use
41 */
42 private ProxyAutoConfigJavaScriptConfiguration(final BrowserVersion browser) {
43 super(browser, ProxyAutoConfig.class);
44 }
45
46 /**
47 * Returns the instance that represents the configuration for the specified {@link BrowserVersion}.
48 * This method is synchronized to allow multi-threaded access to the JavaScript configuration.
49 * @param browserVersion the {@link BrowserVersion}
50 * @return the instance for the specified {@link BrowserVersion}
51 */
52 @SuppressWarnings("PMD.SingletonClassReturningNewInstance")
53 public static synchronized ProxyAutoConfigJavaScriptConfiguration getInstance(final BrowserVersion browserVersion) {
54 if (browserVersion == null) {
55 throw new IllegalArgumentException("BrowserVersion must be provided");
56 }
57 ProxyAutoConfigJavaScriptConfiguration configuration = CONFIGURATION_MAP_.get(browserVersion.getNickname());
58
59 if (configuration == null) {
60 configuration = new ProxyAutoConfigJavaScriptConfiguration(browserVersion);
61 CONFIGURATION_MAP_.put(browserVersion.getNickname(), configuration);
62 }
63 return configuration;
64 }
65
66 @Override
67 protected Class<? extends HtmlUnitScriptable>[] getClasses() {
68 return CLASSES_;
69 }
70 }