/* * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. */ package net.curisit.securis; import java.io.IOException; import java.text.MessageFormat; import java.util.Properties; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * AppVersion *

* This class allows to read a version.properties file with information about * application version This properties file is created automatically during * compilation process. The source of this information is the version string * inside pom.xml file. Format version has this format: * {majorVersion}.{minorVersion}.{incrementalVersion}[-{qualifier}] * * @author cesar * Last reviewed by JRA on Oct 5, 2025. */ public class AppVersion { private static AppVersion instance; private Properties prop; private static final String PROPERTIES_FILE_NAME = "/version.properties"; private static final Logger LOG = LogManager.getLogger(AppVersion.class); /** * AppVersion

* Constructor */ private AppVersion() { prop = new Properties(); try { prop.load(getClass().getResourceAsStream(PROPERTIES_FILE_NAME)); } catch (IOException e) { LOG.error("Version file is missing", e); } } /** * getInstance

* Get unique instance * * @return instance */ public synchronized static AppVersion getInstance() { if (instance == null) { instance = new AppVersion(); } return instance; } /** * getMajorVersion

* Returns the major version * * @return majorVersion */ public Integer getMajorVersion() { return getParamAsInt(Keys.MAJOR_VERSION, -1); } /** * getMinorVersion

* Return the minor version * * @return minorVersion */ public Integer getMinorVersion() { return getParamAsInt(Keys.MINOR_VERSION, -1); } /** * getIncrementalVersion

* Returns the incremental version * * @return incrementalVersion */ public Integer getIncrementalVersion() { return getParamAsInt(Keys.INCREMENTAL_VERSION, -1); } /** * getQualifier

* Returns qualifier if it exists * * @return qualifier */ public String getQualifier() { return getParam(Keys.QUALIFIER); } /** * getCompleteVersion

* Return complete version * * @return completeVersion */ public String getCompleteVersion() { String strVersion = MessageFormat.format("{0}.{1}.{2}", getMajorVersion(), getMinorVersion(), getIncrementalVersion()); if (getQualifier() != null && !getQualifier().isEmpty()) strVersion = strVersion + "-" + getQualifier(); return strVersion; } /*********************** Private methods *********************/ /** * getParam

* Get the parameter associated with the key * * @param key * @return param */ private String getParam(String key) { return prop.getProperty(key, null); } /** * getParamAsInt

* Get the parameter as integer * * @param key * @param defaulValue * @return paramAsInt */ private Integer getParamAsInt(String key, Integer defaulValue) { String value = getParam(key); try { if (value == null) { LOG.error("Wrong version field"); return defaulValue; } else return Integer.parseInt(value); } catch (NumberFormatException e) { LOG.error("Wrong version field"); return defaulValue; } } /** * Keys

* Application version keys */ private static class Keys { public static final String MAJOR_VERSION = "majorVersion"; public static final String MINOR_VERSION = "minorVersion"; public static final String INCREMENTAL_VERSION = "incrementalVersion"; public static final String QUALIFIER = "qualifier"; } }