GlueGen: src/java/com/jogamp/common/util/VersionUtil.java Source File

GlueGen v2.6.0 GlueGen, Native Binding Generator for Java™ (public API).

VersionUtil.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import com.jogamp.common.os.AndroidVersion;
32import com.jogamp.common.os.Platform;
33
34import java.io.IOException;
35import java.io.InputStream;
36import java.net.URL;
37import java.util.ArrayList;
38import java.util.Arrays;
39import java.util.Enumeration;
40import java.util.Iterator;
41import java.util.List;
42import java.util.Set;
43import java.util.jar.Attributes;
44import java.util.jar.Manifest;
45
46import jogamp.common.Debug;
47import jogamp.common.os.PlatformPropsImpl;
48
49 public class VersionUtil {
50 private static final boolean DEBUG = Debug.debug("VersionUtil");
51
52 public static final String SEPERATOR = "-----------------------------------------------------------------------------------------------------";
53
54 /**
55 * Appends environment information like OS, JVM and CPU architecture properties to the StringBuilder.
56 */
57 public static StringBuilder getPlatformInfo(StringBuilder sb) {
58 if(null == sb) {
59 sb = new StringBuilder();
60 }
61
62 sb.append(SEPERATOR).append(Platform.getNewline());
63
64 // environment
65 sb.append("Platform: ").append(Platform.getOSType()).append(" / ").append(Platform.getOSName()).append(' ').append(Platform.getOSVersion()).append(" (").append(Platform.getOSVersionNumber()).append("), ");
66 sb.append(Platform.getArchName()).append(" (").append(Platform.getCPUType()).append(", ").append(Platform.getABIType()).append("), ");
67 sb.append(Runtime.getRuntime().availableProcessors()).append(" cores, ").append("littleEndian ").append(PlatformPropsImpl.LITTLE_ENDIAN);
68 sb.append(Platform.getNewline());
69 if( Platform.OSType.ANDROID == PlatformPropsImpl.OS_TYPE ) {
70 sb.append("Platform: Android Version: ").append(AndroidVersion.CODENAME).append(", ");
71 sb.append(AndroidVersion.RELEASE).append(" [").append(AndroidVersion.RELEASE).append("], SDK: ").append(AndroidVersion.SDK_INT).append(", ").append(AndroidVersion.SDK_NAME);
72 sb.append(Platform.getNewline());
73 }
74
75 Platform.getMachineDataInfo().toString(sb).append(Platform.getNewline());
76
77 // JVM/JRE
78 sb.append("Platform: Java Version: ").append(Platform.getJavaVersion()).append(" (").append(Platform.getJavaVersionNumber()).append("u").append(PlatformPropsImpl.JAVA_VERSION_UPDATE).append("), VM: ").append(Platform.getJavaVMName());
79 sb.append(", Runtime: ").append(Platform.getJavaRuntimeName()).append(Platform.getNewline());
80 sb.append("Platform: Java Vendor: ").append(Platform.getJavaVendor()).append(", ").append(Platform.getJavaVendorURL());
81 if( PlatformPropsImpl.JAVA_21 ) {
82 sb.append(", Java21");
83 } else if( PlatformPropsImpl.JAVA_17 ) {
84 sb.append(", Java17");
85 } else if( PlatformPropsImpl.JAVA_9 ) {
86 sb.append(", Java9");
87 } else if( PlatformPropsImpl.JAVA_6 ) {
88 sb.append(", Java6");
89 } else if( PlatformPropsImpl.JAVA_SE ) {
90 sb.append(", JavaSE");
91 }
92 sb.append(", dynamicLib: ").append(PlatformPropsImpl.useDynamicLibraries);
93 sb.append(", AWT enabled: ").append(Platform.AWT_AVAILABLE);
94 sb.append(Platform.getNewline()).append(SEPERATOR);
95
96 return sb;
97 }
98
99 /**
100 * Prints platform info.
101 * @see #getPlatformInfo(java.lang.StringBuilder)
102 */
103 public static String getPlatformInfo() {
104 return getPlatformInfo(null).toString();
105 }
106
107 /** Returns the manifest's Attributes.Name.EXTENSION_NAME, i.e. package-name. */
108 public static String getExtensionName(final Manifest mf) {
109 return getExtensionName(mf.getMainAttributes());
110 }
111 /** Returns the attributes' Attributes.Name.EXTENSION_NAME, i.e. package-name. */
112 public static String getExtensionName(final Attributes attributes) {
113 if(null != attributes) {
114 return attributes.getValue( Attributes.Name.EXTENSION_NAME );
115 }
116 return null;
117 }
118
119 /**
120 * Returns the manifest of the jar which contains the specified extension.
121 * The provided ClassLoader is used for resource loading, while excluding its parent-classloader resources.
122 * @param cl ClassLoader used to locate the manifest.
123 * @param extension The value of the 'Extension-Name' jar-manifest attribute; used to identify the manifest.
124 * @return the requested manifest, or null if not matching or none found.
125 */
126 public static Manifest getManifest(final ClassLoader cl, final String extension) {
127 return getManifest(cl, new String[] { extension } );
128 }
129
130 /**
131 * Returns the manifest of the jar which contains one of the specified extensions.
132 * The provided ClassLoader is used for resource loading, while excluding its parent-classloader resources.
133 * @param cl ClassLoader used to locate the manifest.
134 * @param extensions The values of many 'Extension-Name's jar-manifest attribute; used to identify the manifest.
135 * Matching is applied in decreasing order, i.e. first element is favored over the second, etc.
136 * @return the requested manifest, or null if not matching or none found.
137 */
138 public static Manifest getManifest(final ClassLoader cl, final String[] extensions) {
139 return getManifest(cl, extensions, false);
140 }
141
142 /**
143 * Returns the manifest of the jar which contains one of the specified extensions.
144 * The provided ClassLoader is used for resource loading, while excluding its parent-classloader resources.
145 * @param cl ClassLoader used to locate the manifest.
146 * @param extensions The values of many 'Extension-Name's jar-manifest attribute; used to identify the manifest.
147 * Matching is applied in decreasing order, i.e. first element is favored over the second, etc.
148 * @param acceptFirst pass true to accept the first Manifest w/ an extension-name if non matching extension is found
149 * @return the requested manifest, otherwise the first found manifest w/ an extension-name or null when no manifest found.
150 */
151 public static Manifest getManifest(final ClassLoader cl, final String[] extensions, final boolean acceptFirst) {
152 final Manifest[] extManifests = new Manifest[extensions.length];
153 Manifest firstManifest = null;
154 try {
155 if( DEBUG ) {
156 System.err.println();
157 System.err.println("XXXX: getManifest: acceptFirst "+acceptFirst+", extensions "+Arrays.asList(extensions));
158 }
159 final List<URL> resources = getResources(cl, "META-INF/MANIFEST.MF");
160 final List<URL> parentResources = getResources(cl.getParent(), "META-INF/MANIFEST.MF");
161 if( DEBUG ) {
162 for(final URL r : parentResources) {
163 System.err.println("XXXX: drop parent "+r);
164 }
165 }
166 resources.removeAll(parentResources);
167 if( DEBUG ) {
168 for(final URL r : resources) {
169 System.err.println("XXXX: uniq "+r);
170 }
171 }
172 for(final URL resource : resources) {
173 final InputStream is = resource.openStream();
174 final Manifest manifest;
175 try {
176 manifest = new Manifest(is);
177 } finally {
178 IOUtil.close(is, false);
179 }
180 final Attributes attributes = manifest.getMainAttributes();
181 final String extensionName = getExtensionName(attributes);
182 if( DEBUG ) { System.err.println("XXXX: ext-name "+extensionName+", resource "+resource); }
183 if( null != extensionName && null != attributes) {
184 if( null == firstManifest ) {
185 firstManifest = manifest;
186 }
187 for(int i=0; i < extensions.length && null == extManifests[i]; i++) {
188 final String extension = extensions[i];
189 if( extension.equals( extensionName ) ) {
190 if( 0 == i ) {
191 return manifest; // 1st one has highest prio - done
192 }
193 extManifests[i] = manifest;
194 }
195 }
196 }
197 }
198 } catch (final IOException ex) {
199 throw new RuntimeException("Unable to read manifest.", ex);
200 }
201 for(int i=1; i<extManifests.length; i++) {
202 if( null != extManifests[i] ) {
203 final Manifest mf = extManifests[i];
204 return mf;
205 }
206 }
207 if( acceptFirst && null != firstManifest ) {
208 return firstManifest;
209 }
210 return null;
211 }
212 private static List<URL> getResources(final ClassLoader cl, final String name) throws IOException {
213 final List<URL> res = new ArrayList<URL>();
214 if( null != cl ) {
215 final Enumeration<URL> resources = cl.getResources(name);
216 while (resources.hasMoreElements()) {
217 final URL resource = resources.nextElement();
218 res.add(resource);
219 }
220 }
221 return res;
222 }
223
224 public static StringBuilder getFullManifestInfo(final Manifest mf, StringBuilder sb) {
225 if(null==mf) {
226 return sb;
227 }
228
229 if(null==sb) {
230 sb = new StringBuilder();
231 }
232
233 final Attributes attr = mf.getMainAttributes();
234 final Set<Object> keys = attr.keySet();
235 for(final Iterator<Object> iter=keys.iterator(); iter.hasNext(); ) {
236 final Attributes.Name key = (Attributes.Name) iter.next();
237 final String val = attr.getValue(key);
238 sb.append(" ");
239 sb.append(key);
240 sb.append(" = ");
241 sb.append(val);
242 sb.append(Platform.getNewline());
243 }
244 return sb;
245 }
246}
247
com.jogamp.common.os.AndroidVersion
Definition: AndroidVersion.java:37
com.jogamp.common.os.AndroidVersion.SDK_INT
static final int SDK_INT
SDK Version number, key to VERSION_CODES.
Definition: AndroidVersion.java:60
com.jogamp.common.os.AndroidVersion.CODENAME
static final String CODENAME
Development codename, or the string "REL" for official release.
Definition: AndroidVersion.java:51
com.jogamp.common.os.AndroidVersion.SDK_NAME
static final String SDK_NAME
SDK Version string.
Definition: AndroidVersion.java:63
com.jogamp.common.os.AndroidVersion.RELEASE
static final String RELEASE
official build version string
Definition: AndroidVersion.java:57
com.jogamp.common.os.MachineDataInfo.toString
StringBuilder toString(StringBuilder sb)
Definition: MachineDataInfo.java:355
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.getOSVersionNumber
static VersionNumber getOSVersionNumber()
Returns the OS version number.
Definition: Platform.java:386
com.jogamp.common.os.Platform.getOSVersion
static String getOSVersion()
Returns the OS version.
Definition: Platform.java:379
com.jogamp.common.os.Platform.getJavaVersion
static String getJavaVersion()
Returns the JAVA version.
Definition: Platform.java:489
com.jogamp.common.os.Platform.getJavaVMName
static String getJavaVMName()
Returns the JAVA VM name.
Definition: Platform.java:468
com.jogamp.common.os.Platform.getABIType
static ABIType getABIType()
Returns the ABI type.
Definition: Platform.java:444
com.jogamp.common.os.Platform.getJavaVersionNumber
static VersionNumber getJavaVersionNumber()
Returns the JAVA version number.
Definition: Platform.java:496
com.jogamp.common.os.Platform.getJavaVendorURL
static String getJavaVendorURL()
Returns the JAVA vendor url.
Definition: Platform.java:482
com.jogamp.common.os.Platform.getOSType
static OSType getOSType()
Returns the OS type.
Definition: Platform.java:401
com.jogamp.common.os.Platform.getMachineDataInfo
static MachineDataInfo getMachineDataInfo()
Returns the MachineDataInfo of the running machine.
Definition: Platform.java:510
com.jogamp.common.os.Platform.getJavaRuntimeName
static String getJavaRuntimeName()
Returns the JAVA runtime name.
Definition: Platform.java:475
com.jogamp.common.os.Platform.getOSName
static String getOSName()
Returns the OS name.
Definition: Platform.java:372
com.jogamp.common.os.Platform.getArchName
static String getArchName()
Returns the CPU architecture String.
Definition: Platform.java:393
com.jogamp.common.os.Platform.getJavaVendor
static String getJavaVendor()
Returns the JAVA vendor.
Definition: Platform.java:461
com.jogamp.common.os.Platform.getNewline
static String getNewline()
Returns the platform's line separator.
Definition: Platform.java:503
com.jogamp.common.os.Platform.AWT_AVAILABLE
static final boolean AWT_AVAILABLE
true if AWT is available and not in headless mode, otherwise false.
Definition: Platform.java:281
com.jogamp.common.os.Platform.getCPUType
static CPUType getCPUType()
Returns the CPU architecture type.
Definition: Platform.java:415
com.jogamp.common.util.IOUtil
Definition: IOUtil.java:69
com.jogamp.common.util.IOUtil.close
static void close(final Closeable stream, final boolean throwRuntimeException)
Definition: IOUtil.java:1434
com.jogamp.common.util.VersionUtil
Definition: VersionUtil.java:49
com.jogamp.common.util.VersionUtil.getManifest
static Manifest getManifest(final ClassLoader cl, final String[] extensions, final boolean acceptFirst)
Returns the manifest of the jar which contains one of the specified extensions.
Definition: VersionUtil.java:151
com.jogamp.common.util.VersionUtil.getPlatformInfo
static StringBuilder getPlatformInfo(StringBuilder sb)
Appends environment information like OS, JVM and CPU architecture properties to the StringBuilder.
Definition: VersionUtil.java:57
com.jogamp.common.util.VersionUtil.getManifest
static Manifest getManifest(final ClassLoader cl, final String[] extensions)
Returns the manifest of the jar which contains one of the specified extensions.
Definition: VersionUtil.java:138
com.jogamp.common.util.VersionUtil.SEPERATOR
static final String SEPERATOR
Definition: VersionUtil.java:52
com.jogamp.common.util.VersionUtil.getManifest
static Manifest getManifest(final ClassLoader cl, final String extension)
Returns the manifest of the jar which contains the specified extension.
Definition: VersionUtil.java:126
com.jogamp.common.util.VersionUtil.getExtensionName
static String getExtensionName(final Manifest mf)
Returns the manifest's Attributes.Name.EXTENSION_NAME, i.e.
Definition: VersionUtil.java:108
com.jogamp.common.util.VersionUtil.getPlatformInfo
static String getPlatformInfo()
Prints platform info.
Definition: VersionUtil.java:103
com.jogamp.common.util.VersionUtil.getExtensionName
static String getExtensionName(final Attributes attributes)
Returns the attributes' Attributes.Name.EXTENSION_NAME, i.e.
Definition: VersionUtil.java:112
com.jogamp.common.util.VersionUtil.getFullManifestInfo
static StringBuilder getFullManifestInfo(final Manifest mf, StringBuilder sb)
Definition: VersionUtil.java:224
com.jogamp.common.os.Platform.OSType
Definition: Platform.java:60
com.jogamp.common.os.Platform.OSType.ANDROID
ANDROID
Definition: Platform.java:61