xref: /aosp_15_r20/external/sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/core/GaxProperties.java (revision 882aa7c72c3cd3b66e72a261bdd69b93f7de7670)
1 /*
2  * Copyright 2017 Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 package com.google.api.gax.core;
31 
32 import static org.graalvm.nativeimage.ImageInfo.PROPERTY_IMAGE_CODE_KEY;
33 import static org.graalvm.nativeimage.ImageInfo.PROPERTY_IMAGE_CODE_VALUE_RUNTIME;
34 
35 import com.google.api.core.InternalApi;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.util.Properties;
39 
40 /** Provides properties of the GAX library. */
41 @InternalApi
42 public class GaxProperties {
43 
44   private static final String DEFAULT_VERSION = "";
45   private static final String GAX_VERSION = getLibraryVersion(GaxProperties.class, "version.gax");
46   private static final String JAVA_VERSION = getRuntimeVersion();
47 
GaxProperties()48   private GaxProperties() {}
49 
50   /** Returns the version of the library that the {@code libraryClass} belongs to */
getLibraryVersion(Class<?> libraryClass)51   public static String getLibraryVersion(Class<?> libraryClass) {
52     String version = libraryClass.getPackage().getImplementationVersion();
53     return version != null ? version : DEFAULT_VERSION;
54   }
55 
56   /**
57    * Returns the version of the library that the {@code libraryClass} belongs to, or a property
58    * value in dependencies.properties resource file instead, if the version was not found. The
59    * method is doing I/O operations and is potentially inefficient, the values returned by this
60    * method are expected to be cached.
61    */
getLibraryVersion(Class<?> libraryClass, String propertyName)62   public static String getLibraryVersion(Class<?> libraryClass, String propertyName) {
63     String version = null;
64     // Always read GaxProperties' version from the properties file.
65     if (!libraryClass.equals(GaxProperties.class)) {
66       version = getLibraryVersion(libraryClass);
67       if (!DEFAULT_VERSION.equals(version)) {
68         return version;
69       }
70     }
71 
72     try (InputStream in = libraryClass.getResourceAsStream("/dependencies.properties")) {
73       if (in != null) {
74         Properties props = new Properties();
75         props.load(in);
76         version = props.getProperty(propertyName);
77       }
78     } catch (IOException e) {
79       // ignore
80     }
81 
82     return version != null ? version : DEFAULT_VERSION;
83   }
84 
85   /** Returns the version of the running JVM */
getJavaVersion()86   public static String getJavaVersion() {
87     // When running the application as a native image, append `-graalvm` to the
88     // version.
89     String imageCode = System.getProperty(PROPERTY_IMAGE_CODE_KEY);
90     if (imageCode != null && imageCode.equals(PROPERTY_IMAGE_CODE_VALUE_RUNTIME)) {
91       return System.getProperty("java.version") + "-graalvm";
92     }
93     return JAVA_VERSION;
94   }
95 
96   /** Returns the current version of GAX. */
getGaxVersion()97   public static String getGaxVersion() {
98     return GAX_VERSION;
99   }
100 
101   /** Returns the current runtime version */
getRuntimeVersion()102   private static String getRuntimeVersion() {
103     return System.getProperty("java.version");
104   }
105 }
106