Java 11 and extract Process name (#260)

Co-authored-by: Ohad Bitton <ohbitton@microsoft.com>
This commit is contained in:
ohad bitton 2022-11-27 11:10:54 +02:00 коммит произвёл GitHub
Родитель 847898d096
Коммит 1b233a68c0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 53 добавлений и 16 удалений

2
.github/workflows/build.yml поставляемый
Просмотреть файл

@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['8']
java: ['11']
name: Java ${{ matrix.java }}
steps:
# Uncomment to run locally with "act"

Просмотреть файл

@ -1,3 +1,3 @@
language: java
jdk:
- oraclejdk8
- oraclejdk11

Просмотреть файл

@ -41,7 +41,7 @@ mvn install
# Prerequisites
- A Java Developer Kit (JDK), version 1.8 or later
- A Java Developer Kit (JDK), version 11 or later
- Maven
# Samples

Просмотреть файл

@ -5,6 +5,7 @@ import org.apache.http.client.utils.URIBuilder;
import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;
public class UriUtils {
private UriUtils() {
@ -55,7 +56,7 @@ public class UriUtils {
if (extensionPos == -1 || lastDirSeparator > extensionPos) {
return filename;
} else {
return filename.substring(0, extensionPos);
return filename.substring(lastDirSeparator + 1, extensionPos);
}
}
@ -67,4 +68,21 @@ public class UriUtils {
}
return parts;
}
// Given a cmd line used to run the java app, this method strips out the file name running
// i.e: "home/user/someFile.jar -arg1 val" -> someFile
public static String stripFileNameFromCommandLine(String cmdLine) {
try {
String processNameForTracing = cmdLine;
if (processNameForTracing != null) {
processNameForTracing = Path.of(processNameForTracing.trim().split(" ")[0]).getFileName().toString();
}
return processNameForTracing;
} catch (Exception e) {
return null;
}
}
}

Просмотреть файл

@ -3,14 +3,9 @@
package com.microsoft.azure.kusto.data.auth;
import com.microsoft.azure.kusto.data.auth.endpoints.KustoTrustedEndpoints;
import com.microsoft.azure.kusto.data.exceptions.KustoClientInvalidConnectionStringException;
import com.microsoft.azure.kusto.data.UriUtils;
import org.apache.commons.lang3.StringUtils;
import java.net.URISyntaxException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.List;
@ -39,6 +34,7 @@ public class ConnectionStringBuilder {
private String clientVersionForTracing;
private String applicationNameForTracing;
private static final String DEFAULT_DEVICE_AUTH_TENANT = "organizations";
private String processNameForTracing = null;
private ConnectionStringBuilder(String clusterUrl) {
this.clusterUrl = clusterUrl;
@ -58,6 +54,14 @@ public class ConnectionStringBuilder {
this.userNameForTracing = null;
this.clientVersionForTracing = null;
this.applicationNameForTracing = null;
this.initProcessNameForTracing();
}
private void initProcessNameForTracing() {
// sun.java.command holds the cmd line used to invoke the running application
this.processNameForTracing = UriUtils.stripFileNameFromCommandLine(System.getProperty("sun.java.command"));
// user.name is used by jvm to hold the user name
this.userNameForTracing = System.getProperty("user.name");
}
public ConnectionStringBuilder(ConnectionStringBuilder other) {
@ -78,6 +82,7 @@ public class ConnectionStringBuilder {
this.userNameForTracing = other.userNameForTracing;
this.clientVersionForTracing = other.clientVersionForTracing;
this.applicationNameForTracing = other.applicationNameForTracing;
this.processNameForTracing = other.processNameForTracing;
}
public String getClusterUrl() {
@ -153,7 +158,7 @@ public class ConnectionStringBuilder {
}
public String getApplicationNameForTracing() {
return applicationNameForTracing;
return applicationNameForTracing == null ? this.processNameForTracing : applicationNameForTracing;
}
public void setApplicationNameForTracing(String applicationNameForTracing) {

Просмотреть файл

@ -12,6 +12,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@ -134,6 +135,19 @@ class UtilitiesTest {
Assertions.assertEquals(401, Objects.requireNonNull(error.getStatusCode()).intValue());
}
@Test
@DisplayName("Remove extension")
void removeExtensionFromFileName() {
Assertions.assertEquals("fileName", UriUtils.removeExtension("fileName.csv"));
}
@Test
@DisplayName("Assert file name extracted from some cmd line")
void extractFileNameFromCommandLine() {
String cmdLine = Path.of(" home", "user", "someFile.jar").toString() + " -arg1 val";
Assertions.assertEquals(UriUtils.stripFileNameFromCommandLine(cmdLine), "someFile.jar");
}
@NotNull
private BasicHttpResponse getBasicHttpResponse(int statusCode) {
BasicHttpResponse basicHttpResponse = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("http", 1, 1), statusCode, "Some Error"));

Просмотреть файл

@ -32,9 +32,9 @@
</developers>
<properties>
<revision>3.3.0</revision> <!-- CHANGE THIS to adjust project version-->
<revision>4.0.0</revision> <!-- CHANGE THIS to adjust project version-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<java.version>11</java.version>
<!-- Use 1.0.6 for fasterxml v 2.12.x (good for databricks runtime 2.10.x) -->
<azure-bom-version>1.2.4</azure-bom-version>

Просмотреть файл

@ -33,8 +33,8 @@
</developers>
<properties>
<revision>3.3.0</revision> <!-- CHANGE THIS to match project version in the root (not technically parent) pom -->
<java.version>1.8</java.version>
<revision>4.0.0</revision> <!-- CHANGE THIS to match project version in the root (not technically parent) pom -->
<java.version>11</java.version>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>

Просмотреть файл

@ -1,7 +1,7 @@
# Getting Started with Kusto Java SDK
### Prerequisites
- A Java Developer Kit (JDK), version 1.8 or later
- A Java Developer Kit (JDK), version 11 or later
- Maven
- Clone the project and enter the samples directory:
```sh