View Javadoc

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 992817 2010-09-05 16:32:58Z 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 Artifact artifact;
46  
47      // Those are set by the configuration
48  
49      private String groupId;
50  
51      private String artifactId;
52  
53      private String classifier;
54  
55      protected String bundleDir;
56  
57      protected String bundleFileName;
58  
59      protected Boolean excluded = Boolean.FALSE;
60  
61      private String uri;
62  
63      protected Boolean unpack = null;
64  
65      protected String altDeploymentDescriptor;
66  
67      private String moduleId;
68  
69      // This is injected once the module has been built.
70  
71      protected EarExecutionContext earExecutionContext;
72  
73      /**
74       * Empty constructor to be used when the module
75       * is built based on the configuration.
76       */
77      public AbstractEarModule()
78      {
79      }
80  
81      /**
82       * Creates an ear module from the artifact.
83       *
84       * @param a the artifact
85       */
86      public AbstractEarModule( Artifact a )
87      {
88          this.artifact = a;
89          this.groupId = a.getGroupId();
90          this.artifactId = a.getArtifactId();
91          this.classifier = a.getClassifier();
92          this.bundleDir = null;
93      }
94  
95      public void setEarExecutionContext( EarExecutionContext earExecutionContext )
96      {
97          this.earExecutionContext = earExecutionContext;
98      }
99  
100     public void resolveArtifact( Set artifacts )
101         throws EarPluginException, MojoFailureException
102     {
103         // If the artifact is already set no need to resolve it
104         if ( artifact == null )
105         {
106             // Make sure that at least the groupId and the artifactId are specified
107             if ( groupId == null || artifactId == null )
108             {
109                 throw new MojoFailureException(
110                     "Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" );
111             }
112             final ArtifactRepository ar = earExecutionContext.getArtifactRepository();
113             artifact = ar.getUniqueArtifact( groupId, artifactId, getType(), classifier );
114             // Artifact has not been found
115             if ( artifact == null )
116             {
117                 Set candidates = ar.getArtifacts( groupId, artifactId, getType() );
118                 if ( candidates.size() > 1 )
119                 {
120                     throw new MojoFailureException( "Artifact[" + this + "] has " + candidates.size() +
121                                                         " candidates, please provide a classifier." );
122                 }
123                 else
124                 {
125                     throw new MojoFailureException( "Artifact[" + this + "] " + "is not a dependency of the project." );
126                 }
127             }
128         }
129     }
130 
131     public Artifact getArtifact()
132     {
133         return artifact;
134     }
135 
136     public String getModuleId()
137     {
138         return moduleId;
139     }
140 
141     public String getUri()
142     {
143         if ( uri == null )
144         {
145             if ( getBundleDir() == null )
146             {
147                 uri = getBundleFileName();
148             }
149             else
150             {
151                 uri = getBundleDir() + getBundleFileName();
152             }
153         }
154         return uri;
155     }
156 
157     /**
158      * Returns the artifact's groupId.
159      *
160      * @return the group Id
161      */
162     public String getGroupId()
163     {
164         return groupId;
165     }
166 
167     /**
168      * Returns the artifact's Id.
169      *
170      * @return the artifact Id
171      */
172     public String getArtifactId()
173     {
174         return artifactId;
175     }
176 
177     /**
178      * Returns the artifact's classifier.
179      *
180      * @return the artifact classifier
181      */
182     public String getClassifier()
183     {
184         return classifier;
185     }
186 
187     /**
188      * Returns the bundle directory. If null, the module
189      * is bundled in the root of the EAR.
190      *
191      * @return the custom bundle directory
192      */
193     public String getBundleDir()
194     {
195         if ( bundleDir != null )
196         {
197             bundleDir = cleanBundleDir( bundleDir );
198         }
199         return bundleDir;
200     }
201 
202     /**
203      * Returns the bundle file name. If null, the artifact's
204      * file name is returned.
205      *
206      * @return the bundle file name
207      */
208     public String getBundleFileName()
209     {
210         if ( bundleFileName == null )
211         {
212             bundleFileName = earExecutionContext.getFileNameMapping().mapFileName( artifact );
213         }
214         return bundleFileName;
215     }
216 
217 
218     /**
219      * The alt-dd element specifies an optional URI to the post-assembly version
220      * of the deployment descriptor file for a particular Java EE module. The URI
221      * must specify the full pathname of the deployment descriptor file relative
222      * to the application's root directory.
223      *
224      * @return the alternative deployment descriptor for this module
225      */
226     public String getAltDeploymentDescriptor()
227     {
228         return altDeploymentDescriptor;
229     }
230 
231     /**
232      * Specify whether this module should be excluded or not.
233      *
234      * @return true if this module should be skipped, false otherwise
235      */
236     public boolean isExcluded()
237     {
238         return excluded.booleanValue();
239     }
240 
241     public Boolean shouldUnpack()
242     {
243         return unpack;
244     }
245 
246     /**
247      * Writes the alternative deployment descriptor if necessary.
248      *
249      * @param writer  the writer to use
250      * @param version the java EE version in use
251      */
252     protected void writeAltDeploymentDescriptor( XMLWriter writer, String version )
253     {
254         if ( getAltDeploymentDescriptor() != null )
255         {
256             writer.startElement( ALT_DD );
257             writer.writeText( getAltDeploymentDescriptor() );
258             writer.endElement();
259         }
260     }
261 
262     /**
263      * Starts a new {@link #MODULE_ELEMENT} on the specified writer, possibly
264      * including an id attribute.
265      *
266      * @param writer     the XML writer.
267      * @param generateId whether an id should be generated
268      */
269     protected void startModuleElement( XMLWriter writer, Boolean generateId )
270     {
271         writer.startElement( MODULE_ELEMENT );
272 
273         // If a moduleId is specified, always include it
274         if ( getModuleId() != null )
275         {
276             writer.addAttribute( "id", getModuleId() );
277         }
278         else if ( generateId.booleanValue() )
279         {
280             // No module id was specified but one should be generated.
281             Artifact artifact = getArtifact();
282             String generatedId =
283                 artifact.getType().toUpperCase() + "_" + artifact.getGroupId() + "." + artifact.getArtifactId();
284             if ( null != artifact.getClassifier() && artifact.getClassifier().trim().length() > 0 )
285             {
286                 generatedId += "-" + artifact.getClassifier().trim();
287             }
288             writer.addAttribute( "id", generatedId );
289         }
290     }
291 
292     public String toString()
293     {
294         StringBuffer sb = new StringBuffer();
295         sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId );
296         if ( classifier != null )
297         {
298             sb.append( ":" ).append( classifier );
299         }
300         if ( artifact != null )
301         {
302             sb.append( ":" ).append( artifact.getVersion() );
303         }
304         return sb.toString();
305     }
306 
307     /**
308      * Cleans the bundle directory so that it might be used
309      * properly.
310      *
311      * @param bundleDir the bundle directory to clean
312      * @return the cleaned bundle directory
313      */
314     static String cleanBundleDir( String bundleDir )
315     {
316         if ( bundleDir == null )
317         {
318             return bundleDir;
319         }
320 
321         // Using slashes
322         bundleDir = bundleDir.replace( '\\', '/' );
323 
324         // Remove '/' prefix if any so that directory is a relative path
325         if ( bundleDir.startsWith( "/" ) )
326         {
327             bundleDir = bundleDir.substring( 1, bundleDir.length() );
328         }
329 
330         if ( bundleDir.length() > 0 && !bundleDir.endsWith( "/" ) )
331         {
332             // Adding '/' suffix to specify a directory structure if it is not empty
333             bundleDir = bundleDir + "/";
334         }
335 
336         return bundleDir;
337     }
338 
339     /**
340      * Specify if the objects are both null or both equal.
341      *
342      * @param first  the first object
343      * @param second the second object
344      * @return true if parameters are either both null or equal
345      */
346     static boolean areNullOrEqual( Object first, Object second )
347     {
348         if ( first != null )
349         {
350             return first.equals( second );
351         }
352         else
353         {
354             return second == null;
355         }
356     }
357 
358     /**
359      * Sets the URI of the module explicitely for testing purposes.
360      *
361      * @param uri the uri
362      */
363     void setUri( String uri )
364     {
365         this.uri = uri;
366 
367     }
368 }