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

io.mstream.trader.commons.config.ApplicationVersionProvider Maven / Gradle / Ivy

The newest version!
package io.mstream.trader.commons.config;


import io.mstream.trader.commons.config.model.Version;
import org.slf4j.Logger;

import javax.inject.Inject;
import javax.inject.Provider;
import java.io.InputStream;
import java.util.Properties;

import static java.lang.String.format;
import static org.slf4j.LoggerFactory.getLogger;


public class ApplicationVersionProvider
        implements Provider {
    
    private static final Logger LOGGER =
            getLogger(ApplicationVersionProvider.class);
    
    private static final String DEFAULT_VERSION = "SNAPSHOT";
    
    private static final String POM_PROPERTIES_PATH_FORMAT =
            "/META-INF/maven/com.github.mstream-trader/%s/pom.properties";
    
    private final String applicationName;
    
    @Inject
    public ApplicationVersionProvider(
            @ApplicationName
                    String applicationName
    ) {
        
        this.applicationName = applicationName;
    }
    
    @Override
    public Version get() {
        
        String path = format(POM_PROPERTIES_PATH_FORMAT, applicationName);
        InputStream inputStream = getClass().getResourceAsStream(path);
        String version = null;
        if (inputStream == null) {
            LOGGER.error("no maven properties found in path: {}", path);
        } else {
            version = readVersionFromInputStream(inputStream);
        }
        if (version == null) {
            LOGGER.warn("using the default version value");
            return new Version(DEFAULT_VERSION);
        }
        return new Version(version);
    }
    
    private static String readVersionFromInputStream(InputStream inputStream) {
        
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            return properties.getProperty("version");
        } catch (Exception e) {
            LOGGER.error("could not obtain the application version", e);
        }
        return null;
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy