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.javascript.configuration;
16  
17  import java.util.Map;
18  import java.util.concurrent.ConcurrentHashMap;
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_ =
37              new ConcurrentHashMap<>();
38  
39      /**
40       * Constructor is only called from {@link #getInstance(BrowserVersion)}.
41       * @param browser the browser version to use
42       */
43      private ProxyAutoConfigJavaScriptConfiguration(final BrowserVersion browser) {
44          super(browser, ProxyAutoConfig.class);
45      }
46  
47      /**
48       * Returns the instance that represents the configuration for the specified {@link BrowserVersion}.
49       * This method is thread-safe; the configuration is created only once per browser version.
50       * @param browserVersion the {@link BrowserVersion}
51       * @return the instance for the specified {@link BrowserVersion}
52       */
53      public static ProxyAutoConfigJavaScriptConfiguration getInstance(final BrowserVersion browserVersion) {
54          if (browserVersion == null) {
55              throw new IllegalArgumentException("BrowserVersion must be provided");
56          }
57          return CONFIGURATION_MAP_.computeIfAbsent(browserVersion.getNickname(),
58                      nickname -> new ProxyAutoConfigJavaScriptConfiguration(browserVersion));
59      }
60  
61      @Override
62      protected Class<? extends HtmlUnitScriptable>[] getClasses() {
63          return CLASSES_;
64      }
65  }