All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.alexdlaird.ngrok.conf.JavaNgrokVersion Maven / Gradle / Ivy

The newest version!
package com.github.alexdlaird.ngrok.conf;

import com.github.alexdlaird.ngrok.NgrokClient;
import com.github.alexdlaird.ngrok.NgrokClient.Builder;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.Objects.nonNull;

/**
 * A singleton object that represents the java-ngrok library version.
 */
public class JavaNgrokVersion {

    private static final Logger LOGGER = Logger.getLogger(String.valueOf(NgrokClient.class));

    private static JavaNgrokVersion instance = null;

    private final String version;

    private JavaNgrokVersion(final String version) {
        this.version = version;
    }

    /**
     * Get or initialize the singleton instance.
     *
     * @return The singleton instance.
     */
    public static synchronized JavaNgrokVersion getInstance() {
        if (instance == null) {
            final String version = getVersionFromProperties();
            instance = new JavaNgrokVersion(version);
        }

        return instance;
    }

    private static String getVersionFromProperties() {
        try {
            try (final InputStream resourceStream = Builder.class.getResourceAsStream("/version.properties")) {
                if (nonNull(resourceStream)) {
                    final Properties properties = new Properties();
                    properties.load(resourceStream);

                    return properties.getProperty("version");
                }
            }
        } catch (final IOException e) {
            LOGGER.log(Level.WARNING, "An error occurred trying to read \"version\" from resource properties", e);
        }

        return null;
    }

    /**
     * Get the version.
     */
    public String getVersion() {
        return version;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy