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 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      protected Boolean excluded = Boolean.FALSE;
62  
63      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      {
73      }
74  
75      /**
76       * Creates an ear module from the artifact.
77       *
78       * @param a the artifact
79       */
80      public AbstractEarModule( Artifact a )
81      {
82          this.artifact = a;
83          this.groupId = a.getGroupId();
84          this.artifactId = a.getArtifactId();
85          this.classifier = a.getClassifier();
86          this.bundleDir = null;
87      }
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          if ( artifact == null )
94          {
95              // Make sure that at least the groupId and the artifactId are specified
96              if ( groupId == null || artifactId == null )
97              {
98                  throw new MojoFailureException(
99                      "Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" );
100             }
101             final ArtifactRepository ar = EarExecutionContext.getInstance().getArtifactRepository();
102             artifact = ar.getUniqueArtifact( groupId, artifactId, getType(), classifier );
103             // Artifact has not been found
104             if ( artifact == null )
105             {
106                 Set candidates = ar.getArtifacts( groupId, artifactId, getType() );
107                 if ( candidates.size() > 1 )
108                 {
109                     throw new MojoFailureException( "Artifact[" + this + "] has " + candidates.size() +
110                         " candidates, please provide a classifier." );
111                 }
112                 else
113                 {
114                     throw new MojoFailureException( "Artifact[" + this + "] " + "is not a dependency of the project." );
115                 }
116             }
117         }
118     }
119 
120     public Artifact getArtifact()
121     {
122         return artifact;
123     }
124 
125     public String getUri()
126     {
127         if ( uri == null )
128         {
129             if ( getBundleDir() == null )
130             {
131                 uri = getBundleFileName();
132             }
133             else
134             {
135                 uri = getBundleDir() + getBundleFileName();
136             }
137         }
138         return uri;
139     }
140 
141     /**
142      * Returns the artifact's groupId.
143      *
144      * @return the group Id
145      */
146     public String getGroupId()
147     {
148         return groupId;
149     }
150 
151     /**
152      * Returns the artifact's Id.
153      *
154      * @return the artifact Id
155      */
156     public String getArtifactId()
157     {
158         return artifactId;
159     }
160 
161     /**
162      * Returns the artifact's classifier.
163      *
164      * @return the artifact classifier
165      */
166     public String getClassifier()
167     {
168         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         if ( bundleDir != null )
180         {
181             bundleDir = cleanBundleDir( bundleDir );
182         }
183         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         if ( bundleFileName == null )
195         {
196             bundleFileName = EarExecutionContext.getInstance().getFileNameMapping().mapFileName( artifact );
197         }
198         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         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         return excluded.booleanValue();
223     }
224 
225     public Boolean shouldUnpack()
226     {
227         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         if ( getAltDeploymentDescriptor() != null )
239         {
240             writer.startElement( ALT_DD );
241             writer.writeText( getAltDeploymentDescriptor() );
242             writer.endElement();
243         }
244     }
245 
246     public String toString()
247     {
248         StringBuffer sb = new StringBuffer();
249         sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId );
250         if ( classifier != null )
251         {
252             sb.append( ":" ).append( classifier );
253         }
254         if ( artifact != null )
255         {
256             sb.append( ":" ).append( artifact.getVersion() );
257         }
258         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         if ( bundleDir == null )
271         {
272             return bundleDir;
273         }
274 
275         // Using slashes
276         bundleDir = bundleDir.replace( '\\', '/' );
277 
278         // Remove '/' prefix if any so that directory is a relative path
279         if ( bundleDir.startsWith( "/" ) )
280         {
281             bundleDir = bundleDir.substring( 1, bundleDir.length() );
282         }
283 
284         if ( bundleDir.length() > 0 && !bundleDir.endsWith( "/" ) )
285         {
286             // Adding '/' suffix to specify a directory structure if it is not empty
287             bundleDir = bundleDir + "/";
288         }
289 
290         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         if ( first != null )
303         {
304             return first.equals( second );
305         }
306         else
307         {
308             return second == null;
309         }
310     }
311 }