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.numerical;
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 * Location of properties file that contains build data.
38 * Build data is stored in this file, which is modified each time that
39 * compilation is 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 try (final var stream = BuildInfo.class.getResourceAsStream(BUILD_INFO_PROPERTIES)) {
130 final var props = new Properties();
131 props.load(stream);
132
133 final var buildTimestampString = props.getProperty(BUILD_TIMESTAMP_KEY);
134 final var format = new SimpleDateFormat(TIMESTAMP_FORMAT, Locale.ENGLISH);
135 buildTimestamp = format.parse(buildTimestampString);
136
137 groupId = props.getProperty(GROUP_ID_KEY);
138 artifactId = props.getProperty(ARTIFACT_ID_KEY);
139 version = props.getProperty(VERSION_KEY);
140 buildNumber = props.getProperty(BUILD_NUMBER_KEY);
141 commit = props.getProperty(COMMIT_KEY);
142 branch = props.getProperty(BRANCH_KEY);
143 } catch (final Exception e) {
144 LOGGER.log(Level.WARNING, "Failed to load build info", e);
145 }
146 }
147
148 /**
149 * Obtains singleton instance.
150 *
151 * @return singleton instance.
152 */
153 public static synchronized BuildInfo getInstance() {
154 BuildInfo info;
155 if (reference == null || (info = reference.get()) == null) {
156 info = new BuildInfo();
157 reference = new SoftReference<>(info);
158 }
159
160 return info;
161 }
162
163 /**
164 * Obtains build timestamp.
165 *
166 * @return build timestamp.
167 */
168 public Date getBuildTimestamp() {
169 return (Date) buildTimestamp.clone();
170 }
171
172 /**
173 * Obtains groupId of this library.
174 *
175 * @return groupId of this library.
176 */
177 public String getGroupId() {
178 return groupId;
179 }
180
181 /**
182 * Obtains artifactId of this library.
183 *
184 * @return artifactId of this library.
185 */
186 public String getArtifactId() {
187 return artifactId;
188 }
189
190 /**
191 * Obtains version of this library.
192 *
193 * @return version of this library.
194 */
195 public String getVersion() {
196 return version;
197 }
198
199 /**
200 * Obtains build number.
201 *
202 * @return build number.
203 */
204 public String getBuildNumber() {
205 return buildNumber;
206 }
207
208 /**
209 * Obtains build commit.
210 *
211 * @return build commit.
212 */
213 public String getCommit() {
214 return commit;
215 }
216
217 /**
218 * Obtains build branch.
219 *
220 * @return build branch.
221 */
222 public String getBranch() {
223 return branch;
224 }
225 }