1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.statistics;
17
18 import java.lang.ref.SoftReference;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.Locale;
22 import java.util.Properties;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26
27
28
29 public class BuildInfo {
30
31
32
33
34 private static final Logger LOGGER = Logger.getLogger(
35 BuildInfo.class.getName());
36
37
38
39
40
41
42 private static final String BUILD_INFO_PROPERTIES = "build-info.properties";
43
44
45
46
47 private static final String BUILD_TIMESTAMP_KEY = "BUILD_TIMESTAMP";
48
49
50
51
52 private static final String GROUP_ID_KEY = "GROUP_ID";
53
54
55
56
57 private static final String ARTIFACT_ID_KEY = "ARTIFACT_ID";
58
59
60
61
62 private static final String VERSION_KEY = "VERSION";
63
64
65
66
67 private static final String BUILD_NUMBER_KEY = "BUILD_NUMBER";
68
69
70
71
72 private static final String COMMIT_KEY = "COMMIT";
73
74
75
76
77 private static final String BRANCH_KEY = "BRANCH";
78
79
80
81
82 private static final String TIMESTAMP_FORMAT = "yy-MM-dd HH:mm:ss";
83
84
85
86
87
88 private static SoftReference<BuildInfo> reference;
89
90
91
92
93 private Date buildTimestamp;
94
95
96
97
98 private String groupId;
99
100
101
102
103 private String artifactId;
104
105
106
107
108 private String version;
109
110
111
112
113 private String buildNumber;
114
115
116
117
118 private String commit;
119
120
121
122
123 private String branch;
124
125
126
127
128 private BuildInfo() {
129
130 try (final var stream = BuildInfo.class.getResourceAsStream(BUILD_INFO_PROPERTIES)) {
131 final var props = new Properties();
132 props.load(stream);
133
134 final var buildTimestampString = props.getProperty(BUILD_TIMESTAMP_KEY);
135 final var format = new SimpleDateFormat(TIMESTAMP_FORMAT, Locale.ENGLISH);
136 buildTimestamp = format.parse(buildTimestampString);
137
138 groupId = props.getProperty(GROUP_ID_KEY);
139 artifactId = props.getProperty(ARTIFACT_ID_KEY);
140 version = props.getProperty(VERSION_KEY);
141 buildNumber = props.getProperty(BUILD_NUMBER_KEY);
142 commit = props.getProperty(COMMIT_KEY);
143 branch = props.getProperty(BRANCH_KEY);
144 } catch (final Exception e) {
145 LOGGER.log(Level.WARNING, "Failed to load build info", e);
146 }
147 }
148
149
150
151
152
153
154 public static synchronized BuildInfo getInstance() {
155 BuildInfo info;
156 if (reference == null || (info = reference.get()) == null) {
157 info = new BuildInfo();
158 reference = new SoftReference<>(info);
159 }
160
161 return info;
162 }
163
164
165
166
167
168
169 public Date getBuildTimestamp() {
170 return (Date) buildTimestamp.clone();
171 }
172
173
174
175
176
177
178 public String getGroupId() {
179 return groupId;
180 }
181
182
183
184
185
186
187 public String getArtifactId() {
188 return artifactId;
189 }
190
191
192
193
194
195
196 public String getVersion() {
197 return version;
198 }
199
200
201
202
203
204
205 public String getBuildNumber() {
206 return buildNumber;
207 }
208
209
210
211
212
213
214 public String getCommit() {
215 return commit;
216 }
217
218
219
220
221
222
223 public String getBranch() {
224 return branch;
225 }
226 }