/** Adds the source attributes to the destination map. */ private void addAll(final Attributes src, final Map dest) { for (Iterator iterator = src.entrySet().iterator(); iterator.hasNext(); ) { final Map.Entry entry = (Map.Entry) iterator.next(); // final String name = entry.getKey().toString().toLowerCase(); final String name = entry.getKey().toString(); dest.put(name, entry.getValue()); } }
private static Properties loadBuildProperties() { Properties properties = new Properties(); Manifest manifest = null; JarFile jar = null; try { URL url = BuildInfo.class.getProtectionDomain().getCodeSource().getLocation(); File file = new File(url.toURI()); jar = new JarFile(file); ZipEntry entry = jar.getEntry("META-INF/build-stamp.properties"); if (entry != null) { try (InputStream stream = jar.getInputStream(entry)) { properties.load(stream); } } manifest = jar.getManifest(); } catch (IllegalArgumentException | IOException | NullPointerException | URISyntaxException ignored) { } finally { if (jar != null) { try { jar.close(); } catch (IOException e) { // ignore } } } if (manifest == null) { return properties; } try { Attributes attributes = manifest.getAttributes("Build-Info"); Set<Entry<Object, Object>> entries = attributes.entrySet(); for (Entry<Object, Object> e : entries) { properties.put(String.valueOf(e.getKey()), String.valueOf(e.getValue())); } } catch (NullPointerException e) { // Fall through } return properties; }
private static Properties loadBuildProperties() { Properties properties = new Properties(); try { Manifest manifest = null; URL url = BuildInfo.class.getProtectionDomain().getCodeSource().getLocation(); File file = new File(url.toURI()); JarFile jar = new JarFile(file); manifest = jar.getManifest(); Attributes attributes = manifest.getMainAttributes(); Set<Entry<Object, Object>> entries = attributes.entrySet(); for (Entry<Object, Object> e : entries) { properties.put(String.valueOf(e.getKey()), String.valueOf(e.getValue())); } } catch (Exception e) { // throw new InstantiationError("Cannot load info for jar manifest." + e.getMessage()); } return properties; }
/** @tests java.util.jar.Attributes#entrySet() */ public void test_entrySet() { Set<Map.Entry<Object, Object>> entrySet = a.entrySet(); Set<Object> keySet = new HashSet<Object>(); Set<Object> valueSet = new HashSet<Object>(); Iterator<?> i; assertEquals(4, entrySet.size()); i = entrySet.iterator(); while (i.hasNext()) { java.util.Map.Entry<?, ?> e; e = (Map.Entry<?, ?>) i.next(); keySet.add(e.getKey()); valueSet.add(e.getValue()); } assertTrue("a) Should contain entry", valueSet.contains("one")); assertTrue("b) Should contain entry", valueSet.contains("two")); assertTrue("c) Should contain entry", valueSet.contains("three")); assertTrue("d) Should contain entry", valueSet.contains("four")); assertTrue("a) Should contain key", keySet.contains(new Attributes.Name("1"))); assertTrue("b) Should contain key", keySet.contains(new Attributes.Name("2"))); assertTrue("c) Should contain key", keySet.contains(new Attributes.Name("3"))); assertTrue("d) Should contain key", keySet.contains(new Attributes.Name("4"))); }
void output(Attributes attr) { Set<Entry<Object, Object>> set = attr.entrySet(); for (Entry<Object, Object> entry : set) System.out.println(entry.getKey() + " " + entry.getValue()); }