com.google.api.client.googleapis.GoogleUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.google.api.client.googleapis.GoogleUtils.java

Source

/*
 * 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() {
    }
}