1 /*
2 * Copyright (C) 2015 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.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 * 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(
35 BuildInfo.class.getName());
36
37 /**
38 * Location of properties file that contains build data.
39 * Build data is stored in this file, which is modified each time that
40 * compilation is run in the CI server.
41 */
42 private static final String BUILD_INFO_PROPERTIES = "build-info.properties";
43
44 /**
45 * Key to obtain build timestamp from properties file.
46 */
47 private static final String BUILD_TIMESTAMP_KEY = "BUILD_TIMESTAMP";
48
49 /**
50 * Key to obtain groupID of this library from properties file.
51 */
52 private static final String GROUP_ID_KEY = "GROUP_ID";
53
54 /**
55 * Key to obtain artifactId of this library from properties file.
56 */
57 private static final String ARTIFACT_ID_KEY = "ARTIFACT_ID";
58
59 /**
60 * Key to obtain version of this library from properties file.
61 */
62 private static final String VERSION_KEY = "VERSION";
63
64 /**
65 * Key to obtain build number from properties file.
66 */
67 private static final String BUILD_NUMBER_KEY = "BUILD_NUMBER";
68
69 /**
70 * Key to obtain build commit from properties file.
71 */
72 private static final String COMMIT_KEY = "COMMIT";
73
74 /**
75 * Key to obtain build branch from properties file.
76 */
77 private static final String BRANCH_KEY = "BRANCH";
78
79 /**
80 * Format for build timestamp.
81 */
82 private static final String TIMESTAMP_FORMAT = "yy-MM-dd HH:mm:ss";
83
84 /**
85 * Singleton stored in a soft reference (to keep it cached in memory unless
86 * memory is claimed).
87 */
88 private static SoftReference<BuildInfo> reference;
89
90 /**
91 * Build timestamp.
92 */
93 private Date buildTimestamp;
94
95 /**
96 * GroupId of this library.
97 */
98 private String groupId;
99
100 /**
101 * ArtifactId of this library.
102 */
103 private String artifactId;
104
105 /**
106 * Version of this library.
107 */
108 private String version;
109
110 /**
111 * Build number.
112 */
113 private String buildNumber;
114
115 /**
116 * Build commit.
117 */
118 private String commit;
119
120 /**
121 * Build branch.
122 */
123 private String branch;
124
125 /**
126 * Constructor.
127 */
128 private BuildInfo() {
129 // loads properties file data
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 }