/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.googleapis;

import com.google.api.client.util.SecurityUtils;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class for the Google API Client Library.
 *
 * @since 1.12
 * @author rmistry@google.com (Ravi Mistry)
 */
public final class GoogleUtils {

  /** Current release version. */
  public static final String VERSION = getVersion();

  // NOTE: Integer instead of int so compiler thinks it isn't a constant, so it won't inline it
  /**
   * Major part of the current release version.
   *
   * @since 1.14
   */
  public static final Integer MAJOR_VERSION;

  /**
   * Minor part of the current release version.
   *
   * @since 1.14
   */
  public static final Integer MINOR_VERSION;

  /**
   * Bug fix part of the current release version.
   *
   * @since 1.14
   */
  public static final Integer BUGFIX_VERSION;

  @VisibleForTesting
  static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(-SNAPSHOT)?");

  static {
    Matcher versionMatcher = VERSION_PATTERN.matcher(VERSION);
    versionMatcher.find();
    MAJOR_VERSION = Integer.parseInt(versionMatcher.group(1));
    MINOR_VERSION = Integer.parseInt(versionMatcher.group(2));
    BUGFIX_VERSION = Integer.parseInt(versionMatcher.group(3));
  }

  /** Cached value for {@link #getCertificateTrustStore()}. */
  static KeyStore certTrustStore;

  /**
   * Returns the key store for trusted root certificates to use for Google APIs.
   *
   * <p>Value is cached, so subsequent access is fast.
   *
   * @since 1.14
   */
  public static synchronized KeyStore getCertificateTrustStore()
      throws IOException, GeneralSecurityException {
    if (certTrustStore == null) {
      certTrustStore = SecurityUtils.getJavaKeyStore();
      InputStream keyStoreStream = GoogleUtils.class.getResourceAsStream("google.jks");
      SecurityUtils.loadKeyStore(certTrustStore, keyStoreStream, "notasecret");
    }
    return certTrustStore;
  }

  private static String getVersion() {
    String version = GoogleUtils.class.getPackage().getImplementationVersion();
    // in a non-packaged environment (local), there's no implementation version to read
    if (version == null) {
      // fall back to reading from a properties file - note this value is expected to be cached
      try (InputStream inputStream =
          GoogleUtils.class.getResourceAsStream("google-api-client.properties")) {
        if (inputStream != null) {
          Properties properties = new Properties();
          properties.load(inputStream);
          version = properties.getProperty("google-api-client.version");
        }
      } catch (IOException e) {
        // ignore
      }
    }
    return version;
  }

  private GoogleUtils() {}
}

@@ -2832,297 +2832,184 @@
     
-String version = GoogleUtils.class.getPackage().getImplementationVersion();%0A    // in a non-packaged environment (local), there's no implementation version to read%0A    if (version == null) %7B%0A      // fall back to reading from a properties file - note this value is expected to be cached%0A  
+// attempt to read the library's version from a properties file generated during the build%0A    // this value should be read and cached for later use%0A    String version = null;%0A
     
@@ -3039,18 +3039,16 @@
 tream =%0A
-  
         
@@ -3126,18 +3126,16 @@
 %7B%0A      
-  
 if (inpu
@@ -3149,26 +3149,24 @@
  != null) %7B%0A
-  
         Prop
@@ -3211,18 +3211,16 @@
         
-  
 properti
@@ -3241,18 +3241,16 @@
 tream);%0A
-  
         
@@ -3308,26 +3308,24 @@
 .version%22);%0A
-  
       %7D%0A    
@@ -3324,18 +3324,16 @@
   %7D%0A    
-  
 %7D catch 
@@ -3356,18 +3356,16 @@
 %7B%0A      
-  
 // ignor
@@ -3374,28 +3374,58 @@
     
- 
- 
 %7D%0A    
-%7D%0A    
 return
+ version == null ? %22unknown-version%22 :
  ver

update GoogleUtils#getVersion() to use stable logic (#1452)