static

in src/com/amazon/corretto/crypto/provider/Loader.java [96:203]


    static {
        boolean available = false;
        Throwable error = null;
        String versionStr = null;
        double oldVersion = 0;

        try {
            versionStr = AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> {
                try (InputStream is = Loader.class.getResourceAsStream("version.properties")) {
                    Properties p = new Properties();
                    p.load(is);
                    return p.getProperty("versionStr");
                }
            });

            Matcher m = OLD_VERSION_PATTERN.matcher(versionStr);
            if (!m.matches()) {
                throw new AssertionError("Version string has wrong form: " + versionStr);
            }
            oldVersion = Double.parseDouble(m.group(1));

            available = AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () -> {
                // This is to work a JVM runtime bug where FileSystems.getDefault() and
                // System.loadLibrary() can deadlock. Calling this explicitly shoulf prevent
                // the problem from happening, but since we don't know what other threads are
                // doing, we cannot promise success.
                FileSystems.getDefault();

                // First, try to find the library in our own jar
                final boolean useExternalLib = Boolean.valueOf(getProperty("useExternalLib", "false"));
                Exception loadingException = null;

                String libraryName = System.mapLibraryName(LIBRARY_NAME);
                if (useExternalLib) {
                    loadingException = new RuntimeCryptoException("Skipping bundled library due to system property");
                } else if (libraryName != null) {
                    int index = libraryName.lastIndexOf('.');
                    final String prefix = libraryName.substring(0, index);
                    final String suffix = libraryName.substring(index, libraryName.length());

                    final Path libPath = createTmpFile(prefix, suffix);

                    try (final InputStream is = Loader.class.getResourceAsStream(libraryName);
                         final OutputStream os = Files.newOutputStream(libPath, StandardOpenOption.CREATE,
                                 StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
                        final byte[] buffer = new byte[16 * 1024];
                        int read = is.read(buffer);
                        while (read >= 0) {
                            os.write(buffer, 0, read);
                            read = is.read(buffer);
                        }
                        os.flush();
                        System.load(libPath.toAbsolutePath().toString());
                        return true;
                    } catch (final Exception realException) {
                        loadingException = realException;
                    } catch (final Throwable realError) {
                        loadingException = new RuntimeCryptoException("Unable to load native library", realError);
                    } finally {
                        Files.delete(libPath);
                    }
                } else {
                    loadingException = new RuntimeCryptoException("Skipping bundled library null mapped name");
                }

                if (loadingException != null) {
                    // We failed to load the library from our JAR but don't know why.
                    // Try to load it directly off of the system path.
                    try {
                        System.loadLibrary(LIBRARY_NAME);
                        return true;
                    } catch (final Throwable suppressedError) {
                        loadingException.addSuppressed(suppressedError);
                        throw loadingException;
                    }
                }

                return false;
            });
        } catch (final Throwable t) {
            available = false;
            error = t;
        }
        PROVIDER_VERSION_STR = versionStr;
        PROVIDER_VERSION = oldVersion;

        // Check for native/java library version mismatch
        if (available) {
            try {
                assertVersionMatch();
            } catch (final AssertionError e) {
                available = false;
                error = e;
            }
        }
        IS_AVAILABLE = available;
        LOADING_ERROR = error;
        if (DebugFlag.VERBOSELOGS.isEnabled()) {
            if (available) {
                LOG.log(Level.CONFIG, "Successfully loaded native library version " + PROVIDER_VERSION_STR);
            } else {
                LOG.log(Level.CONFIG, "Unable to load native library", error);
            }
        }

        // Finally start up a cleaning thread if necessary
        RESOURCE_JANITOR = new Janitor();
    }