|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.docker.client; |
| 19 | + |
| 20 | +import java.util.logging.Logger; |
| 21 | + |
| 22 | +/** |
| 23 | + * Factory for creating API version-specific adapters. |
| 24 | + * |
| 25 | + * <p>This factory selects the appropriate adapter based on the Docker API version: |
| 26 | + * |
| 27 | + * <ul> |
| 28 | + * <li>API v1.40-1.43: Uses {@link V140Adapter} |
| 29 | + * <li>API v1.44+: Uses {@link V144Adapter} |
| 30 | + * </ul> |
| 31 | + * |
| 32 | + * <p>The factory uses version comparison to determine which adapter to use, ensuring that future |
| 33 | + * API versions (e.g., 1.45, 1.46) automatically use the most appropriate adapter. |
| 34 | + */ |
| 35 | +class AdapterFactory { |
| 36 | + |
| 37 | + private static final Logger LOG = Logger.getLogger(AdapterFactory.class.getName()); |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates an appropriate adapter for the given API version. |
| 41 | + * |
| 42 | + * @param apiVersion The Docker API version (e.g., "1.40", "1.44") |
| 43 | + * @return An adapter suitable for the specified API version |
| 44 | + * @throws IllegalArgumentException if apiVersion is null or empty |
| 45 | + */ |
| 46 | + public static ApiVersionAdapter createAdapter(String apiVersion) { |
| 47 | + if (apiVersion == null || apiVersion.trim().isEmpty()) { |
| 48 | + throw new IllegalArgumentException("API version cannot be null or empty"); |
| 49 | + } |
| 50 | + |
| 51 | + // API v1.44+ uses the new adapter |
| 52 | + if (compareVersions(apiVersion, "1.44") >= 0) { |
| 53 | + LOG.fine("Using V144Adapter for API version " + apiVersion); |
| 54 | + return new V144Adapter(apiVersion); |
| 55 | + } |
| 56 | + |
| 57 | + // API v1.40-1.43 uses the legacy adapter |
| 58 | + LOG.fine("Using V140Adapter for API version " + apiVersion); |
| 59 | + return new V140Adapter(apiVersion); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Compares two version strings in the format "major.minor". |
| 64 | + * |
| 65 | + * @param version1 First version string (e.g., "1.44") |
| 66 | + * @param version2 Second version string (e.g., "1.44") |
| 67 | + * @return negative if version1 < version2, zero if equal, positive if version1 > version2 |
| 68 | + */ |
| 69 | + private static int compareVersions(String version1, String version2) { |
| 70 | + String[] parts1 = version1.split("\\."); |
| 71 | + String[] parts2 = version2.split("\\."); |
| 72 | + |
| 73 | + int major1 = Integer.parseInt(parts1[0]); |
| 74 | + int minor1 = parts1.length > 1 ? Integer.parseInt(parts1[1]) : 0; |
| 75 | + |
| 76 | + int major2 = Integer.parseInt(parts2[0]); |
| 77 | + int minor2 = parts2.length > 1 ? Integer.parseInt(parts2[1]) : 0; |
| 78 | + |
| 79 | + if (major1 != major2) { |
| 80 | + return major1 - major2; |
| 81 | + } |
| 82 | + return minor1 - minor2; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Determines if the given API version supports multiple network endpoints. |
| 87 | + * |
| 88 | + * @param apiVersion The Docker API version |
| 89 | + * @return true if multiple networks are supported, false otherwise |
| 90 | + */ |
| 91 | + public static boolean supportsMultipleNetworks(String apiVersion) { |
| 92 | + return createAdapter(apiVersion).supportsMultipleNetworks(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Determines if the given API version includes the VirtualSize field. |
| 97 | + * |
| 98 | + * @param apiVersion The Docker API version |
| 99 | + * @return true if VirtualSize field is present, false otherwise |
| 100 | + */ |
| 101 | + public static boolean hasVirtualSizeField(String apiVersion) { |
| 102 | + return createAdapter(apiVersion).hasVirtualSizeField(); |
| 103 | + } |
| 104 | +} |
0 commit comments