Coverage Report - org.apache.maven.plugin.ear.AbstractEarModule
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEarModule
31%
21/67
22%
8/36
2.053
 
 1  
 package org.apache.maven.plugin.ear;
 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 org.apache.maven.artifact.Artifact;
 23  
 import org.apache.maven.plugin.MojoFailureException;
 24  
 import org.apache.maven.plugin.ear.util.ArtifactRepository;
 25  
 import org.codehaus.plexus.util.xml.XMLWriter;
 26  
 
 27  
 import java.util.Set;
 28  
 
 29  
 /**
 30  
  * A base implementation of an {@link EarModule}.
 31  
  *
 32  
  * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
 33  
  * @version $Id: AbstractEarModule.java 942855 2010-05-10 19:13:52Z krosenvold $
 34  
  */
 35  
 public abstract class AbstractEarModule
 36  
     implements EarModule
 37  
 {
 38  
 
 39  
     protected static final String MODULE_ELEMENT = "module";
 40  
 
 41  
     protected static final String JAVA_MODULE = "java";
 42  
 
 43  
     protected static final String ALT_DD = "alt-dd";
 44  
 
 45  
     private String uri;
 46  
 
 47  
     private Artifact artifact;
 48  
 
 49  
     // Those are set by the configuration
 50  
 
 51  
     private String groupId;
 52  
 
 53  
     private String artifactId;
 54  
 
 55  
     private String classifier;
 56  
 
 57  
     protected String bundleDir;
 58  
 
 59  
     protected String bundleFileName;
 60  
 
 61  4
     protected Boolean excluded = Boolean.FALSE;
 62  
 
 63  4
     protected Boolean unpack = null;
 64  
 
 65  
     protected String altDeploymentDescriptor;
 66  
 
 67  
     protected EarExecutionContext earExecutionContext;
 68  
 
 69  
     /**
 70  
      * Empty constructor to be used when the module
 71  
      * is built based on the configuration.
 72  
      */
 73  
     public AbstractEarModule()
 74  0
     {
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Creates an ear module from the artifact.
 79  
      *
 80  
      * @param a the artifact
 81  
      */
 82  
     public AbstractEarModule( Artifact a )
 83  4
     {
 84  4
         this.artifact = a;
 85  4
         this.groupId = a.getGroupId();
 86  4
         this.artifactId = a.getArtifactId();
 87  4
         this.classifier = a.getClassifier();
 88  4
         this.bundleDir = null;
 89  4
     }
 90  
 
 91  
     public void setEarExecutionContext( EarExecutionContext earExecutionContext )
 92  
     {
 93  0
         this.earExecutionContext = earExecutionContext;
 94  0
     }
 95  
 
 96  
     public void resolveArtifact( Set artifacts )
 97  
         throws EarPluginException, MojoFailureException
 98  
     {
 99  
         // If the artifact is already set no need to resolve it
 100  0
         if ( artifact == null )
 101  
         {
 102  
             // Make sure that at least the groupId and the artifactId are specified
 103  0
             if ( groupId == null || artifactId == null )
 104  
             {
 105  0
                 throw new MojoFailureException(
 106  
                     "Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" );
 107  
             }
 108  0
             final ArtifactRepository ar = earExecutionContext.getArtifactRepository();
 109  0
             artifact = ar.getUniqueArtifact( groupId, artifactId, getType(), classifier );
 110  
             // Artifact has not been found
 111  0
             if ( artifact == null )
 112  
             {
 113  0
                 Set candidates = ar.getArtifacts( groupId, artifactId, getType() );
 114  0
                 if ( candidates.size() > 1 )
 115  
                 {
 116  0
                     throw new MojoFailureException( "Artifact[" + this + "] has " + candidates.size() +
 117  
                         " candidates, please provide a classifier." );
 118  
                 }
 119  
                 else
 120  
                 {
 121  0
                     throw new MojoFailureException( "Artifact[" + this + "] " + "is not a dependency of the project." );
 122  
                 }
 123  
             }
 124  
         }
 125  0
     }
 126  
 
 127  
     public Artifact getArtifact()
 128  
     {
 129  0
         return artifact;
 130  
     }
 131  
 
 132  
     public String getUri()
 133  
     {
 134  4
         if ( uri == null )
 135  
         {
 136  0
             if ( getBundleDir() == null )
 137  
             {
 138  0
                 uri = getBundleFileName();
 139  
             }
 140  
             else
 141  
             {
 142  0
                 uri = getBundleDir() + getBundleFileName();
 143  
             }
 144  
         }
 145  4
         return uri;
 146  
     }
 147  
 
 148  
     /**
 149  
      * Returns the artifact's groupId.
 150  
      *
 151  
      * @return the group Id
 152  
      */
 153  
     public String getGroupId()
 154  
     {
 155  0
         return groupId;
 156  
     }
 157  
 
 158  
     /**
 159  
      * Returns the artifact's Id.
 160  
      *
 161  
      * @return the artifact Id
 162  
      */
 163  
     public String getArtifactId()
 164  
     {
 165  0
         return artifactId;
 166  
     }
 167  
 
 168  
     /**
 169  
      * Returns the artifact's classifier.
 170  
      *
 171  
      * @return the artifact classifier
 172  
      */
 173  
     public String getClassifier()
 174  
     {
 175  0
         return classifier;
 176  
     }
 177  
 
 178  
     /**
 179  
      * Returns the bundle directory. If null, the module
 180  
      * is bundled in the root of the EAR.
 181  
      *
 182  
      * @return the custom bundle directory
 183  
      */
 184  
     public String getBundleDir()
 185  
     {
 186  0
         if ( bundleDir != null )
 187  
         {
 188  0
             bundleDir = cleanBundleDir( bundleDir );
 189  
         }
 190  0
         return bundleDir;
 191  
     }
 192  
 
 193  
     /**
 194  
      * Returns the bundle file name. If null, the artifact's
 195  
      * file name is returned.
 196  
      *
 197  
      * @return the bundle file name
 198  
      */
 199  
     public String getBundleFileName()
 200  
     {
 201  0
         if ( bundleFileName == null )
 202  
         {
 203  0
             bundleFileName = earExecutionContext.getFileNameMapping().mapFileName( artifact );
 204  
         }
 205  0
         return bundleFileName;
 206  
     }
 207  
 
 208  
 
 209  
     /**
 210  
      * The alt-dd element specifies an optional URI to the post-assembly version
 211  
      * of the deployment descriptor file for a particular Java EE module. The URI
 212  
      * must specify the full pathname of the deployment descriptor file relative
 213  
      * to the application's root directory.
 214  
      *
 215  
      * @return the alternative deployment descriptor for this module
 216  
      */
 217  
     public String getAltDeploymentDescriptor()
 218  
     {
 219  0
         return altDeploymentDescriptor;
 220  
     }
 221  
 
 222  
     /**
 223  
      * Specify whether this module should be excluded or not.
 224  
      *
 225  
      * @return true if this module should be skipped, false otherwise
 226  
      */
 227  
     public boolean isExcluded()
 228  
     {
 229  4
         return excluded.booleanValue();
 230  
     }
 231  
 
 232  
     public Boolean shouldUnpack()
 233  
     {
 234  0
         return unpack;
 235  
     }
 236  
 
 237  
     /**
 238  
      * Writes the alternative deployment descriptor if necessary.
 239  
      *
 240  
      * @param writer  the writer to use
 241  
      * @param version the java EE version in use
 242  
      */
 243  
     protected void writeAltDeploymentDescriptor( XMLWriter writer, String version )
 244  
     {
 245  0
         if ( getAltDeploymentDescriptor() != null )
 246  
         {
 247  0
             writer.startElement( ALT_DD );
 248  0
             writer.writeText( getAltDeploymentDescriptor() );
 249  0
             writer.endElement();
 250  
         }
 251  0
     }
 252  
 
 253  
     public String toString()
 254  
     {
 255  0
         StringBuffer sb = new StringBuffer();
 256  0
         sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId );
 257  0
         if ( classifier != null )
 258  
         {
 259  0
             sb.append( ":" ).append( classifier );
 260  
         }
 261  0
         if ( artifact != null )
 262  
         {
 263  0
             sb.append( ":" ).append( artifact.getVersion() );
 264  
         }
 265  0
         return sb.toString();
 266  
     }
 267  
 
 268  
     /**
 269  
      * Cleans the bundle directory so that it might be used
 270  
      * properly.
 271  
      *
 272  
      * @param bundleDir the bundle directory to clean
 273  
      * @return the cleaned bundle directory
 274  
      */
 275  
     static String cleanBundleDir( String bundleDir )
 276  
     {
 277  5
         if ( bundleDir == null )
 278  
         {
 279  0
             return bundleDir;
 280  
         }
 281  
 
 282  
         // Using slashes
 283  5
         bundleDir = bundleDir.replace( '\\', '/' );
 284  
 
 285  
         // Remove '/' prefix if any so that directory is a relative path
 286  5
         if ( bundleDir.startsWith( "/" ) )
 287  
         {
 288  3
             bundleDir = bundleDir.substring( 1, bundleDir.length() );
 289  
         }
 290  
 
 291  5
         if ( bundleDir.length() > 0 && !bundleDir.endsWith( "/" ) )
 292  
         {
 293  
             // Adding '/' suffix to specify a directory structure if it is not empty
 294  2
             bundleDir = bundleDir + "/";
 295  
         }
 296  
 
 297  5
         return bundleDir;
 298  
     }
 299  
 
 300  
     /**
 301  
      * Specify if the objects are both null or both equal.
 302  
      *
 303  
      * @param first  the first object
 304  
      * @param second the second object
 305  
      * @return true if parameters are either both null or equal
 306  
      */
 307  
     static boolean areNullOrEqual( Object first, Object second )
 308  
     {
 309  0
         if ( first != null )
 310  
         {
 311  0
             return first.equals( second );
 312  
         }
 313  
         else
 314  
         {
 315  0
             return second == null;
 316  
         }
 317  
     }
 318  
 
 319  
     /**
 320  
      * Sets the URI of the module explicitely for testing purposes.
 321  
      *
 322  
      * @param uri the uri
 323  
      */
 324  
     void setUri(String uri) {
 325  4
         this.uri = uri;
 326  
 
 327  4
     }
 328  
 }