protected CompletableFuture handleLaunchCommand()

in com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java [83:164]


    protected CompletableFuture<Response> handleLaunchCommand(Arguments arguments, Response response, IDebugAdapterContext context) {
        LaunchArguments launchArguments = (LaunchArguments) arguments;
        // validation
        if (StringUtils.isBlank(launchArguments.mainClass)
                || ArrayUtils.isEmpty(launchArguments.modulePaths) && ArrayUtils.isEmpty(launchArguments.classPaths)) {
            throw AdapterUtils.createCompletionException(
                "Failed to launch debuggee VM. Missing mainClass or modulePaths/classPaths options in launch configuration.",
                ErrorCode.ARGUMENT_MISSING);
        }
        if (StringUtils.isNotBlank(launchArguments.encoding)) {
            if (!Charset.isSupported(launchArguments.encoding)) {
                throw AdapterUtils.createCompletionException(
                    "Failed to launch debuggee VM. 'encoding' options in the launch configuration is not recognized.",
                    ErrorCode.INVALID_ENCODING);
            }
            context.setDebuggeeEncoding(Charset.forName(launchArguments.encoding));
            if (StringUtils.isBlank(launchArguments.vmArgs)) {
                launchArguments.vmArgs = String.format("-Dfile.encoding=%s", context.getDebuggeeEncoding().name());
            } else {
                // if vmArgs already has the file.encoding settings, duplicate options for jvm will not cause an error, the right most value wins
                launchArguments.vmArgs = String.format("%s -Dfile.encoding=%s", launchArguments.vmArgs, context.getDebuggeeEncoding().name());
            }
        }

        context.setLaunchMode(launchArguments.noDebug ? LaunchMode.NO_DEBUG : LaunchMode.DEBUG);

        activeLaunchHandler.preLaunch(launchArguments, context);

        // Use the specified cli style to launch the program.
        if (launchArguments.shortenCommandLine == ShortenApproach.JARMANIFEST) {
            if (ArrayUtils.isNotEmpty(launchArguments.classPaths)) {
                try {
                    Path tempfile = LaunchUtils.generateClasspathJar(launchArguments.classPaths);
                    launchArguments.vmArgs += " -cp \"" + tempfile.toAbsolutePath().toString() + "\"";
                    launchArguments.classPaths = new String[0];
                    context.setClasspathJar(tempfile);
                } catch (IllegalArgumentException | MalformedURLException ex) {
                    logger.log(Level.SEVERE, String.format("Failed to launch the program with jarmanifest style: %s", ex.toString(), ex));
                    throw AdapterUtils.createCompletionException("Failed to launch the program with jarmanifest style: " + ex.toString(),
                            ErrorCode.LAUNCH_FAILURE, ex);
                } catch (IOException e) {
                    logger.log(Level.SEVERE, String.format("Failed to create a temp classpath.jar: %s", e.toString()), e);
                }
            }
        } else if (launchArguments.shortenCommandLine == ShortenApproach.ARGFILE) {
            try {
                Path tempfile = LaunchUtils.generateArgfile(launchArguments.classPaths, launchArguments.modulePaths);
                launchArguments.vmArgs += " \"@" + tempfile.toAbsolutePath().toString() + "\"";
                launchArguments.classPaths = new String[0];
                launchArguments.modulePaths = new String[0];
                context.setArgsfile(tempfile);
            } catch (IOException e) {
                logger.log(Level.SEVERE, String.format("Failed to create a temp argfile: %s", e.toString()), e);
            }
        }

        return launch(launchArguments, response, context).thenCompose(res -> {
            LaunchUtils.releaseTempLaunchFile(context.getClasspathJar());
            LaunchUtils.releaseTempLaunchFile(context.getArgsfile());
            if (res.success) {
                activeLaunchHandler.postLaunch(launchArguments, context);
            }

            IDebugSession debugSession = context.getDebugSession();
            if (debugSession != null) {
                debugSession.getEventHub().events()
                    .filter((debugEvent) -> debugEvent.event instanceof VMDisconnectEvent)
                    .subscribe((debugEvent) -> {
                        context.setVmTerminated();
                        // Terminate eventHub thread.
                        try {
                            debugSession.getEventHub().close();
                        } catch (Exception e) {
                            // do nothing.
                        }

                        handleTerminatedEvent(context);
                    });
            }
            return CompletableFuture.completedFuture(res);
        });
    }