entlicher commented on a change in pull request #2978:
URL: https://github.com/apache/netbeans/pull/2978#discussion_r642116843



##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/attach/AttachConfigurations.java
##########
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.lsp.server.debugging.attach;
+
+import com.sun.jdi.Bootstrap;
+import com.sun.jdi.VirtualMachineManager;
+import com.sun.jdi.connect.AttachingConnector;
+import com.sun.jdi.connect.Connector;
+import com.sun.tools.attach.AttachNotSupportedException;
+import com.sun.tools.attach.VirtualMachine;
+import com.sun.tools.attach.VirtualMachineDescriptor;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+
+import org.eclipse.lsp4j.MessageParams;
+import org.eclipse.lsp4j.MessageType;
+
+import org.netbeans.modules.java.lsp.server.protocol.DebugConnector;
+import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
+import org.netbeans.modules.java.lsp.server.protocol.QuickPickItem;
+import org.netbeans.modules.java.lsp.server.protocol.ShowQuickPickParams;
+import org.openide.util.RequestProcessor;
+
+/**
+ * Debugger attach configurations provider.
+ *
+ * @author Martin Entlicher
+ */
+public final class AttachConfigurations {
+
+    static final String NAME_ATTACH_PROCESS = "Attach to Process";          // 
NOI18N
+    static final String NAME_ATTACH_SOCKET = "Attach to Port";              // 
NOI18N
+    static final String NAME_ATTACH_SHMEM = "Attach to Shared Memory";      // 
NOI18N
+    static final String NAME_ATTACH_BY = "Attach by ";                      // 
NOI18N
+
+    static final String CONNECTOR_PROCESS = "com.sun.jdi.ProcessAttach";    // 
NOI18N
+    static final String CONNECTOR_SOCKET = "com.sun.jdi.SocketAttach";      // 
NOI18N
+    static final String CONNECTOR_SHMEM = "com.sun.jdi.SharedMemoryAttach"; // 
NOI18N
+
+    static final String PROCESS_ARG_PID = "processId";          // NOI18N
+    static final String SOCKET_ARG_HOST = "hostName";           // NOI18N
+    static final String SOCKET_ARG_PORT = "port";               // NOI18N
+    static final String SHMEM_ARG_NAME = "sharedMemoryName";    // NOI18N
+
+    private static final RequestProcessor RP = new 
RequestProcessor(AttachConfigurations.class);
+
+    private AttachConfigurations() {}
+
+    public static CompletableFuture<Object> findConnectors() {
+        return CompletableFuture.supplyAsync(() -> {
+            return listAttachingConnectors();
+        }, RP);
+    }
+
+    private static List<DebugConnector> listAttachingConnectors() {
+        VirtualMachineManager vmm = Bootstrap.virtualMachineManager ();
+        List<AttachingConnector> attachingConnectors = 
vmm.attachingConnectors();
+        List<DebugConnector> connectors = new ArrayList<>(5);
+        String type = "java8+";             // NOI18N
+        for (AttachingConnector ac : attachingConnectors) {
+            String connectorName = ac.name();
+            Map<String, Connector.Argument> defaultArguments = 
ac.defaultArguments();
+            DebugConnector connector;
+            switch (connectorName) {
+                case CONNECTOR_PROCESS:
+                    connector = new DebugConnector(NAME_ATTACH_PROCESS, type,
+                            Collections.singletonList(PROCESS_ARG_PID),
+                            
Collections.singletonList("${command:java.attachDebugger.pickProcess}"));    // 
NOI18N
+                    break;
+                case CONNECTOR_SOCKET: {
+                    String hostName = 
getArgumentOrDefault(defaultArguments.get("hostname"), "localhost");          
// NOI18N
+                    String port = 
getArgumentOrDefault(defaultArguments.get("port"), "<port of the debuggee 
JVM>"); // NOI18N
+                    connector = new DebugConnector(NAME_ATTACH_SOCKET, type,
+                            Arrays.asList(SOCKET_ARG_HOST, SOCKET_ARG_PORT),
+                            Arrays.asList(hostName, port));
+                    break;
+                }
+                case CONNECTOR_SHMEM: {
+                    String name = 
getArgumentOrDefault(defaultArguments.get("name"), "<shared memory name>");     
  // NOI18N
+                    connector = new DebugConnector(NAME_ATTACH_SHMEM, type,
+                            Collections.singletonList(SHMEM_ARG_NAME),
+                            Collections.singletonList(name));
+                    break;
+                }
+                default: {
+                    List<String> names = new ArrayList<>();
+                    List<String> values = new ArrayList<>();
+                    for (Connector.Argument arg : defaultArguments.values()) {
+                        if (arg.mustSpecify()) {
+                            names.add(arg.name());
+                            String value = arg.value();
+                            if (value.isEmpty()) {
+                                value = "<" + arg.description()+ ">";   // 
NOI18N
+                            }
+                            values.add(value);
+                        }
+                    }
+                    connector = new DebugConnector(NAME_ATTACH_BY + 
connectorName, type,
+                            names, values);
+                }
+            }
+            connectors.add(connector);
+        }
+        connectors.sort((c1, c2) -> 
c1.getName().compareToIgnoreCase(c2.getName()));
+        return connectors;
+    }
+
+    private static String getArgumentOrDefault(Connector.Argument arg, String 
def) {
+        if (arg != null) {
+            String value = arg.value();
+            if (!value.isEmpty()) {
+                return value;
+            }
+        }
+        return def;
+    }
+
+    public static CompletableFuture<Object> 
findProcessAttachTo(NbCodeLanguageClient client) {
+        return CompletableFuture.supplyAsync(() -> {
+            return listProcessesToAttachTo(client);
+        }, RP).thenCompose(params -> 
client.showQuickPick(params)).thenApply(itemsList -> {
+            if (itemsList == null || itemsList.isEmpty()) {
+                return null;
+            } else {
+                return itemsList.get(0).getUserData();
+            }
+        });
+    }
+
+    private static void notifyNoProcessesError(NbCodeLanguageClient client) {
+        MessageParams params = new MessageParams();
+        params.setMessage("No debuggable JVM process found.\nPlease be sure to 
use `-agentlib:jdwp=transport=dt_socket,server=y` option.");

Review comment:
       I've added localization to Java code.




-- 
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org
For additional commands, e-mail: notifications-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to