chan-dx commented on code in PR #4718:
URL: https://github.com/apache/solr/pull/4718#discussion_r3776908263


##########
solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:
##########
@@ -174,55 +173,48 @@ private static Optional<String> commandLine(ProcessHandle 
ph) {
   }
 
   /**
-   * Gets the command lines of all java processes on Windows using PowerShell.
+   * WMI columns to select from {@code Win32_Process}. The enum constant names 
are used verbatim as
+   * the WQL {@code SELECT} column names (WQL is case-insensitive).
+   */
+  enum ProcessProperty {
+    PROCESSID,
+    NAME,
+    COMMANDLINE
+  }
+
+  /**
+   * Gets the command lines of all java processes on Windows by querying WMI 
({@code Win32_Process})
+   * through JNA. This avoids spawning an external PowerShell process.
    *
    * @return a map of process IDs to command lines
    */
   private static Map<Long, String> commandLinesWindows() {
+    COMUtils.checkRC(Ole32.INSTANCE.CoInitializeEx(null, 
Ole32.COINIT_MULTITHREADED));
     try {
-      Process process =
-          new ProcessBuilder(
-                  "powershell.exe",
-                  "-Command",
-                  "Get-CimInstance -ClassName Win32_Process | Where-Object { 
$_.Name -like '*java*' } | Select-Object ProcessId, CommandLine | 
ConvertTo-Json -Depth 1")
-              .redirectErrorStream(true)
-              .start();
-      String output = IOUtils.toString(process.getInputStream(), 
StandardCharsets.UTF_8);
-      int exitCode = process.waitFor();
-      if (exitCode != 0) {
-        String errorText = IOUtils.toString(process.getErrorStream(), 
StandardCharsets.UTF_8);
-        throw new SolrException(
-            SolrException.ErrorCode.SERVER_ERROR,
-            "Error getting command lines for Windows: " + errorText);
+      WmiResult<ProcessProperty> result =
+          new WmiQuery<>("Win32_Process", ProcessProperty.class).execute();
+      Map<Long, String> pidToCommandLine = new HashMap<>();
+      for (int i = 0; i < result.getResultCount(); i++) {
+        Object name = result.getValue(ProcessProperty.NAME, i);
+        if (name == null || 
!name.toString().toLowerCase(Locale.ROOT).contains("java")) {
+          continue;
+        }

Review Comment:
   One question on the query: it doesn't push the `java` filter into the WQL 
itself.  JNA's `WmiQuery` javadoc notes the class name may include a `WHERE` 
clause with filtering conditions. so `"Win32_Process WHERE NAME LIKE '%java%'"` 
would let WMI do the filtering and make the name check in the loop redundant. 
Also, the `LIKE` is case-insensitive according to  
[MS-WMI](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-wmi/6c8a38f4-4ee1-47cb-99f1-b42718a575ce),
 so it should match the old `-like '*java*'` behaviour. 
   
   (I noticed this is still a draft, so ignore it if this is already on your 
radar.)
   
   ```suggestion
             new WmiQuery<>("Win32_Process WHERE NAME LIKE '%java%'", 
ProcessProperty.class).execute();
         Map<Long, String> pidToCommandLine = new HashMap<>();
         for (int i = 0; i < result.getResultCount(); i++) {
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to