diff --git a/pom.xml b/pom.xml index 0cb56e50..c9a7d65d 100644 --- a/pom.xml +++ b/pom.xml @@ -79,6 +79,16 @@ + + + src/main/java + true + + hadoopCosPluginVersionInfo.properties + + + + org.apache.maven.plugins @@ -146,5 +156,6 @@ + diff --git a/src/main/java/hadoopCosPluginVersionInfo.properties b/src/main/java/hadoopCosPluginVersionInfo.properties new file mode 100644 index 00000000..f68edbca --- /dev/null +++ b/src/main/java/hadoopCosPluginVersionInfo.properties @@ -0,0 +1 @@ +plugin_version=${project.version} \ No newline at end of file diff --git a/src/main/java/org/apache/hadoop/fs/CosNativeFileSystemStore.java b/src/main/java/org/apache/hadoop/fs/CosNativeFileSystemStore.java index d923c807..cbafbf0a 100644 --- a/src/main/java/org/apache/hadoop/fs/CosNativeFileSystemStore.java +++ b/src/main/java/org/apache/hadoop/fs/CosNativeFileSystemStore.java @@ -10,6 +10,7 @@ import com.qcloud.cos.model.*; import com.qcloud.cos.region.Region; import com.qcloud.cos.utils.Base64; +import com.qcloud.cos.utils.IOUtils; import com.qcloud.cos.utils.Jackson; import com.qcloud.cos.utils.StringUtils; import org.apache.commons.codec.binary.Hex; @@ -138,9 +139,9 @@ private void initCOSClient(URI uri, Configuration conf) throws IOException { config.setProxyPassword(proxyPassword); } - String userAgent = conf.get( - CosNConfigKeys.USER_AGENT, - CosNConfigKeys.DEFAULT_USER_AGENT); + String versionNum = getPluginVersionInfo(); + String versionInfo = versionNum.equals("unknown") ? CosNConfigKeys.DEFAULT_USER_AGENT : "cos-hadoop-plugin-v" + versionNum; + String userAgent = conf.get(CosNConfigKeys.USER_AGENT,versionInfo); String emrVersion = conf.get(CosNConfigKeys.TENCENT_EMR_VERSION_KEY); if (null != emrVersion && !emrVersion.isEmpty()) { userAgent = String.format("%s on %s", userAgent, emrVersion); @@ -1343,4 +1344,26 @@ private Object callCOSClientWithRetry(X request) throws CosServiceException, private static String ensureValidAttributeName(String attributeName) { return attributeName.replace('.', '-').toLowerCase(); } + + private String getPluginVersionInfo() { + Properties versionProperties = new Properties(); + InputStream inputStream= null; + String versionStr = "unknown"; + final String versionFile = "hadoopCosPluginVersionInfo.properties"; + try { + inputStream = this.getClass().getClassLoader().getResourceAsStream(versionFile); + if (inputStream != null) { + versionProperties.load(inputStream); + versionStr = versionProperties.getProperty("plugin_version"); + }else { + LOG.error("load versionInfo properties failed, propName: {} ", versionFile); + } + } catch (IOException e) { + LOG.error("load versionInfo properties exception, propName: {} ", versionFile); + } finally { + IOUtils.closeQuietly(inputStream,LOG); + } + return versionStr; + } + }