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 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      protected Boolean excluded = Boolean.FALSE;
62  
63      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      {
75      }
76  
77      /**
78       * Creates an ear module from the artifact.
79       *
80       * @param a the artifact
81       */
82      public AbstractEarModule( Artifact a )
83      {
84          this.artifact = a;
85          this.groupId = a.getGroupId();
86          this.artifactId = a.getArtifactId();
87          this.classifier = a.getClassifier();
88          this.bundleDir = null;
89      }
90  
91      public void setEarExecutionContext( EarExecutionContext earExecutionContext )
92      {
93          this.earExecutionContext = earExecutionContext;
94      }
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         if ( artifact == null )
101         {
102             // Make sure that at least the groupId and the artifactId are specified
103             if ( groupId == null || artifactId == null )
104             {
105                 throw new MojoFailureException(
106                     "Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" );
107             }
108             final ArtifactRepository ar = earExecutionContext.getArtifactRepository();
109             artifact = ar.getUniqueArtifact( groupId, artifactId, getType(), classifier );
110             // Artifact has not been found
111             if ( artifact == null )
112             {
113                 Set candidates = ar.getArtifacts( groupId, artifactId, getType() );
114                 if ( candidates.size() > 1 )
115                 {
116                     throw new MojoFailureException( "Artifact[" + this + "] has " + candidates.size() +
117                         " candidates, please provide a classifier." );
118                 }
119                 else
120                 {
121                     throw new MojoFailureException( "Artifact[" + this + "] " + "is not a dependency of the project." );
122                 }
123             }
124         }
125     }
126 
127     public Artifact getArtifact()
128     {
129         return artifact;
130     }
131 
132     public String getUri()
133     {
134         if ( uri == null )
135         {
136             if ( getBundleDir() == null )
137             {
138                 uri = getBundleFileName();
139             }
140             else
141             {
142                 uri = getBundleDir() + getBundleFileName();
143             }
144         }
145         return uri;
146     }
147 
148     /**
149      * Returns the artifact's groupId.
150      *
151      * @return the group Id
152      */
153     public String getGroupId()
154     {
155         return groupId;
156     }
157 
158     /**
159      * Returns the artifact's Id.
160      *
161      * @return the artifact Id
162      */
163     public String getArtifactId()
164     {
165         return artifactId;
166     }
167 
168     /**
169      * Returns the artifact's classifier.
170      *
171      * @return the artifact classifier
172      */
173     public String getClassifier()
174     {
175         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         if ( bundleDir != null )
187         {
188             bundleDir = cleanBundleDir( bundleDir );
189         }
190         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         if ( bundleFileName == null )
202         {
203             bundleFileName = earExecutionContext.getFileNameMapping().mapFileName( artifact );
204         }
205         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         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         return excluded.booleanValue();
230     }
231 
232     public Boolean shouldUnpack()
233     {
234         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         if ( getAltDeploymentDescriptor() != null )
246         {
247             writer.startElement( ALT_DD );
248             writer.writeText( getAltDeploymentDescriptor() );
249             writer.endElement();
250         }
251     }
252 
253     public String toString()
254     {
255         StringBuffer sb = new StringBuffer();
256         sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId );
257         if ( classifier != null )
258         {
259             sb.append( ":" ).append( classifier );
260         }
261         if ( artifact != null )
262         {
263             sb.append( ":" ).append( artifact.getVersion() );
264         }
265         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         if ( bundleDir == null )
278         {
279             return bundleDir;
280         }
281 
282         // Using slashes
283         bundleDir = bundleDir.replace( '\\', '/' );
284 
285         // Remove '/' prefix if any so that directory is a relative path
286         if ( bundleDir.startsWith( "/" ) )
287         {
288             bundleDir = bundleDir.substring( 1, bundleDir.length() );
289         }
290 
291         if ( bundleDir.length() > 0 && !bundleDir.endsWith( "/" ) )
292         {
293             // Adding '/' suffix to specify a directory structure if it is not empty
294             bundleDir = bundleDir + "/";
295         }
296 
297         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         if ( first != null )
310         {
311             return first.equals( second );
312         }
313         else
314         {
315             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         this.uri = uri;
326 
327     }
328 }