// Path: sonar-plugin-common/src/main/java/jetbrains/buildserver/sonarplugin/sqrunner/tool/SonarQubeScannerConstants.java
// public final class SonarQubeScannerConstants {
//     // Used in editSQRRunParams.jsp
//     @NotNull public static final String SONAR_QUBE_SCANNER_TOOL_TYPE_ID = "sonar-qube-scanner";
//     @NotNull public static final String SONAR_QUBE_SCANNER_TOOL_TYPE_ID_ALT = "sonar-scanner-cli";
//     @NotNull public static final String SONAR_QUBE_SCANNER_VERSION_PARAMETER = "teamcity.tool.sonarquberunner";
// }
// 
// Path: sonar-plugin-agent/src/main/java/jetbrains/buildserver/sonarplugin/util/SSLTools.java
// public final class SSLTools {
//     private static final Logger log = LoggerFactory.getLogger(SQScannerArgsComposer.class);
// 
//     /**
//      * Returns path to temporary created keystore containing TC-trusted certificates with jdk-trusted ones.
//      *
//      * @param javaHome if null, this jvm keystore will be used
//      * @param teamcityCertificateFolderPath path to folder containing TC-trusted certificates
//      * @return path to created keystore
//      */
//     public static Path cloneKeyStoreWithTC(@Nullable String javaHome, String teamcityCertificateFolderPath) {
//         try {
//             KeyStore cacerts = getCacertsKeyStore(javaHome);
//             File f = File.createTempFile("tmpks", ".jks");
//             if (storeCertificates(cacerts, teamcityCertificateFolderPath) > 0) {
//                 try (FileOutputStream fos = new FileOutputStream(f)) {
//                     cacerts.store(fos, "changeit".toCharArray());
//                 }
//                 return f.toPath();
//             }
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath);
//         } catch (Exception e) {
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath, e);
//         }
//         return null;
//     }
// 
//     public static KeyStore getCacertsKeyStore(String javaHome) throws Exception {
//         File file = new File(getCacerts(javaHome));
//         if (!file.exists()) {
//             return null;
//         }
//         KeyStore instance = KeyStore.getInstance(KeyStore.getDefaultType());
//         try (InputStream in = new FileInputStream(file)) {
//             instance.load(in, "changeit".toCharArray());
//             return instance;
//         }
//     }
// 
//     public static String getCacerts(String javaHome) {
//         String sep = File.separator;
//         if (javaHome == null)
//             javaHome = System.getProperty("java.home");
//         return javaHome + sep
//                 + "lib" + sep + "security" + sep
//                 + "cacerts";
//     }
// 
//     private static int storeCertificates(KeyStore ks, String syncCertPath) {
//         Path certificateFolder = new File(syncCertPath).toPath();
//         AtomicInteger counter = new AtomicInteger(0);
//         try {
//             Files.walk(certificateFolder, 1)
//                     .filter(Files::isRegularFile)
//                     .map(it -> storeCertificate(ks, it))
//                     .filter(Objects::nonNull)
//                     .forEach(t -> counter.incrementAndGet());
//         } catch (IOException e) {
//             log.error("Error while copying certificates from {}", syncCertPath, e);
//         }
//         return counter.get();
//     }
// 
//     private static Path storeCertificate(KeyStore ks, Path certificatePath) {
//         try (FileInputStream fis = new FileInputStream(certificatePath.toFile())) {
//             Certificate c = CertificateFactory.getInstance("X.509").generateCertificate(fis);
//             ks.setCertificateEntry(certificatePath.toFile().getName(), c);
//             return certificatePath;
//         } catch (IOException | CertificateException | KeyStoreException e) {
//             log.error("Error while reading certificate from {}", certificatePath, e);
//         }
//         return null;
//     }
// 
// }


import com.intellij.util.Function;
import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.BuildAgentConfiguration;
import jetbrains.buildServer.agent.runner.CommandLineBuildService;
import jetbrains.buildServer.agent.runner.JavaCommandLineBuilder;
import jetbrains.buildServer.agent.runner.JavaRunnerUtil;
import jetbrains.buildServer.agent.runner.ProgramCommandLine;
import jetbrains.buildServer.agent.ssl.TrustedCertificatesDirectory;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.runner.JavaRunnerConstants;
import jetbrains.buildServer.util.OSType;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildserver.sonarplugin.sqrunner.tool.SonarQubeScannerConstants;
import jetbrains.buildserver.sonarplugin.util.SSLTools;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.nio.file.Path;
import java.util.*;
import static jetbrains.buildServer.util.OSType.WINDOWS;
     * @throws SQRJarException
     */
    @NotNull
    private String getClasspath() throws SQRJarException {
        final File pluginJar[] = getSQRJar(new File(getSonarScannerRoot()));
        final StringBuilder builder = new StringBuilder();
        for (final File file : pluginJar) {
            builder.append(file.getAbsolutePath()).append(File.pathSeparatorChar);
        }
        return builder.substring(0, builder.length() - 1);
    }

    @NotNull
    private String getExecutablePath() throws RunBuildException {
        final String path = getSonarScannerRoot();

        final String execName = myOsType == WINDOWS ? "sonar-runner.bat" : "sonar-runner";

        final File exec = new File(path + File.separatorChar + "bin" + File.separatorChar + execName);

        if (!exec.exists()) {
            throw new RunBuildException("SonarQube executable doesn't exist: " + exec.getAbsolutePath());
        }
        if (!exec.isFile()) {
            throw new RunBuildException("SonarQube executable is not a file: " + exec.getAbsolutePath());
        }
        return exec.getAbsolutePath();
    }

    private String getSonarScannerRoot() {
// Path: sonar-plugin-common/src/main/java/jetbrains/buildserver/sonarplugin/sqrunner/tool/SonarQubeScannerConstants.java
// public final class SonarQubeScannerConstants {
//     // Used in editSQRRunParams.jsp
//     @NotNull public static final String SONAR_QUBE_SCANNER_TOOL_TYPE_ID = "sonar-qube-scanner";
//     @NotNull public static final String SONAR_QUBE_SCANNER_TOOL_TYPE_ID_ALT = "sonar-scanner-cli";
//     @NotNull public static final String SONAR_QUBE_SCANNER_VERSION_PARAMETER = "teamcity.tool.sonarquberunner";
// }
// 
// Path: sonar-plugin-agent/src/main/java/jetbrains/buildserver/sonarplugin/util/SSLTools.java
// public final class SSLTools {
//     private static final Logger log = LoggerFactory.getLogger(SQScannerArgsComposer.class);
// 
//     /**
//      * Returns path to temporary created keystore containing TC-trusted certificates with jdk-trusted ones.
//      *
//      * @param javaHome if null, this jvm keystore will be used
//      * @param teamcityCertificateFolderPath path to folder containing TC-trusted certificates
//      * @return path to created keystore
//      */
//     public static Path cloneKeyStoreWithTC(@Nullable String javaHome, String teamcityCertificateFolderPath) {
//         try {
//             KeyStore cacerts = getCacertsKeyStore(javaHome);
//             File f = File.createTempFile("tmpks", ".jks");
//             if (storeCertificates(cacerts, teamcityCertificateFolderPath) > 0) {
//                 try (FileOutputStream fos = new FileOutputStream(f)) {
//                     cacerts.store(fos, "changeit".toCharArray());
//                 }
//                 return f.toPath();
//             }
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath);
//         } catch (Exception e) {
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath, e);
//         }
//         return null;
//     }
// 
//     public static KeyStore getCacertsKeyStore(String javaHome) throws Exception {
//         File file = new File(getCacerts(javaHome));
//         if (!file.exists()) {
//             return null;
//         }
//         KeyStore instance = KeyStore.getInstance(KeyStore.getDefaultType());
//         try (InputStream in = new FileInputStream(file)) {
//             instance.load(in, "changeit".toCharArray());
//             return instance;
//         }
//     }
// 
//     public static String getCacerts(String javaHome) {
//         String sep = File.separator;
//         if (javaHome == null)
//             javaHome = System.getProperty("java.home");
//         return javaHome + sep
//                 + "lib" + sep + "security" + sep
//                 + "cacerts";
//     }
// 
//     private static int storeCertificates(KeyStore ks, String syncCertPath) {
//         Path certificateFolder = new File(syncCertPath).toPath();
//         AtomicInteger counter = new AtomicInteger(0);
//         try {
//             Files.walk(certificateFolder, 1)
//                     .filter(Files::isRegularFile)
//                     .map(it -> storeCertificate(ks, it))
//                     .filter(Objects::nonNull)
//                     .forEach(t -> counter.incrementAndGet());
//         } catch (IOException e) {
//             log.error("Error while copying certificates from {}", syncCertPath, e);
//         }
//         return counter.get();
//     }
// 
//     private static Path storeCertificate(KeyStore ks, Path certificatePath) {
//         try (FileInputStream fis = new FileInputStream(certificatePath.toFile())) {
//             Certificate c = CertificateFactory.getInstance("X.509").generateCertificate(fis);
//             ks.setCertificateEntry(certificatePath.toFile().getName(), c);
//             return certificatePath;
//         } catch (IOException | CertificateException | KeyStoreException e) {
//             log.error("Error while reading certificate from {}", certificatePath, e);
//         }
//         return null;
//     }
// 
// }


// Path: sonar-plugin-agent/src/main/java/jetbrains/buildserver/sonarplugin/SQRBuildService.java
import com.intellij.util.Function;
import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.BuildAgentConfiguration;
import jetbrains.buildServer.agent.runner.CommandLineBuildService;
import jetbrains.buildServer.agent.runner.JavaCommandLineBuilder;
import jetbrains.buildServer.agent.runner.JavaRunnerUtil;
import jetbrains.buildServer.agent.runner.ProgramCommandLine;
import jetbrains.buildServer.agent.ssl.TrustedCertificatesDirectory;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.runner.JavaRunnerConstants;
import jetbrains.buildServer.util.OSType;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildserver.sonarplugin.sqrunner.tool.SonarQubeScannerConstants;
import jetbrains.buildserver.sonarplugin.util.SSLTools;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.nio.file.Path;
import java.util.*;
import static jetbrains.buildServer.util.OSType.WINDOWS;

     * @throws SQRJarException
     */
    @NotNull
    private String getClasspath() throws SQRJarException {
        final File pluginJar[] = getSQRJar(new File(getSonarScannerRoot()));
        final StringBuilder builder = new StringBuilder();
        for (final File file : pluginJar) {
            builder.append(file.getAbsolutePath()).append(File.pathSeparatorChar);
        }
        return builder.substring(0, builder.length() - 1);
    }

    @NotNull
    private String getExecutablePath() throws RunBuildException {
        final String path = getSonarScannerRoot();

        final String execName = myOsType == WINDOWS ? "sonar-runner.bat" : "sonar-runner";

        final File exec = new File(path + File.separatorChar + "bin" + File.separatorChar + execName);

        if (!exec.exists()) {
            throw new RunBuildException("SonarQube executable doesn't exist: " + exec.getAbsolutePath());
        }
        if (!exec.isFile()) {
            throw new RunBuildException("SonarQube executable is not a file: " + exec.getAbsolutePath());
        }
        return exec.getAbsolutePath();
    }

    private String getSonarScannerRoot() {
        final String explicitPath = getRunnerContext().getConfigParameters().get(SonarQubeScannerConstants.SONAR_QUBE_SCANNER_VERSION_PARAMETER);