1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.plexus.archiver.jar;
18
19 import java.io.IOException;
20 import java.util.Map;
21 import java.util.Properties;
22 import java.util.jar.Attributes;
23
24 import org.codehaus.plexus.archiver.ArchiverException;
25 import org.codehaus.plexus.util.PropertyUtils;
26
27
28
29
30
31
32 class JdkManifestFactory {
33
34 public static java.util.jar.Manifest getDefaultManifest() throws ArchiverException {
35 final java.util.jar.Manifest defaultManifest = new java.util.jar.Manifest();
36 defaultManifest.getMainAttributes().putValue("Manifest-Version", "1.0");
37
38 String createdBy = "Plexus Archiver";
39
40 final String plexusArchiverVersion = getArchiverVersion();
41
42 if (plexusArchiverVersion != null) {
43 createdBy += " " + plexusArchiverVersion;
44 }
45
46 defaultManifest.getMainAttributes().putValue("Created-By", createdBy);
47 return defaultManifest;
48 }
49
50 static String getArchiverVersion() {
51 try {
52 final Properties properties = PropertyUtils.loadProperties(JdkManifestFactory.class.getResourceAsStream(
53 "/META-INF/maven/org.codehaus.plexus/plexus-archiver/pom.properties"));
54
55 return properties != null ? properties.getProperty("version") : null;
56
57 } catch (final IOException e) {
58 throw new AssertionError(e);
59 }
60 }
61
62 public static void merge(java.util.jar.Manifest target, java.util.jar.Manifest other, boolean overwriteMain) {
63 if (other != null) {
64 final Attributes mainAttributes = target.getMainAttributes();
65 if (overwriteMain) {
66 mainAttributes.clear();
67 mainAttributes.putAll(other.getMainAttributes());
68 } else {
69 mergeAttributes(mainAttributes, other.getMainAttributes());
70 }
71
72 for (Map.Entry<String, Attributes> o : other.getEntries().entrySet()) {
73 Attributes ourSection = target.getAttributes(o.getKey());
74 Attributes otherSection = o.getValue();
75 if (ourSection == null) {
76 if (otherSection != null) {
77 target.getEntries().put(o.getKey(), (Attributes) otherSection.clone());
78 }
79 } else {
80 mergeAttributes(ourSection, otherSection);
81 }
82 }
83 }
84 }
85
86
87
88
89
90
91
92 public static void mergeAttributes(java.util.jar.Attributes target, java.util.jar.Attributes section) {
93 for (Object o : section.keySet()) {
94 java.util.jar.Attributes.Name key = (Attributes.Name) o;
95 final Object value = section.get(o);
96
97 target.put(key, value);
98 }
99 }
100 }