Skip to content

Commit 46b7324

Browse files
committed
feat(environment): Add more Javadoc
1 parent 7e24729 commit 46b7324

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

components/environment/src/main/java/datadog/environment/CommandLine.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,40 @@
66
import java.util.Arrays;
77
import java.util.List;
88

9-
/** Fetches and captures the command line, both command and its arguments. */
9+
/**
10+
* Fetches and captures the command line, both command and its arguments. It relies on a
11+
* non-standard {@code sun.java.command} system property and was tested on:
12+
*
13+
* <ul>
14+
* <li>OracleJDK,
15+
* <li>OpenJDK,
16+
* <li>Temurin based JDK,
17+
* <li>IMB JDK,
18+
* <li>Azul Zulu,
19+
* <li>Amazon Coretto,
20+
* </ul>
21+
*/
1022
class CommandLine {
11-
final List<String> FULL_CMD = findFullCommand();
12-
final String CMD = getCommand();
13-
final List<String> CMD_ARGUMENTS = getCommandArguments();
23+
private static final String SUN_JAVA_COMMAND_PROPERTY = "sun.java.command";
24+
final List<String> fullCommand = findFullCommand();
25+
final String command = getCommand();
26+
final List<String> arguments = getCommandArguments();
1427

1528
@SuppressForbidden // split on single-character uses fast path
1629
private List<String> findFullCommand() {
17-
// Besides "sun.java.command" property is not an standard, all main JDKs has set this
18-
// property.
19-
// Tested on:
20-
// - OracleJDK, OpenJDK, AdoptOpenJDK, IBM JDK, Azul Zulu JDK, Amazon Coretto JDK
21-
String command = SystemProperties.getOrDefault("sun.java.command", "").trim();
30+
String command = SystemProperties.getOrDefault(SUN_JAVA_COMMAND_PROPERTY, "").trim();
2231
return command.isEmpty() ? emptyList() : Arrays.asList(command.split(" "));
2332
}
2433

2534
private String getCommand() {
26-
return FULL_CMD.isEmpty() ? null : FULL_CMD.get(0);
35+
return fullCommand.isEmpty() ? null : fullCommand.get(0);
2736
}
2837

2938
private List<String> getCommandArguments() {
30-
if (FULL_CMD.isEmpty()) {
31-
return FULL_CMD;
39+
if (fullCommand.isEmpty()) {
40+
return fullCommand;
3241
} else {
33-
return FULL_CMD.subList(1, FULL_CMD.size());
42+
return fullCommand.subList(1, fullCommand.size());
3443
}
3544
}
3645
}

components/environment/src/main/java/datadog/environment/EnvironmentVariables.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
55

6-
/** Safely queries environment variables. */
6+
/**
7+
* Safely queries environment variables against security manager.
8+
*
9+
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html">Security
10+
* Manager</a>
11+
*/
712
public final class EnvironmentVariables {
813
private EnvironmentVariables() {}
914

components/environment/src/main/java/datadog/environment/JavaVirtualMachine.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static List<String> getVmOptions() {
136136
* @return The command arguments, an empty collection if missing or can't be retrieved.
137137
*/
138138
public static List<String> getCommandArguments() {
139-
return commandLine.CMD_ARGUMENTS;
139+
return commandLine.arguments;
140140
}
141141

142142
/**
@@ -146,7 +146,9 @@ public static List<String> getCommandArguments() {
146146
* retrieved.
147147
*/
148148
public static @Nullable String getMainClass() {
149-
return commandLine.CMD != null && !isJarName(commandLine.CMD) ? commandLine.CMD : null;
149+
return commandLine.command != null && !isJarName(commandLine.command)
150+
? commandLine.command
151+
: null;
150152
}
151153

152154
/**
@@ -156,7 +158,9 @@ public static List<String> getCommandArguments() {
156158
* retrieved.
157159
*/
158160
public static @Nullable String getJarFile() {
159-
return commandLine.CMD != null && isJarName(commandLine.CMD) ? commandLine.CMD : null;
161+
return commandLine.command != null && isJarName(commandLine.command)
162+
? commandLine.command
163+
: null;
160164
}
161165

162166
private static boolean isJarName(String argument) {

components/environment/src/main/java/datadog/environment/OperatingSystem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Arrays;
1111
import java.util.Locale;
1212

13+
/** Detects operating systems and libc library. */
1314
public final class OperatingSystem {
1415
private OperatingSystem() {}
1516

components/environment/src/main/java/datadog/environment/SystemProperties.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package datadog.environment;
22

3-
/** Safely queries system properties. */
3+
/**
4+
* Safely queries system properties against security manager.
5+
*
6+
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html">Security
7+
* Manager</a>
8+
*/
49
public final class SystemProperties {
510
private SystemProperties() {}
611

@@ -30,11 +35,18 @@ public static String getOrDefault(String property, String defaultValue) {
3035
}
3136
}
3237

38+
/**
39+
* Sets a system property value.
40+
*
41+
* @param property The system property name.
42+
* @param value The system property value to set.
43+
* @return {@code true} if the system property was successfully set, {@code} false otherwise.
44+
*/
3345
public static boolean set(String property, String value) {
3446
try {
3547
System.setProperty(property, value);
3648
return true;
37-
} catch (SecurityException ignored) {
49+
} catch (SecurityException ignored) {
3850
return false;
3951
}
4052
}

0 commit comments

Comments
 (0)