Skip to content

Commit

Permalink
Make project.properties path independent per API (#1863)
Browse files Browse the repository at this point in the history
- Previously we generate all project.properties files into root which is
incorrect
- Now the caller API client needs to pass in package path to retrieve the
corresponding version
- Create project.properties for Veneer-only clients as well
  • Loading branch information
shinfan authored Apr 12, 2017
1 parent 95946ef commit 3f0f645
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 96 deletions.
21 changes: 21 additions & 0 deletions google-cloud-bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</parent>
<properties>
<site.installationModule>google-cloud-bigquery</site.installationModule>
<artifact.version>${project.version}</artifact.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -61,4 +62,24 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/com/google/cloud/bigquery/project.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions google-cloud-compute/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</parent>
<properties>
<site.installationModule>google-cloud-compute</site.installationModule>
<artifact.version>${project.version}</artifact.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -54,4 +55,24 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/com/google/cloud/compute/project.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions google-cloud-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</parent>
<properties>
<site.installationModule>google-cloud-core</site.installationModule>
<artifact.version>${project.version}</artifact.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -171,4 +172,24 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/com/google/cloud/project.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static ChannelProvider setUpChannelProvider(
InstantiatingChannelProvider.Builder providerBuilder, ServiceOptions<?, ?> serviceOptions) {
providerBuilder.setEndpoint(serviceOptions.getHost())
.setClientLibHeader(ServiceOptions.getGoogApiClientLibName(),
firstNonNull(ServiceOptions.getLibraryVersion(), ""));
firstNonNull(serviceOptions.getLibraryVersion(), ""));
Credentials scopedCredentials = serviceOptions.getScopedCredentials();
if (scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()) {
providerBuilder.setCredentialsProvider(FixedCredentialsProvider.create(scopedCredentials));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public HttpTransportFactory getHttpTransportFactory() {
* Returns a request initializer responsible for initializing requests according to service
* options.
*/
public HttpRequestInitializer getHttpRequestInitializer(ServiceOptions<?, ?> serviceOptions) {
public HttpRequestInitializer getHttpRequestInitializer(
final ServiceOptions<?, ?> serviceOptions) {
Credentials scopedCredentials = serviceOptions.getScopedCredentials();
final HttpRequestInitializer delegate =
scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()
Expand All @@ -158,17 +159,17 @@ public void initialize(HttpRequest httpRequest) throws IOException {
}

HttpHeaders headers = httpRequest.getHeaders();
headers.set("x-goog-api-client", getXGoogApiClientHeader());
headers.set("x-goog-api-client", getXGoogApiClientHeader(serviceOptions));
}
};
}

String getXGoogApiClientHeader() {
String getXGoogApiClientHeader(ServiceOptions<?, ?> serviceOptions) {
return String.format(
"gl-java/%s %s/%s",
getJavaVersion(),
ServiceOptions.getGoogApiClientLibName(),
ServiceOptions.getLibraryVersion());
serviceOptions.getLibraryVersion());
}

private static String getJavaVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.api.gax.core.CurrentMillisClock;
import com.google.api.gax.core.ApiClock;
import com.google.api.gax.core.CurrentMillisClock;
import com.google.api.gax.core.PropertiesProvider;
import com.google.api.gax.core.RetrySettings;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.spi.ServiceRpcFactory;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;

import org.joda.time.Duration;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -49,17 +44,16 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.joda.time.Duration;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
* Abstract class representing service options.
Expand All @@ -73,17 +67,13 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>,
private static final String DEFAULT_HOST = "https://www.googleapis.com";
private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT";
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";
private static final String MANIFEST_ARTIFACT_ID_KEY = "artifactId";
private static final String MANIFEST_VERSION_KEY = "Implementation-Version";
private static final String ARTIFACT_ID = "google-cloud-core";
private static final String LIBRARY_NAME = "gcloud-java";
private static final String X_GOOGLE_CLIENT_HEADER_NAME = "gccl";
private static final String LIBRARY_VERSION = defaultLibraryVersion();
private static final String APPLICATION_NAME =
LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION;

private static final String META_FILE_ROOT = "/META-INF/maven/";
private static final String META_VERSION_KEY = "version";
private static final String PROPERTIES_VERSION_KEY = "artifact.version";
private static final String DEFAULT_PACKAGE_PATH = "com/google/cloud";
private static final String PROPERTIES_FILE = "project.properties";

private static final RetrySettings DEFAULT_RETRY_SETTINGS = getDefaultRetrySettingsBuilder()
.build();
private static final RetrySettings NO_RETRY_SETTINGS = getDefaultRetrySettingsBuilder()
Expand Down Expand Up @@ -525,8 +515,9 @@ public TransportOptions getTransportOptions() {
/**
* Returns the application's name as a string in the format {@code gcloud-java/[version]}.
*/
public static String getApplicationName() {
return APPLICATION_NAME;
public String getApplicationName() {
String libraryVersion = getLibraryVersion();
return libraryVersion == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + libraryVersion;
}


Expand All @@ -547,8 +538,17 @@ public static String getGoogApiClientLibName() {
/**
* Returns the library's version as a string.
*/
public static String getLibraryVersion() {
return LIBRARY_VERSION;
public String getLibraryVersion() {
try {
String version = getVersionProperty(getPackagePath());
if (version == null) {
version = getVersionProperty(DEFAULT_PACKAGE_PATH);
}
return version;
} catch (Exception e) {
// ignore
}
return null;
}

protected int baseHashCode() {
Expand Down Expand Up @@ -618,47 +618,13 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
}

private static String defaultLibraryVersion() {
String version = getPomVersion();
if (version == null) {
version = getManifestVersion();
}
return version;
}

private static String getPomVersion() {
try {
Properties properties = new Properties();
String mavenPropertiesPath = META_FILE_ROOT
+ ServiceOptions.class.getPackage().getName() + "/"
+ ARTIFACT_ID + "/pom.properties";
InputStream inputStream = ServiceOptions.class.getResourceAsStream(mavenPropertiesPath);
if (inputStream != null) {
properties.load(inputStream);
return properties.getProperty(META_VERSION_KEY, "");
}
} catch (Exception e) {
// ignore
}
return null;
private String getVersionProperty(String packagePath) {
String projectPropertiesPath = "/" + packagePath + "/" + PROPERTIES_FILE;
return PropertiesProvider.loadProperty(
ServiceOptions.class, projectPropertiesPath, PROPERTIES_VERSION_KEY);
}

private static String getManifestVersion() {
String version = null;
try {
Enumeration<URL> resources =
ServiceOptions.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
while (resources.hasMoreElements() && version == null) {
Manifest manifest = new Manifest(resources.nextElement().openStream());
Attributes manifestAttributes = manifest.getMainAttributes();
String artifactId = manifestAttributes.getValue(MANIFEST_ARTIFACT_ID_KEY);
if (artifactId != null && artifactId.equals(ARTIFACT_ID)) {
version = manifestAttributes.getValue(MANIFEST_VERSION_KEY);
}
}
} catch (IOException e) {
// ignore
}
return version;
private String getPackagePath() {
return this.getClass().getPackage().getName().replaceAll("\\.", "/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ public void testBaseHashCode() {

@Test
public void testHeader() {
String expectedHeaderPattern = "^gl-java/.* gccl/.*";
String expectedHeaderPattern = "^gl-java/.* gccl/0.0.0";
final ServiceOptions mockOptions = EasyMock.createMock(ServiceOptions.class);
EasyMock.expect(mockOptions.getLibraryVersion()).andReturn("0.0.0");
EasyMock.replay(mockOptions);
assertTrue(Pattern.compile(expectedHeaderPattern)
.matcher(DEFAULT_OPTIONS.getXGoogApiClientHeader())
.matcher(OPTIONS.getXGoogApiClientHeader(mockOptions))
.find());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
import com.google.api.gax.core.CurrentMillisClock;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.spi.ServiceRpcFactory;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.regex.Pattern;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ServiceOptionsTest {
private static final String JSON_KEY =
Expand Down Expand Up @@ -98,7 +96,7 @@ public class ServiceOptionsTest {
private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build();
private static final String LIBRARY_NAME = "gcloud-java";
private static final Pattern APPLICATION_NAME_PATTERN =
Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?");
Pattern.compile(LIBRARY_NAME + "/.*");

@Rule public ExpectedException thrown = ExpectedException.none();

Expand Down Expand Up @@ -268,7 +266,7 @@ public void testBaseEquals() {

@Test
public void testLibraryName() {
assertEquals(LIBRARY_NAME, OPTIONS.getLibraryName());
assertEquals(LIBRARY_NAME, ServiceOptions.getLibraryName());
}

@Test
Expand Down
21 changes: 21 additions & 0 deletions google-cloud-datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</parent>
<properties>
<site.installationModule>google-cloud-datastore</site.installationModule>
<artifact.version>${project.version}</artifact.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -59,4 +60,24 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/com/google/cloud/datastore/project.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 3f0f645

Please sign in to comment.