1 2 /** 3 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 * SPDX-License-Identifier: Apache-2.0. 5 */ 6 package software.amazon.awssdk.crt.utils; 7 8 import software.amazon.awssdk.crt.CRT; 9 import software.amazon.awssdk.crt.CrtPlatform; 10 11 /** 12 * Class that wraps version and package introspection 13 */ 14 public final class PackageInfo { 15 16 /** 17 * Class representing the introspected semantic version of the CRT library 18 */ 19 public static class Version { 20 private final String version; 21 public final int major; 22 public final int minor; 23 public final int patch; 24 public final String tag; 25 Version(String v)26 public Version(String v) { 27 version = v != null ? v : "UNKNOWN"; 28 29 int dashIdx = version.indexOf('-'); 30 if (dashIdx != -1) { 31 tag = version.substring(dashIdx + 1); 32 } else { 33 tag = ""; 34 } 35 36 v = version.replace("-.+$", ""); // remove -SNAPSHOT or any other suffix 37 String[] parts = v.split("\\.", 3); 38 39 int len = parts.length; 40 patch = len > 2 ? maybeParse(parts[2]) : 0; 41 minor = len > 1 ? maybeParse(parts[1]) : 0; 42 major = len > 0 ? maybeParse(parts[0]) : 0; 43 } 44 45 @Override toString()46 public String toString() { 47 return version; 48 } 49 } 50 maybeParse(String s)51 private static int maybeParse(String s) { 52 try { 53 return Integer.parseInt(s); 54 } catch (Exception ex) { 55 return 0; 56 } 57 } 58 59 /** 60 * the introspected semantic version of the CRT library instance 61 */ 62 public Version version; 63 64 /** 65 * Default constructor 66 */ PackageInfo()67 public PackageInfo() { 68 CrtPlatform platform = CRT.getPlatformImpl(); 69 if (platform != null) { 70 version = platform.getVersion(); 71 return; 72 } 73 74 Package pkg = CRT.class.getPackage(); 75 String pkgVersion = pkg.getSpecificationVersion(); 76 if (pkgVersion == null) { 77 pkgVersion = pkg.getImplementationVersion(); 78 } 79 // There is no JAR/manifest during internal tests 80 if (pkgVersion == null) { 81 pkgVersion = "0.0.0-UNITTEST"; 82 } 83 version = new Version(pkgVersion); 84 } 85 86 } 87