| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.util; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.net.URL; |
| 23 |
import java.security.CodeSource; |
| 24 |
import java.util.Optional; |
| 25 |
import java.util.jar.JarFile; |
| 26 |
import java.util.jar.Manifest; |
| 27 |
|
| 28 |
public class ManifestUtils |
| 29 |
{ |
| 30 |
private ManifestUtils() |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
public static Optional<Manifest> getManifest(Class<?> klass) |
| 35 |
{ |
| 36 |
try |
| 37 |
{ |
| 38 |
CodeSource codeSource = klass.getProtectionDomain().getCodeSource(); |
| 39 |
if (codeSource != null) |
| 40 |
{ |
| 41 |
URL location = codeSource.getLocation(); |
| 42 |
if (location != null) |
| 43 |
{ |
| 44 |
try (JarFile jarFile = new JarFile(new File(location.toURI()))) |
| 45 |
{ |
| 46 |
return Optional.of(jarFile.getManifest()); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
return Optional.empty(); |
| 51 |
} |
| 52 |
catch (Throwable x) |
| 53 |
{ |
| 54 |
return Optional.empty(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* <p>Attempts to return the version of the jar/module for the given class.</p> |
| 60 |
* <p>First, retrieves the {@code Implementation-Version} main attribute of the manifest; |
| 61 |
* if that is missing, retrieves the JPMS module version (via reflection); |
| 62 |
* if that is missing, returns an empty Optional.</p> |
| 63 |
* |
| 64 |
* @param klass the class of the jar/module to retrieve the version |
| 65 |
* @return the jar/module version, or an empty Optional |
| 66 |
*/ |
| 67 |
public static Optional<String> getVersion(Class<?> klass) |
| 68 |
{ |
| 69 |
Optional<String> version = getManifest(klass).map(Manifest::getMainAttributes) |
| 70 |
.map(attributes -> attributes.getValue("Implementation-Version")); |
| 71 |
if (version.isPresent()) |
| 72 |
return version; |
| 73 |
|
| 74 |
try |
| 75 |
{ |
| 76 |
Object module = klass.getClass().getMethod("getModule").invoke(klass); |
| 77 |
Object descriptor = module.getClass().getMethod("getDescriptor").invoke(module); |
| 78 |
return (Optional<String>)descriptor.getClass().getMethod("rawVersion").invoke(descriptor); |
| 79 |
} |
| 80 |
catch (Throwable x) |
| 81 |
{ |
| 82 |
return Optional.empty(); |
| 83 |
} |
| 84 |
} |
| 85 |
} |