-
Notifications
You must be signed in to change notification settings - Fork 11
Enhance logic to retrieve application name #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
e7d7a76
df5b8d0
85e9874
93289f6
3cf2299
87d7c6a
1a1003e
55009b1
2cbeccb
c8d4060
21ce537
8161fa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import org.slf4j.bridge.SLF4JBridgeHandler; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.lang.management.ManagementFactory; | ||
|
|
@@ -175,9 +176,47 @@ public boolean jdbcCompliant() { | |
| * @return the name of the currently running application. | ||
| */ | ||
| private static String getApplicationName() { | ||
| // Currently not supported. | ||
| // Need to implement logic to get the process ID of the current process, then check the set of running processes and pick out | ||
| // What we do is get the process ID of the current process, then check the set of running processes and pick out | ||
| // the one that matches the current process. From there we can grab the name of what is running the process. | ||
| try { | ||
| final String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; | ||
| final boolean isWindows = System.getProperty("os.name").startsWith("Windows"); | ||
|
|
||
| String command; | ||
| if (isWindows) { | ||
| command = "tasklist /fo csv /nh"; | ||
| } else { | ||
| command = "ps -eo pid,comm"; | ||
| } | ||
|
|
||
| final Process process = Runtime.getRuntime().exec(command); | ||
| try (BufferedReader input = new BufferedReader( | ||
| new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { | ||
| String line; | ||
| if (isWindows) { | ||
| while ((line = input.readLine()) != null) { | ||
| int start = line.indexOf(","); | ||
| int end = line.indexOf(",", start+1); | ||
| if (line.substring(start+1, end).contains(pid)){ | ||
| return line.substring(0, line.indexOf(",")); | ||
| } | ||
| } | ||
| } else { | ||
| while ((line = input.readLine()) != null) { | ||
| line = line.trim(); | ||
| if (line.startsWith(pid)) { | ||
| return line.substring(line.indexOf(" ") + 1); | ||
RoyZhang2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception err) { | ||
| // Eat the exception and fall through. | ||
| LOGGER.warning( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't expect it to fail, it would have been best to use error-level logging and propagate the error properly, so it is possible to retrieve exceptions from log files later (that is an essential information for troubleshooting)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is not a fatal error that would block the driver running, so warning is a proper log level as it is not serious if application name could not be got. |
||
| "An exception has occurred and ignored while retrieving the caller application name: " | ||
| + err.getLocalizedMessage()); | ||
| } | ||
|
|
||
| return "Unknown"; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.