1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.util;
18
19 import io.netty5.util.internal.PlatformDependent;
20
21 import java.io.InputStream;
22 import java.net.URL;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.Enumeration;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.Set;
30 import java.util.TreeMap;
31
32
33
34
35
36
37
38
39
40 public final class Version {
41
42 private static final String PROP_VERSION = ".version";
43 private static final String PROP_BUILD_DATE = ".buildDate";
44 private static final String PROP_COMMIT_DATE = ".commitDate";
45 private static final String PROP_SHORT_COMMIT_HASH = ".shortCommitHash";
46 private static final String PROP_LONG_COMMIT_HASH = ".longCommitHash";
47 private static final String PROP_REPO_STATUS = ".repoStatus";
48
49
50
51
52
53
54
55 public static Map<String, Version> identify() {
56 return identify(null);
57 }
58
59
60
61
62
63
64 public static Map<String, Version> identify(ClassLoader classLoader) {
65 if (classLoader == null) {
66 classLoader = PlatformDependent.getContextClassLoader();
67 }
68
69
70 Properties props = new Properties();
71 try {
72 Enumeration<URL> resources = classLoader.getResources("META-INF/io.netty5.versions.properties");
73 while (resources.hasMoreElements()) {
74 URL url = resources.nextElement();
75 try (InputStream in = url.openStream()) {
76 props.load(in);
77 }
78
79 }
80 } catch (Exception ignore) {
81
82 }
83
84
85 Set<String> artifactIds = new HashSet<>();
86 for (Object o: props.keySet()) {
87 String k = (String) o;
88
89 int dotIndex = k.indexOf('.');
90 if (dotIndex <= 0) {
91 continue;
92 }
93
94 String artifactId = k.substring(0, dotIndex);
95
96
97 if (!props.containsKey(artifactId + PROP_VERSION) ||
98 !props.containsKey(artifactId + PROP_BUILD_DATE) ||
99 !props.containsKey(artifactId + PROP_COMMIT_DATE) ||
100 !props.containsKey(artifactId + PROP_SHORT_COMMIT_HASH) ||
101 !props.containsKey(artifactId + PROP_LONG_COMMIT_HASH) ||
102 !props.containsKey(artifactId + PROP_REPO_STATUS)) {
103 continue;
104 }
105
106 artifactIds.add(artifactId);
107 }
108
109 Map<String, Version> versions = new TreeMap<>();
110 for (String artifactId: artifactIds) {
111 versions.put(
112 artifactId,
113 new Version(
114 artifactId,
115 props.getProperty(artifactId + PROP_VERSION),
116 parseIso8601(props.getProperty(artifactId + PROP_BUILD_DATE)),
117 parseIso8601(props.getProperty(artifactId + PROP_COMMIT_DATE)),
118 props.getProperty(artifactId + PROP_SHORT_COMMIT_HASH),
119 props.getProperty(artifactId + PROP_LONG_COMMIT_HASH),
120 props.getProperty(artifactId + PROP_REPO_STATUS)));
121 }
122
123 return versions;
124 }
125
126 private static long parseIso8601(String value) {
127 try {
128 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(value).getTime();
129 } catch (ParseException ignored) {
130 return 0;
131 }
132 }
133
134
135
136
137 public static void main(String[] args) {
138 for (Version v: identify().values()) {
139 System.err.println(v);
140 }
141 }
142
143 private final String artifactId;
144 private final String artifactVersion;
145 private final long buildTimeMillis;
146 private final long commitTimeMillis;
147 private final String shortCommitHash;
148 private final String longCommitHash;
149 private final String repositoryStatus;
150
151 private Version(
152 String artifactId, String artifactVersion,
153 long buildTimeMillis, long commitTimeMillis,
154 String shortCommitHash, String longCommitHash, String repositoryStatus) {
155 this.artifactId = artifactId;
156 this.artifactVersion = artifactVersion;
157 this.buildTimeMillis = buildTimeMillis;
158 this.commitTimeMillis = commitTimeMillis;
159 this.shortCommitHash = shortCommitHash;
160 this.longCommitHash = longCommitHash;
161 this.repositoryStatus = repositoryStatus;
162 }
163
164 public String artifactId() {
165 return artifactId;
166 }
167
168 public String artifactVersion() {
169 return artifactVersion;
170 }
171
172 public long buildTimeMillis() {
173 return buildTimeMillis;
174 }
175
176 public long commitTimeMillis() {
177 return commitTimeMillis;
178 }
179
180 public String shortCommitHash() {
181 return shortCommitHash;
182 }
183
184 public String longCommitHash() {
185 return longCommitHash;
186 }
187
188 public String repositoryStatus() {
189 return repositoryStatus;
190 }
191
192 @Override
193 public String toString() {
194 return artifactId + '-' + artifactVersion + '.' + shortCommitHash +
195 ("clean".equals(repositoryStatus)? "" : " (repository: " + repositoryStatus + ')');
196 }
197 }