jaulib v1.3.0
Jau Support Library (C++, Java, ..)
VersionUtil.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.io.IOException;
30import java.io.InputStream;
31import java.net.URL;
32import java.util.Enumeration;
33import java.util.Iterator;
34import java.util.Set;
35import java.util.jar.Attributes;
36import java.util.jar.Manifest;
37
38import org.jau.io.IOUtil;
39import org.jau.sys.AndroidVersion;
40import org.jau.sys.PlatformProps;
41import org.jau.sys.RuntimeProps;
42import org.jau.sys.PlatformTypes;
43
44public class VersionUtil {
45
46 public static final String SEPERATOR = "-----------------------------------------------------------------------------------------------------";
47
48 /**
49 * Appends environment information like OS, JVM and CPU architecture properties to the StringBuilder.
50 */
51 public static StringBuilder getPlatformInfo(StringBuilder sb) {
52 if(null == sb) {
53 sb = new StringBuilder();
54 }
55
56 sb.append(SEPERATOR).append(PlatformProps.NEWLINE);
57
58 // environment
59 sb.append("Platform: ").append(PlatformProps.OS).append(" / ").append(PlatformProps.os_name).append(' ').append(PlatformProps.os_version).append(", ");
60 sb.append(PlatformProps.os_arch).append(" (").append(PlatformProps.CPU).append(", ").append(PlatformProps.ABI).append("), ");
61 sb.append(Runtime.getRuntime().availableProcessors()).append(" cores, ").append("littleEndian ").append(PlatformProps.LITTLE_ENDIAN);
62 sb.append(PlatformProps.NEWLINE);
64 sb.append("Platform: Android Version: ").append(AndroidVersion.CODENAME).append(", ");
65 sb.append(AndroidVersion.RELEASE).append(" [").append(AndroidVersion.RELEASE).append("], SDK: ").append(AndroidVersion.SDK_INT).append(", ").append(AndroidVersion.SDK_NAME);
66 sb.append(PlatformProps.NEWLINE);
67 }
68
69 if( null != RuntimeProps.MACH_DESC_RT ) {
70 RuntimeProps.MACH_DESC_RT.toString(sb).append("; (runtime)").append(PlatformProps.NEWLINE);
71 } else {
72 PlatformProps.MACH_DESC_STAT.toString(sb).append("; (static)").append(PlatformProps.NEWLINE);
73 }
74
75 // JVM/JRE
76 sb.append("Platform: Java Version: ").append(PlatformProps.JAVA_VERSION_NUMBER).append(", VM: ").append(PlatformProps.JAVA_VM_NAME);
77 sb.append(", Runtime: ").append(PlatformProps.JAVA_RUNTIME_NAME).append(PlatformProps.NEWLINE);
78 sb.append("Platform: Java Vendor: ").append(PlatformProps.JAVA_VENDOR);
80 sb.append(", Java21");
81 } else if( PlatformProps.JAVA_17 ) {
82 sb.append(", Java17");
83 } else if( PlatformProps.JAVA_9 ) {
84 sb.append(", Java9");
85 }
86 // sb.append(", dynamicLib: ").append(PlatformProps.useDynamicLibraries);
87 sb.append(PlatformProps.NEWLINE).append(SEPERATOR);
88
89 return sb;
90 }
91
92 /**
93 * Prints platform info.
94 * @see #getPlatformInfo(java.lang.StringBuilder)
95 */
96 public static String getPlatformInfo() {
97 return getPlatformInfo(null).toString();
98 }
99
100 /**
101 * Returns the manifest of the jar which contains the specified extension.
102 * The provided ClassLoader is used for resource loading.
103 * @param cl A ClassLoader which should find the manifest.
104 * @param extension The value of the 'Extension-Name' jar-manifest attribute; used to identify the manifest.
105 * @return the requested manifest or null when not found.
106 */
107 public static Manifest getManifest(final ClassLoader cl, final String extension) {
108 return getManifest(cl, new String[] { extension } );
109 }
110
111 /**
112 * Returns the manifest of the jar which contains one of the specified extensions.
113 * The provided ClassLoader is used for resource loading.
114 * @param cl A ClassLoader which should find the manifest.
115 * @param extensions The values of many 'Extension-Name's jar-manifest attribute; used to identify the manifest.
116 * Matching is applied in decreasing order, i.e. first element is favored over the second, etc.
117 * @return the requested manifest or null when not found.
118 */
119 public static Manifest getManifest(final ClassLoader cl, final String[] extensions) {
120 final Manifest[] extManifests = new Manifest[extensions.length];
121 try {
122 final Enumeration<URL> resources = cl.getResources("META-INF/MANIFEST.MF");
123 while (resources.hasMoreElements()) {
124 final InputStream is = resources.nextElement().openStream();
125 final Manifest manifest;
126 try {
127 manifest = new Manifest(is);
128 } finally {
129 IOUtil.close(is, false);
130 }
131 final Attributes attributes = manifest.getMainAttributes();
132 if(attributes != null) {
133 for(int i=0; i < extensions.length && null == extManifests[i]; i++) {
134 final String extension = extensions[i];
135 if( extension.equals( attributes.getValue( Attributes.Name.EXTENSION_NAME ) ) ) {
136 if( 0 == i ) {
137 return manifest; // 1st one has highest prio - done
138 }
139 extManifests[i] = manifest;
140 }
141 }
142 }
143 }
144 } catch (final IOException ex) {
145 throw new RuntimeException("Unable to read manifest.", ex);
146 }
147 for(int i=1; i<extManifests.length; i++) {
148 if( null != extManifests[i] ) {
149 return extManifests[i];
150 }
151 }
152 return null;
153 }
154
155 public static StringBuilder getFullManifestInfo(final Manifest mf, StringBuilder sb) {
156 if(null==mf) {
157 return sb;
158 }
159
160 if(null==sb) {
161 sb = new StringBuilder();
162 }
163
164 final Attributes attr = mf.getMainAttributes();
165 final Set<Object> keys = attr.keySet();
166 for(final Iterator<Object> iter=keys.iterator(); iter.hasNext(); ) {
167 final Attributes.Name key = (Attributes.Name) iter.next();
168 final String val = attr.getValue(key);
169 sb.append(" ");
170 sb.append(key);
171 sb.append(" = ");
172 sb.append(val);
173 sb.append(PlatformProps.NEWLINE);
174 }
175 return sb;
176 }
177}
178
static void close(final Closeable stream, final boolean throwRuntimeException)
Definition: IOUtil.java:1248
static final int SDK_INT
SDK Version number, key to VERSION_CODES.
static final String SDK_NAME
SDK Version string.
static final String RELEASE
official build version string
static final String CODENAME
Development codename, or the string "REL" for official release.
StringBuilder toString(StringBuilder sb)
Platform Properties derived from Java properties.
static final MachineDataInfo MACH_DESC_STAT
Static (not runtime) determined MachineDataInfo.
static final boolean JAVA_9
True only if being compatible w/ language level 9, e.g.
static final String JAVA_VM_NAME
static final VersionNumber os_version
static final boolean JAVA_21
True only if being compatible w/ language level 21, e.g.
static final OSType OS
static final String os_name
Lower case system property derived from 'os.name'.
static final String JAVA_VENDOR
static final boolean JAVA_17
True only if being compatible w/ language level 17, e.g.
static final String JAVA_RUNTIME_NAME
static final ABIType ABI
static final CPUType CPU
static final VersionNumber JAVA_VERSION_NUMBER
static final String NEWLINE
static final String os_arch
Lower case system property derived from ELF or 'os.arch'.
static final boolean LITTLE_ENDIAN
Exposing types describing the underlying platform.
Runtime platform properties derived from PlatformProps and runtime query.
static final MachineDataInfo MACH_DESC_RT
Runtime determined MachineDataInfo, null if not available (i.e.
static StringBuilder getPlatformInfo(StringBuilder sb)
Appends environment information like OS, JVM and CPU architecture properties to the StringBuilder.
static Manifest getManifest(final ClassLoader cl, final String[] extensions)
Returns the manifest of the jar which contains one of the specified extensions.
static String getPlatformInfo()
Prints platform info.
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.