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