From cd0f3d97f78de3823541229032642f8af9f00040 Mon Sep 17 00:00:00 2001 From: Kevin Herron Date: Fri, 9 Feb 2024 04:45:02 -0800 Subject: [PATCH] Refactor ManifestUtil, remove conversion to URI (#1218) fixes #1216 --- .../opcua/stack/core/util/ManifestUtil.java | 68 ++++++------------- 1 file changed, 21 insertions(+), 47 deletions(-) diff --git a/opc-ua-stack/stack-core/src/main/java/org/eclipse/milo/opcua/stack/core/util/ManifestUtil.java b/opc-ua-stack/stack-core/src/main/java/org/eclipse/milo/opcua/stack/core/util/ManifestUtil.java index c3289d3c72..e9df9e30a7 100644 --- a/opc-ua-stack/stack-core/src/main/java/org/eclipse/milo/opcua/stack/core/util/ManifestUtil.java +++ b/opc-ua-stack/stack-core/src/main/java/org/eclipse/milo/opcua/stack/core/util/ManifestUtil.java @@ -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 @@ -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; @@ -30,38 +26,40 @@ public class ManifestUtil { private static final Logger logger = LoggerFactory.getLogger(ManifestUtil.class); - private static final Map attributes = load(); + private static final Map ATTRIBUTES = readAttributes(); public static Optional 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 load() { + private static Map readAttributes() { Map attributes = Maps.newConcurrentMap(); - for (URI uri : uris()) { - try { - attributes.putAll(load(uri.toURL())); - } catch (Throwable t) { - logger.error("load(): '{}' failed", uri, t); + try { + Enumeration 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 load(URL url) throws IOException { - return load(url.openStream()); - } - - private static Map load(InputStream stream) { - Map props = Maps.newConcurrentMap(); + private static Map readAttributes(URL url) { + Map props = new HashMap<>(); - try { + try (InputStream stream = url.openStream()) { Manifest manifest = new Manifest(stream); Attributes attributes = manifest.getMainAttributes(); @@ -70,34 +68,10 @@ private static Map 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 uris() { - try { - Enumeration resources = ManifestUtil.class - .getClassLoader() - .getResources("META-INF/MANIFEST.MF"); - - Set uris = new HashSet<>(); - while (resources.hasMoreElements()) { - uris.add(resources.nextElement().toURI()); - } - - return uris; - } catch (Throwable t) { - logger.error("uris() failed", t); - return Collections.emptySet(); - } - } - }