private String extractBinaries()

in java/amazon-kinesis-producer/src/main/java/com/amazonaws/services/kinesis/producer/KinesisProducer.java [969:1044]


    private String extractBinaries() {
        synchronized (EXTRACT_BIN_MUTEX) {
            final List<File> watchFiles = new ArrayList<>(2);
            String os = SystemUtils.OS_NAME;
            if (SystemUtils.IS_OS_WINDOWS) {
                os = "windows";
            } else if (SystemUtils.IS_OS_LINUX) {
                os = "linux-" + (SystemUtils.OS_ARCH.equals("amd64") ? "x86_64" : SystemUtils.OS_ARCH);
            } else if (SystemUtils.IS_OS_MAC_OSX) {
                os = "osx";
            } else {
                throw new RuntimeException("Your operation system is not supported (" +
                        os + "), the KPL only supports Linux, OSX and Windows");
            }
            
            String root = "amazon-kinesis-producer-native-binaries";
            String tmpDir = config.getTempDirectory();
            if (tmpDir.trim().length() == 0) {
                tmpDir = System.getProperty("java.io.tmpdir");
            }
            tmpDir = Paths.get(tmpDir, root).toString();
            pathToTmpDir = tmpDir;
            
            String binPath = config.getNativeExecutable();
            if (binPath != null && !binPath.trim().isEmpty()) {
                pathToExecutable = binPath.trim();
                log.warn("Using non-default native binary at " + pathToExecutable);

                File parent = new File(binPath).getParentFile();
                pathToLibDir = parent.getAbsolutePath();
                CertificateExtractor certificateExtractor = new CertificateExtractor();

                try {
                    String caDirectory = certificateExtractor
                            .extractCertificates(parent.getAbsoluteFile());
                    watchFiles.addAll(certificateExtractor.getExtractedCertificates());
                    FileAgeManager.instance().registerFiles(watchFiles);
                    return caDirectory;
                } catch (IOException ioex) {
                    log.error("Exception while extracting certificates.  Returning no CA directory", ioex);
                    return "";
                }
            } else {
                log.info("Extracting binaries to " + tmpDir);
                try {
                    File tmpDirFile = new File(tmpDir);
                    if (!tmpDirFile.exists() && !tmpDirFile.mkdirs()) {
                        throw new IOException("Could not create tmp dir " + tmpDir);
                    }
                    
                    String extension = os.equals("windows") ? ".exe" : "";
                    String executableName = "kinesis_producer" + extension;

                    InputStream is = this.getClass().getClassLoader().getResourceAsStream(root + "/" + os + "/" + executableName);
                    String resultFileFormat = "kinesis_producer_%s" + extension;

                    File extracted = HashedFileCopier.copyFileFrom(is, tmpDirFile, resultFileFormat);
                    watchFiles.add(extracted);
                    extracted.setExecutable(true);
                    pathToExecutable = extracted.getAbsolutePath();

                    CertificateExtractor certificateExtractor = new CertificateExtractor();

                    String caDirectory = certificateExtractor
                            .extractCertificates(new File(pathToTmpDir).getAbsoluteFile());
                    watchFiles.addAll(certificateExtractor.getExtractedCertificates());
                    pathToLibDir = pathToTmpDir;
                    FileAgeManager.instance().registerFiles(watchFiles);
                    return caDirectory;
                } catch (Exception e) {
                    throw new RuntimeException("Could not copy native binaries to temp directory " + tmpDir, e);
                }

            }
        }
    }