class Utils {

    public static KeyStore createKeyStore(final String athenzPublicCert, final String athenzPrivateKey) throws FileNotFoundException, IOException, InterruptedException, KeyRefresherException {
        if (athenzPublicCert == null || athenzPublicCert.isEmpty()) {
            throw new FileNotFoundException("athenzPublicCert can not be empty");
        }
        if (athenzPrivateKey == null || athenzPrivateKey.isEmpty()) {
            throw new FileNotFoundException("athenzPrivateKey can not be empty");
        }

        final Supplier<InputStream> certFileSupplier;
        final Supplier<InputStream> keyFileSupplier;
        final Supplier<String> certLocationSupplier = () -> athenzPublicCert;
        final Supplier<String> keyLocationSupplier = () -> athenzPrivateKey;
        if (Paths.get(athenzPublicCert).isAbsolute() && Paths.get(athenzPrivateKey).isAbsolute()) {
            final File certFile = new File(athenzPublicCert);
            final File keyFile = new File(athenzPrivateKey);
            certFileSupplier = inputStreamSupplierFromFile(certFile);
            keyFileSupplier = inputStreamSupplierFromFile(keyFile);
            long startTime = System.currentTimeMillis();
            while (!certFile.exists() || !keyFile.exists()) {
                long durationInMillis = System.currentTimeMillis() - startTime;
                if (durationInMillis > KEY_WAIT_TIME_MILLIS) {
                    throw new KeyRefresherException("Keyfresher waited " + durationInMillis
                            + " ms for valid public cert: " + athenzPublicCert + " or private key: "
                            + athenzPrivateKey + " files. Giving up.");
                }
                LOG.error("Missing Athenz public certificate {} or private key {} files. Waiting {} ms",
                        athenzPublicCert, athenzPrivateKey, durationInMillis);
                Thread.sleep(1000);
            }
        } else {
            certFileSupplier = inputStreamSupplierFromResource(athenzPublicCert);
            keyFileSupplier = inputStreamSupplierFromResource(athenzPrivateKey);
        }

        // Pass input stream providers that throw unchecked exceptions which are caught and rethrown from here.
        try {
            return createKeyStore(certFileSupplier, certLocationSupplier, keyFileSupplier, keyLocationSupplier);
        } catch (UncheckedIOException e) {
            throw e.getCause();
        }
    }

}