View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Not part of any public API
29   *
30   * @author Kristian Rosenvold
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       * Merge in another section
88       *
89       * @param target The target manifest of the merge
90       * @param section the section to be merged with this one.
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              // the merge file always wins
97              target.put(key, value);
98          }
99      }
100 }