Coverage Report - org.apache.maven.plugin.ear.util.EarMavenArchiver
 
Classes in this File Line Coverage Branch Coverage Complexity
EarMavenArchiver
28%
8/28
16%
3/18
3,5
 
 1  
 package org.apache.maven.plugin.ear.util;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.List;
 23  
 import java.util.Set;
 24  
 
 25  
 import org.apache.maven.archiver.MavenArchiveConfiguration;
 26  
 import org.apache.maven.archiver.MavenArchiver;
 27  
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 28  
 import org.apache.maven.plugin.ear.EarModule;
 29  
 import org.apache.maven.project.MavenProject;
 30  
 import org.codehaus.plexus.archiver.jar.Manifest;
 31  
 import org.codehaus.plexus.archiver.jar.ManifestException;
 32  
 
 33  
 /**
 34  
  * A custom {@link MavenArchiver} implementation that takes care
 35  
  * of setting the right classpath value according to the actual
 36  
  * path of bundled files.
 37  
  *
 38  
  * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
 39  
  */
 40  
 public class EarMavenArchiver
 41  
     extends MavenArchiver
 42  
 {
 43  
     public static final String CLASS_PATH_KEY = "Class-Path";
 44  
 
 45  
     private final List<EarModule> earModules;
 46  
 
 47  
 
 48  
     /**
 49  
      * Creates an instance with the ear modules that will be packaged
 50  
      * in the EAR archive.
 51  
      *
 52  
      * @param earModules the intitialized list of ear modules
 53  
      */
 54  
     public EarMavenArchiver( List<EarModule> earModules )
 55  3
     {
 56  3
         this.earModules = earModules;
 57  3
     }
 58  
 
 59  
     public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config )
 60  
         throws ManifestException, DependencyResolutionRequiredException
 61  
     {
 62  0
         final Manifest manifest = super.getManifest( project, config );
 63  0
         if ( config.getManifest().isAddClasspath() )
 64  
         {
 65  0
             String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() );
 66  
             // Class-path can be customized. Let's make sure we don't overwrite this
 67  
             // with our custom change!
 68  0
             final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config );
 69  0
             if ( userSuppliedClassPathEntry != null )
 70  
             {
 71  0
                 earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry;
 72  
             }
 73  
 
 74  
             // Overwrite the existing one, if any
 75  0
             final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY );
 76  0
             if ( classPathAttr != null )
 77  
             {
 78  0
                 classPathAttr.setValue( earManifestClassPathEntry );
 79  
             }
 80  
             else
 81  
             {
 82  0
                 final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry );
 83  0
                 manifest.addConfiguredAttribute( attr );
 84  
             }
 85  
         }
 86  0
         return manifest;
 87  
     }
 88  
 
 89  
     /**
 90  
      * Generates the <tt>Class-Path</tt> entry of the manifest according to
 91  
      * the list of ear modules.
 92  
      *
 93  
      * @param classPathPrefix the classpath prefix to use
 94  
      * @return the <tt>Class-Path</tt> entry
 95  
      */
 96  
     protected String generateClassPathEntry( String classPathPrefix )
 97  
     {
 98  3
         final StringBuffer classpath = new StringBuffer();
 99  3
         for ( final EarModule earModule : earModules )
 100  
         {
 101  4
             if ( !earModule.isExcluded() )
 102  
             {
 103  4
                 classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " );
 104  
             }
 105  
         }
 106  3
         return classpath.toString().trim();
 107  
     }
 108  
 
 109  
     protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config )
 110  
     {
 111  0
         if ( config.getManifestEntries() != null )
 112  
         {
 113  
             @SuppressWarnings( "unchecked" )
 114  0
             final Set<String> keys = config.getManifestEntries().keySet();
 115  0
             for ( String key : keys )
 116  
             {
 117  0
                 String value = (String) config.getManifestEntries().get( key );
 118  0
                 if ( "Class-Path".equals( key ) && value != null )
 119  
                 {
 120  0
                     return value;
 121  
 
 122  
                 }
 123  
 
 124  0
             }
 125  
         }
 126  0
         return null;
 127  
     }
 128  
 }