Last active
May 3, 2016 00:51
-
-
Save kylestev/17e9e58513ef53bb2c59923e61a17f62 to your computer and use it in GitHub Desktop.
Used for grabbing a value associated with a key from a manifest in your classpath.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.jar.Manifest; | |
import java.util.stream.Stream; | |
/** | |
* Created by kylestev | |
*/ | |
public class Manifests { | |
private static final String MANIFEST_FILE = "META-INF/MANIFEST.MF"; | |
private static List<URL> manifestUrls() { | |
try { | |
final ClassLoader current = Thread.currentThread().getContextClassLoader(); | |
return Collections.list(current.getResources(MANIFEST_FILE)); | |
} catch (IOException e) { | |
return Collections.emptyList(); | |
} | |
} | |
private static Manifest loadManifest(final URL url) { | |
try { | |
return new Manifest(url.openStream()); | |
} catch (IOException e) { | |
return null; | |
} | |
} | |
private static Stream<Manifest> load() { | |
return manifestUrls() | |
.stream() | |
.map(Manifests::loadManifest) | |
.filter(m -> m != null); | |
} | |
public static Optional<String> get(final String key) { | |
return load() | |
.map(m -> m.getMainAttributes().getValue(key)) | |
.filter(v -> v != null) | |
.findFirst(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment