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  import org.codehaus.plexus.archiver.ArchiverException;
24  import org.codehaus.plexus.util.PropertyUtils;
25  
26  /**
27   * Not part of any public API
28   *
29   * @author Kristian Rosenvold
30   */
31  class JdkManifestFactory
32  {
33  
34      public static java.util.jar.Manifest getDefaultManifest()
35          throws ArchiverException
36      {
37          final java.util.jar.Manifest defaultManifest = new java.util.jar.Manifest();
38          defaultManifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
39  
40          String createdBy = "Plexus Archiver";
41  
42          final String plexusArchiverVersion = getArchiverVersion();
43  
44          if ( plexusArchiverVersion != null )
45          {
46              createdBy += " " + plexusArchiverVersion;
47          }
48  
49          defaultManifest.getMainAttributes().putValue( "Created-By", createdBy );
50          return defaultManifest;
51      }
52  
53      static String getArchiverVersion()
54      {
55          try
56          {
57              final Properties properties = PropertyUtils.loadProperties( JdkManifestFactory.class.getResourceAsStream(
58                  "/META-INF/maven/org.codehaus.plexus/plexus-archiver/pom.properties" ) );
59  
60              return properties != null
61                         ? properties.getProperty( "version" )
62                         : null;
63  
64          }
65          catch ( final IOException e )
66          {
67              throw new AssertionError( e );
68          }
69      }
70  
71      public static void merge( java.util.jar.Manifest target, java.util.jar.Manifest other, boolean overwriteMain )
72      {
73          if ( other != null )
74          {
75              final Attributes mainAttributes = target.getMainAttributes();
76              if ( overwriteMain )
77              {
78                  mainAttributes.clear();
79                  mainAttributes.putAll( other.getMainAttributes() );
80              }
81              else
82              {
83                  mergeAttributes( mainAttributes, other.getMainAttributes() );
84              }
85  
86              for ( Map.Entry<String, Attributes> o : other.getEntries().entrySet() )
87              {
88                  Attributes ourSection = target.getAttributes( o.getKey() );
89                  Attributes otherSection = o.getValue();
90                  if ( ourSection == null )
91                  {
92                      if ( otherSection != null )
93                      {
94                          target.getEntries().put( o.getKey(), (Attributes) otherSection.clone() );
95                      }
96                  }
97                  else
98                  {
99                      mergeAttributes( ourSection, otherSection );
100                 }
101             }
102         }
103     }
104 
105     /**
106      * Merge in another section
107      *
108      * @param target The target manifest of the merge
109      * @param section the section to be merged with this one.
110      */
111     public static void mergeAttributes( java.util.jar.Attributes target, java.util.jar.Attributes section )
112     {
113         for ( Object o : section.keySet() )
114         {
115             java.util.jar.Attributes.Name key = (Attributes.Name) o;
116             final Object value = section.get( o );
117             // the merge file always wins
118             target.put( key, value );
119         }
120     }
121 
122 }