Skip to content

Commit

Permalink
fix: update version resolution logic to be more resilient
Browse files Browse the repository at this point in the history
Update StorageOptions to resolve their version from maven metadata rather than manifest version. A jar can only have on MANIFEST.mf thereby leading to storage thinking it is a version it is not.

In particular, if a program is shaded, this change will now allow for the actual version of storage to be carried through.

Related googleapis/google-api-java-client#1419
  • Loading branch information
BenWhitehead committed Aug 17, 2023
1 parent 73a9f75 commit 3fad05c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.storage;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceDefaults;
import com.google.cloud.ServiceOptions;
Expand All @@ -26,10 +27,38 @@
import com.google.cloud.storage.HttpStorageOptions.HttpStorageRpcFactory;
import com.google.cloud.storage.TransportCompatibility.Transport;
import com.google.cloud.storage.spi.StorageRpcFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public abstract class StorageOptions extends ServiceOptions<Storage, StorageOptions> {

private static final long serialVersionUID = -7295846567928013233L;
private static final String VERSION;

static {
String tmp = "unresolved";
final Properties props = new Properties();
try {
String resourcePath =
String.format(
"/META-INF/maven/%s/%s/pom.properties", "com.google.cloud", "google-cloud-storage");
InputStream resourceAsStream = StorageOptions.class.getResourceAsStream(resourcePath);
if (resourceAsStream == null) {
// some classloaders don't like a leading slash
resourceAsStream = StorageOptions.class.getResourceAsStream(resourcePath.substring(1));
}
if (resourceAsStream != null) {
props.load(resourceAsStream);
resourceAsStream.close();

tmp = props.getProperty("version", "unknown-version");
}
} catch (IOException ignore) {
// ignored
}
VERSION = tmp;
}

/** @deprecated Use {@link HttpStorageFactory} */
@Deprecated
Expand Down Expand Up @@ -86,6 +115,17 @@ protected boolean projectIdRequired() {
return false;
}

@Override
public String getLibraryVersion() {
return VERSION;
}

/* This can break at any time, the value produce is intended to be informative not authoritative */
@InternalApi
public static String version() {
return VERSION;
}

@SuppressWarnings("unchecked")
@Override
public abstract StorageOptions.Builder toBuilder();
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.google.api.core.ApiFutures;
import com.google.api.core.BetaApi;
import com.google.api.core.ListenableFutureToApiFuture;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
Expand All @@ -45,8 +44,7 @@
final class TransferManagerImpl implements TransferManager {

private static final String USER_AGENT_ENTRY = "gcloud-tm/";
private static final String LIBRARY_VERSION =
GaxProperties.getLibraryVersion(TransferManagerConfig.class);
private static final String LIBRARY_VERSION = StorageOptions.version();
private final TransferManagerConfig transferManagerConfig;
private final ListeningExecutorService executor;
private final Qos qos;
Expand Down

0 comments on commit 3fad05c

Please sign in to comment.