jaulib v1.3.0
Jau Support Library (C++, Java, ..)
JauVersion.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2010 Gothel Software e.K.
5 * Copyright (c) 2010 JogAmp Community
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package org.jau.util;
28
29import java.util.Iterator;
30import java.util.Set;
31import java.util.jar.Attributes;
32import java.util.jar.Manifest;
33
34import org.jau.sys.AndroidUtil;
35import org.jau.sys.PlatformProps;
36
37public class JauVersion {
38
39 /** See {@link #getImplementationBuild()} */
40 public static final Attributes.Name IMPLEMENTATION_BUILD = new Attributes.Name("Implementation-Build");
41 /** See {@link #getImplementationBranch()} */
42 public static final Attributes.Name IMPLEMENTATION_BRANCH = new Attributes.Name("Implementation-Branch");
43 /** See {@link #getImplementationCommit()} */
44 public static final Attributes.Name IMPLEMENTATION_COMMIT = new Attributes.Name("Implementation-Commit");
45 /** See {@link #getImplementationSHASources()} */
46 public static final Attributes.Name IMPLEMENTATION_SHA_SOURCES = new Attributes.Name("Implementation-SHA-Sources");
47 /** See {@link #getImplementationSHAClasses()} */
48 public static final Attributes.Name IMPLEMENTATION_SHA_CLASSES = new Attributes.Name("Implementation-SHA-Classes");
49 /** See {@link #getImplementationSHAClassesThis()} */
50 public static final Attributes.Name IMPLEMENTATION_SHA_CLASSES_THIS = new Attributes.Name("Implementation-SHA-Classes-this");
51 /** See {@link #getImplementationSHANatives()} */
52 public static final Attributes.Name IMPLEMENTATION_SHA_NATIVES = new Attributes.Name("Implementation-SHA-Natives");
53 /** See {@link #getImplementationSHANativesThis()} */
54 public static final Attributes.Name IMPLEMENTATION_SHA_NATIVES_THIS = new Attributes.Name("Implementation-SHA-Natives-this");
55
56 /** For FAT jaulib jar file */
57 private static final String packageNameFAT = "org.jau";
58
59 private final String packageName;
60 private final Manifest mf;
61 private final int hash;
62 private final Attributes mainAttributes;
63 private final Set<?>/*<Attributes.Name>*/ mainAttributeNames;
64
65 private final String androidPackageVersionName;
66
67 protected JauVersion(final String packageName, final Manifest mf) {
68 if( null != mf ) {
69 // use provided valid data
70 this.mf = mf;
71 this.packageName = packageName;
72 } else {
73 // try FAT jar file
74 final Manifest fatMF = VersionUtil.getManifest(JauVersion.class.getClassLoader(), packageNameFAT);
75 if( null != fatMF ) {
76 // use FAT jar file
77 this.mf = fatMF;
78 this.packageName = packageNameFAT;
79 } else {
80 // use faulty data, unresolvable ..
81 this.mf = new Manifest();
82 this.packageName = packageName;
83 }
84 }
85 this.hash = this.mf.hashCode();
86 mainAttributes = this.mf.getMainAttributes();
87 mainAttributeNames = mainAttributes.keySet();
88 androidPackageVersionName = AndroidUtil.getPackageInfoVersionName(this.packageName); // null if !Android
89 }
90
91 @Override
92 public final int hashCode() {
93 return hash;
94 }
95
96 @Override
97 public final boolean equals(final Object o) {
98 if (o instanceof JauVersion) {
99 return mf.equals(((JauVersion) o).getManifest());
100 }
101 return false;
102 }
103
104 public final Manifest getManifest() {
105 return mf;
106 }
107
108 public final String getPackageName() {
109 return packageName;
110 }
111
112 public final String getAttribute(final Attributes.Name attributeName) {
113 return (null != attributeName) ? (String) mainAttributes.get(attributeName) : null;
114 }
115
116 public final String getAttribute(final String attributeName) {
117 return getAttribute(getAttributeName(attributeName));
118 }
119
120 public final Attributes.Name getAttributeName(final String attributeName) {
121 for (final Iterator<?> iter = mainAttributeNames.iterator(); iter.hasNext();) {
122 final Attributes.Name an = (Attributes.Name) iter.next();
123 if (an.toString().equals(attributeName)) {
124 return an;
125 }
126 }
127 return null;
128 }
129
130 /**
131 * @return set of type {@link Attributes.Name}, disguised as anonymous
132 */
133 public final Set<?>/*<Attributes.Name>*/ getAttributeNames() {
134 return mainAttributeNames;
135 }
136
137 public final String getExtensionName() {
138 if(null != androidPackageVersionName) {
139 return packageName;
140 }
141 return this.getAttribute(Attributes.Name.EXTENSION_NAME);
142 }
143
144 /**
145 * Returns the implementation build number, e.g. <code>2.0-b456-20130328</code>.
146 */
147 public final String getImplementationBuild() {
149 }
150
151 /**
152 * Returns the SCM branch name
153 */
154 public final String getImplementationBranch() {
156 }
157
158 /**
159 * Returns the SCM version of the last commit, e.g. git's sha1
160 */
161 public final String getImplementationCommit() {
163 }
164
165 /**
166 * Returns the SHA of all concatenated source files of the whole project
167 */
168 public final String getImplementationSHASources() {
170 }
171
172 /**
173 * Returns the SHA of all concatenated class files of all build classes
174 */
175 public final String getImplementationSHAClasses() {
177 }
178
179 /**
180 * Returns the SHA of all concatenated class files of the local (jar) package subset
181 */
182 public final String getImplementationSHAClassesThis() {
184 }
185
186 /**
187 * Returns the SHA of all concatenated native library files of all build libs
188 */
189 public final String getImplementationSHANatives() {
191 }
192
193 /**
194 * Returns the SHA of all concatenated native library files of the local (jar) package subset
195 */
196 public final String getImplementationSHANativesThis() {
198 }
199
200 public final String getImplementationTitle() {
201 return this.getAttribute(Attributes.Name.IMPLEMENTATION_TITLE);
202 }
203
204 public final String getImplementationVendor() {
205 return this.getAttribute(Attributes.Name.IMPLEMENTATION_VENDOR);
206 }
207
208 /**
209 * Returns the {@link Attributes.Name#IMPLEMENTATION_VERSION IMPLEMENTATION_VERSION}.
210 * <p>
211 * E.g. <code>2.0.2-rc-20130328</code> for snapshots prior to <code>2.0.2</code> release
212 * and <code>2.0.2</code> for the upcoming release.
213 * </p>
214 */
215 public final String getImplementationVersion() {
216 return this.getAttribute(Attributes.Name.IMPLEMENTATION_VERSION);
217 }
218
219 public final String getAndroidPackageVersionName() {
220 return androidPackageVersionName;
221 }
222
223 public final String getSpecificationTitle() {
224 return this.getAttribute(Attributes.Name.SPECIFICATION_TITLE);
225 }
226
227 public final String getSpecificationVendor() {
228 return this.getAttribute(Attributes.Name.SPECIFICATION_VENDOR);
229 }
230
231 public final String getSpecificationVersion() {
232 return this.getAttribute(Attributes.Name.SPECIFICATION_VERSION);
233 }
234
235 public final StringBuilder getFullManifestInfo(final StringBuilder sb) {
237 }
238
239 public StringBuilder getManifestInfo(StringBuilder sb) {
240 if(null==sb) {
241 sb = new StringBuilder();
242 }
243 final String nl = PlatformProps.NEWLINE;
244 sb.append("Package: ").append(getPackageName()).append(nl);
245 sb.append("Extension Name: ").append(getExtensionName()).append(nl);
246 sb.append("Specification Title: ").append(getSpecificationTitle()).append(nl);
247 sb.append("Specification Vendor: ").append(getSpecificationVendor()).append(nl);
248 sb.append("Specification Version: ").append(getSpecificationVersion()).append(nl);
249 sb.append("Implementation Title: ").append(getImplementationTitle()).append(nl);
250 sb.append("Implementation Vendor: ").append(getImplementationVendor()).append(nl);
251 sb.append("Implementation Version: ").append(getImplementationVersion()).append(nl);
252 sb.append("Implementation Build: ").append(getImplementationBuild()).append(nl);
253 sb.append("Implementation Branch: ").append(getImplementationBranch()).append(nl);
254 sb.append("Implementation Commit: ").append(getImplementationCommit()).append(nl);
255 sb.append("Implementation SHA Sources: ").append(getImplementationSHASources()).append(nl);
256 sb.append("Implementation SHA Classes: ").append(getImplementationSHAClasses()).append(nl);
257 sb.append("Implementation SHA Classes-this: ").append(getImplementationSHAClassesThis()).append(nl);
258 sb.append("Implementation SHA Natives: ").append(getImplementationSHANatives()).append(nl);
259 sb.append("Implementation SHA Natives-this: ").append(getImplementationSHANativesThis()).append(nl);
260 if(null != getAndroidPackageVersionName()) {
261 sb.append("Android Package Version: ").append(getAndroidPackageVersionName()).append(nl);
262 }
263 return sb;
264 }
265
266 public StringBuilder toString(StringBuilder sb) {
267 if(null==sb) {
268 sb = new StringBuilder();
269 }
270
271 sb.append(VersionUtil.SEPERATOR).append(PlatformProps.NEWLINE);
272 getManifestInfo(sb);
273 sb.append(VersionUtil.SEPERATOR);
274
275 return sb;
276 }
277
278 @Override
279 public String toString() {
280 return toString(null).toString();
281 }
282}
static final String getPackageInfoVersionName(final String packageName)
Platform Properties derived from Java properties.
static final String NEWLINE
static final Attributes.Name IMPLEMENTATION_SHA_SOURCES
See getImplementationSHASources().
Definition: JauVersion.java:46
final String getImplementationVersion()
Returns the IMPLEMENTATION_VERSION.
final boolean equals(final Object o)
Definition: JauVersion.java:97
static final Attributes.Name IMPLEMENTATION_SHA_CLASSES_THIS
See getImplementationSHAClassesThis().
Definition: JauVersion.java:50
static final Attributes.Name IMPLEMENTATION_BRANCH
See getImplementationBranch().
Definition: JauVersion.java:42
StringBuilder getManifestInfo(StringBuilder sb)
final Manifest getManifest()
final String getImplementationTitle()
final String getImplementationBranch()
Returns the SCM branch name.
final String getSpecificationVendor()
final String getSpecificationVersion()
final String getImplementationSHANatives()
Returns the SHA of all concatenated native library files of all build libs.
JauVersion(final String packageName, final Manifest mf)
Definition: JauVersion.java:67
final String getAttribute(final String attributeName)
final Set<?> getAttributeNames()
final String getAndroidPackageVersionName()
static final Attributes.Name IMPLEMENTATION_SHA_NATIVES_THIS
See getImplementationSHANativesThis().
Definition: JauVersion.java:54
final String getSpecificationTitle()
static final Attributes.Name IMPLEMENTATION_COMMIT
See getImplementationCommit().
Definition: JauVersion.java:44
final String getImplementationVendor()
final String getImplementationSHAClasses()
Returns the SHA of all concatenated class files of all build classes.
final StringBuilder getFullManifestInfo(final StringBuilder sb)
final String getPackageName()
final String getImplementationSHAClassesThis()
Returns the SHA of all concatenated class files of the local (jar) package subset.
final String getImplementationSHASources()
Returns the SHA of all concatenated source files of the whole project.
final Attributes.Name getAttributeName(final String attributeName)
final String getAttribute(final Attributes.Name attributeName)
static final Attributes.Name IMPLEMENTATION_BUILD
See getImplementationBuild().
Definition: JauVersion.java:40
final String getImplementationCommit()
Returns the SCM version of the last commit, e.g.
static final Attributes.Name IMPLEMENTATION_SHA_CLASSES
See getImplementationSHAClasses().
Definition: JauVersion.java:48
final String getImplementationBuild()
Returns the implementation build number, e.g.
static final Attributes.Name IMPLEMENTATION_SHA_NATIVES
See getImplementationSHANatives().
Definition: JauVersion.java:52
final String getImplementationSHANativesThis()
Returns the SHA of all concatenated native library files of the local (jar) package subset.
StringBuilder toString(StringBuilder sb)
final String getExtensionName()
static final String SEPERATOR
static StringBuilder getFullManifestInfo(final Manifest mf, StringBuilder sb)
static Manifest getManifest(final ClassLoader cl, final String extension)
Returns the manifest of the jar which contains the specified extension.