1 /*
2 * Copyright (C) 2018 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.navigation.indoor;
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 * Contains build data of this library.
28 */
29 public class BuildInfo {
30
31 /**
32 * This class logger.
33 */
34 private static final Logger LOGGER = Logger.getLogger(BuildInfo.class.getName());
35
36 /**
37 * Properties file that contains build data.
38 * Build data is stored in this file, which is modified each time that compilation is
39 * run in the CI server.
40 */
41 private static final String BUILD_INFO_PROPERTIES = "build-info.properties";
42
43 /**
44 * Key to obtain build timestamp from properties file.
45 */
46 private static final String BUILD_TIMESTAMP_KEY = "BUILD_TIMESTAMP";
47
48 /**
49 * Key to obtain groupID of this library from properties file.
50 */
51 private static final String GROUP_ID_KEY = "GROUP_ID";
52
53 /**
54 * Key to obtain artifactId of this library from properties file.
55 */
56 private static final String ARTIFACT_ID_KEY = "ARTIFACT_ID";
57
58 /**
59 * Key to obtain version of this library from properties file.
60 */
61 private static final String VERSION_KEY = "VERSION";
62
63 /**
64 * Key to obtain build number from properties file.
65 */
66 private static final String BUILD_NUMBER_KEY = "BUILD_NUMBER";
67
68 /**
69 * Key to obtain build commit from properties file.
70 */
71 private static final String COMMIT_KEY = "COMMIT";
72
73 /**
74 * Key to obtain build branch from properties file.
75 */
76 private static final String BRANCH_KEY = "BRANCH";
77
78 /**
79 * Format for build timestamp.
80 */
81 private static final String TIMESTAMP_FORMAT = "yy-MM-dd HH:mm:ss";
82
83 /**
84 * Singleton stored in a soft reference (to keep it cached in memory unless
85 * memory is claimed).
86 */
87 private static SoftReference<BuildInfo> reference;
88
89 /**
90 * Build timestamp.
91 */
92 private Date buildTimestamp;
93
94 /**
95 * GroupId of this library.
96 */
97 private String groupId;
98
99 /**
100 * ArtifactId of this library.
101 */
102 private String artifactId;
103
104 /**
105 * Version of this library.
106 */
107 private String version;
108
109 /**
110 * Build number.
111 */
112 private String buildNumber;
113
114 /**
115 * Build commit.
116 */
117 private String commit;
118
119 /**
120 * Build branch.
121 */
122 private String branch;
123
124 /**
125 * Constructor.
126 */
127 private BuildInfo() {
128 // loads properties file data.
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 * Obtains singleton instance.
151 *
152 * @return singleton instance.
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 * Obtains build timestamp.
166 *
167 * @return build timestamp.
168 */
169 public Date getBuildTimestamp() {
170 return (Date) buildTimestamp.clone();
171 }
172
173 /**
174 * Obtains groupId of this library.
175 *
176 * @return groupId of this library.
177 */
178 public String getGroupId() {
179 return groupId;
180 }
181
182 /**
183 * Obtains artifactId of this library.
184 *
185 * @return artifactId of this library.
186 */
187 public String getArtifactId() {
188 return artifactId;
189 }
190
191 /**
192 * Obtains version of this library.
193 *
194 * @return version of this library.
195 */
196 public String getVersion() {
197 return version;
198 }
199
200 /**
201 * Obtains build number.
202 *
203 * @return build number.
204 */
205 public String getBuildNumber() {
206 return buildNumber;
207 }
208
209 /**
210 * Obtains build commit.
211 *
212 * @return build commit.
213 */
214 public String getCommit() {
215 return commit;
216 }
217
218 /**
219 * Obtains build branch.
220 *
221 * @return build branch.
222 */
223 public String getBranch() {
224 return branch;
225 }
226 }