Coverage Report - org.codehaus.plexus.archiver.jar.JdkManifestFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
JdkManifestFactory
74 %
29/39
75 %
12/16
3,75
 
 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  0
 class JdkManifestFactory
 32  
 {
 33  
 
 34  
     public static java.util.jar.Manifest getDefaultManifest()
 35  
         throws ArchiverException
 36  
     {
 37  1
         final java.util.jar.Manifest defaultManifest = new java.util.jar.Manifest();
 38  1
         defaultManifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
 39  
 
 40  1
         String createdBy = "Plexus Archiver";
 41  
 
 42  1
         final String plexusArchiverVersion = getArchiverVersion();
 43  
 
 44  1
         if ( plexusArchiverVersion != null )
 45  
         {
 46  1
             createdBy += " " + plexusArchiverVersion;
 47  
         }
 48  
 
 49  1
         defaultManifest.getMainAttributes().putValue( "Created-By", createdBy );
 50  1
         return defaultManifest;
 51  
     }
 52  
 
 53  
     static String getArchiverVersion()
 54  
     {
 55  
         try
 56  
         {
 57  21
             final Properties properties = PropertyUtils.loadProperties( JdkManifestFactory.class.getResourceAsStream(
 58  0
                 "/META-INF/maven/org.codehaus.plexus/plexus-archiver/pom.properties" ) );
 59  
 
 60  21
             return properties != null
 61  0
                        ? properties.getProperty( "version" )
 62  0
                        : null;
 63  
 
 64  
         }
 65  0
         catch ( final IOException e )
 66  
         {
 67  0
             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  59
         if ( other != null )
 74  
         {
 75  7
             final Attributes mainAttributes = target.getMainAttributes();
 76  7
             if ( overwriteMain )
 77  
             {
 78  0
                 mainAttributes.clear();
 79  0
                 mainAttributes.putAll( other.getMainAttributes() );
 80  0
             }
 81  
             else
 82  
             {
 83  7
                 mergeAttributes( mainAttributes, other.getMainAttributes() );
 84  
             }
 85  
 
 86  7
             for ( Map.Entry<String, Attributes> o : other.getEntries().entrySet() )
 87  
             {
 88  3
                 Attributes ourSection = target.getAttributes( o.getKey() );
 89  3
                 Attributes otherSection = o.getValue();
 90  3
                 if ( ourSection == null )
 91  
                 {
 92  2
                     if ( otherSection != null )
 93  
                     {
 94  2
                         target.getEntries().put( o.getKey(), (Attributes) otherSection.clone() );
 95  
                     }
 96  0
                 }
 97  
                 else
 98  
                 {
 99  1
                     mergeAttributes( ourSection, otherSection );
 100  
                 }
 101  3
             }
 102  
         }
 103  59
     }
 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  8
         for ( Object o : section.keySet() )
 114  
         {
 115  17
             java.util.jar.Attributes.Name key = (Attributes.Name) o;
 116  17
             final Object value = section.get( o );
 117  
             // the merge file always wins
 118  17
             target.put( key, value );
 119  17
         }
 120  8
     }
 121  
 
 122  
 }