Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * 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
 * <p>
 * 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<p>
    * 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<p>
    * Get unique instance
    * 
    * @return instance
    */
   public synchronized static AppVersion getInstance() {
       if (instance == null) {
           instance = new AppVersion();
       }
       return instance;
   }
   /**
    * getMajorVersion<p>
    * Returns the major version
    * 
    * @return majorVersion
    */
   public Integer getMajorVersion() {
       return getParamAsInt(Keys.MAJOR_VERSION, -1);
   }
   /**
    * getMinorVersion<p>
    * Return the minor version
    * 
    * @return minorVersion
    */
   public Integer getMinorVersion() {
       return getParamAsInt(Keys.MINOR_VERSION, -1);
   }
   /**
    * getIncrementalVersion<p>
    * Returns the incremental version
    * 
    * @return incrementalVersion
    */
   public Integer getIncrementalVersion() {
       return getParamAsInt(Keys.INCREMENTAL_VERSION, -1);
   }
   /**
    * getQualifier<p>
    * Returns qualifier if it exists
    * 
    * @return qualifier
    */
   public String getQualifier() {
       return getParam(Keys.QUALIFIER);
   }
   /**
    * getCompleteVersion<p>
    * 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<p>
    * Get the parameter associated with the key
    * 
    * @param key
    * @return param
    */
   private String getParam(String key) {
       return prop.getProperty(key, null);
   }
   /**
    * getParamAsInt<p>
    * 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<p>
    * 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";
   }
}