Skip to content

Commit

Permalink
Refactor ManifestUtil, remove conversion to URI (#1218)
Browse files Browse the repository at this point in the history
fixes #1216
  • Loading branch information
kevinherron committed Feb 9, 2024
1 parent 5806812 commit cd0f3d9
Showing 1 changed file with 21 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 the Eclipse Milo Authors
* Copyright (c) 2024 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -10,16 +10,12 @@

package org.eclipse.milo.opcua.stack.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

Expand All @@ -30,38 +26,40 @@
public class ManifestUtil {

private static final Logger logger = LoggerFactory.getLogger(ManifestUtil.class);
private static final Map<String, String> attributes = load();
private static final Map<String, String> ATTRIBUTES = readAttributes();

public static Optional<String> read(String name) {
return Optional.ofNullable(attributes.get(name));
return Optional.ofNullable(ATTRIBUTES.get(name));
}

public static boolean exists(String name) {
return attributes.containsKey(name);
return ATTRIBUTES.containsKey(name);
}

private static Map<String, String> load() {
private static Map<String, String> readAttributes() {
Map<String, String> attributes = Maps.newConcurrentMap();

for (URI uri : uris()) {
try {
attributes.putAll(load(uri.toURL()));
} catch (Throwable t) {
logger.error("load(): '{}' failed", uri, t);
try {
Enumeration<URL> resources = ManifestUtil.class
.getClassLoader()
.getResources("META-INF/MANIFEST.MF");

while (resources.hasMoreElements()) {
URL url = resources.nextElement();

attributes.putAll(readAttributes(url));
}
} catch (Throwable t) {
logger.error("readAttributes() failed", t);
}

return attributes;
}

private static Map<String, String> load(URL url) throws IOException {
return load(url.openStream());
}

private static Map<String, String> load(InputStream stream) {
Map<String, String> props = Maps.newConcurrentMap();
private static Map<String, String> readAttributes(URL url) {
Map<String, String> props = new HashMap<>();

try {
try (InputStream stream = url.openStream()) {
Manifest manifest = new Manifest(stream);
Attributes attributes = manifest.getMainAttributes();

Expand All @@ -70,34 +68,10 @@ private static Map<String, String> load(InputStream stream) {
props.put(key.toString(), value);
}
} catch (Throwable t) {
logger.error("#load(): failed", t);
} finally {
try {
stream.close();
} catch (IOException e) {
// ignored
}
logger.error("readAttributes(): '{}' failed", url, t);
}

return props;
}

private static Set<URI> uris() {
try {
Enumeration<URL> resources = ManifestUtil.class
.getClassLoader()
.getResources("META-INF/MANIFEST.MF");

Set<URI> uris = new HashSet<>();
while (resources.hasMoreElements()) {
uris.add(resources.nextElement().toURI());
}

return uris;
} catch (Throwable t) {
logger.error("uris() failed", t);
return Collections.emptySet();
}
}

}

0 comments on commit cd0f3d9

Please sign in to comment.