|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 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 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.jdbc.utils; |
| 18 | + |
| 19 | +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.util.Properties; |
| 22 | + |
| 23 | +/** Utility class to load and parse the JDBC driver version from dependencies.properties. */ |
| 24 | +public final class BigQueryJdbcVersionUtility { |
| 25 | + |
| 26 | + private static final BigQueryJdbcCustomLogger LOG = |
| 27 | + new BigQueryJdbcCustomLogger(BigQueryJdbcVersionUtility.class.getName()); |
| 28 | + |
| 29 | + private static final String DRIVER_VERSION; |
| 30 | + private static final int DRIVER_MAJOR_VERSION; |
| 31 | + private static final int DRIVER_MINOR_VERSION; |
| 32 | + |
| 33 | + static { |
| 34 | + String version = "0.0.0"; |
| 35 | + int major = 0; |
| 36 | + int minor = 0; |
| 37 | + try (InputStream input = |
| 38 | + BigQueryJdbcVersionUtility.class.getResourceAsStream( |
| 39 | + "/com/google/cloud/bigquery/jdbc/dependencies.properties")) { |
| 40 | + if (input == null) { |
| 41 | + throw new IllegalArgumentException( |
| 42 | + "Could not find dependencies.properties. Driver version information is unavailable."); |
| 43 | + } |
| 44 | + |
| 45 | + Properties props = new Properties(); |
| 46 | + props.load(input); |
| 47 | + String versionString = props.getProperty("version.jdbc"); |
| 48 | + if (versionString == null || versionString.trim().isEmpty()) { |
| 49 | + throw new IllegalArgumentException( |
| 50 | + "The property version.jdbc not found or empty in dependencies.properties."); |
| 51 | + } |
| 52 | + |
| 53 | + version = versionString.trim(); |
| 54 | + String[] parts = version.split("\\."); |
| 55 | + if (parts.length < 2) { |
| 56 | + throw new IllegalArgumentException("Unexpected version format: " + versionString); |
| 57 | + } |
| 58 | + major = Integer.parseInt(parts[0]); |
| 59 | + String minorPart = parts[1]; |
| 60 | + String numericMinor = minorPart.replaceAll("[^0-9].*", ""); |
| 61 | + if (!numericMinor.isEmpty()) { |
| 62 | + minor = Integer.parseInt(numericMinor); |
| 63 | + } |
| 64 | + } catch (Exception e) { |
| 65 | + LOG.severe( |
| 66 | + "Error reading dependencies.properties. Driver version information is unavailable. Error: " |
| 67 | + + e.getMessage(), |
| 68 | + e); |
| 69 | + } |
| 70 | + DRIVER_VERSION = version; |
| 71 | + DRIVER_MAJOR_VERSION = major; |
| 72 | + DRIVER_MINOR_VERSION = minor; |
| 73 | + } |
| 74 | + |
| 75 | + private BigQueryJdbcVersionUtility() { |
| 76 | + // Utility class, static methods only. |
| 77 | + } |
| 78 | + |
| 79 | + public static String getDriverVersion() { |
| 80 | + return DRIVER_VERSION; |
| 81 | + } |
| 82 | + |
| 83 | + public static int getDriverMajorVersion() { |
| 84 | + return DRIVER_MAJOR_VERSION; |
| 85 | + } |
| 86 | + |
| 87 | + public static int getDriverMinorVersion() { |
| 88 | + return DRIVER_MINOR_VERSION; |
| 89 | + } |
| 90 | +} |
0 commit comments